lundi 23 janvier 2017

Merge overlapping date intervals - bug

I'm using Highchart and I have current code: http://ift.tt/2jRYSjP

Code before any changes: http://ift.tt/2jHWo5l

I added function which merges data intervals that overlap.

  calc(points){

    let output = [];
    let calc = function() {
        let hasChange = false;
        for (let i = 0, j = points.length; i < j; i++) {
            let noCommon = true;
            for (let k = 0; k < output.length; k++) {
                if (points[i]['x'] >= output[k]['x'] &&
                    points[i]['x'] <= output[k]['x2'] ||
                    points[i]['x2'] >= output[k]['x'] &&
                    points[i]['x2'] <= output[k]['x2']) {
                    output[k]['x'] = Math.min(points[i]['x'], output[k]['x']);
                    output[k]['x2'] = Math.max(points[i]['x2'], output[k]['x2']);
                    noCommon = false;
                    hasChange = true;
                    break;
                }
            }
            if (noCommon)
                output.push(points[i]);
        }
        if (hasChange) {
            points = output;
            output = [];
            calc();
        }
    };

    calc();
    console.log(output);
    return output;
},     

But somehow these data intervals line up vertically. Did I missed something?




Aucun commentaire:

Enregistrer un commentaire