Highcharts用0填充散点图数据

时间:2014-07-07 13:41:32

标签: java javascript highcharts

我有一个散点图如下所示(编辑后显示我想要的内容)。我有一段时间的数据(蓝点),但不包含数据的日子我想填写0(黑点)。似乎没有在high charts docs中执行此功能的功能。填写数据的最佳方法是什么?我的图表是使用JSON制作的。

我的JSON采用{x:value,y:value}格式,x是以毫秒为单位的日期,y是实际值。

[{"x":1398952144380,"y":3},{"x":1401976144380,"y":30},{"x":1402062544380,"y":9},{"x":1402148944380,"y":11},{"x":1402235344380,"y":30},{"x":1402321744380,"y":23},{"x":1402408144380,"y":1},{"x":1402580944380,"y":28},{"x":1402667344380,"y":32},{"x":1402753744380,"y":17},{"x":1402840144380,"y":35},{"x":1402926544380,"y":39},{"x":1403012944380,"y":6},{"x":1403185744380,"y":30},{"x":1403272144380,"y":27},{"x":1403358544380,"y":14},{"x":1403444944380,"y":24},{"x":1403531344380,"y":16},{"x":1403617744380,"y":9},{"x":1403790544380,"y":2},{"x":1403876944380,"y":21},{"x":1403963344380,"y":46},{"x":1404049744380,"y":33},{"x":1404136144380,"y":38},{"x":1404222544380,"y":6},{"x":1404308944380,"y":28},{"x":1404395344380,"y":34},{"x":1404481744380,"y":28},{"x":1404568144380,"y":16},{"x":1404654544380,"y":25},{"x":1404740944380,"y":9},{"x":1404913744380,"y":13},{"x":1405000144380,"y":19},{"x":1405086544380,"y":28},{"x":1405172944380,"y":20},{"x":1405259344380,"y":23},{"x":1405345744380,"y":12},{"x":1405518544380,"y":20},{"x":1405604944380,"y":22},{"x":1405691344380,"y":30},{"x":1405777744380,"y":27},{"x":1405864144380,"y":25},{"x":1405950544380,"y":16},{"x":1406123344380,"y":13},{"x":1406209744380,"y":23},{"x":1406296144380,"y":20},{"x":1406382544380,"y":31},{"x":1406468944380,"y":31},{"x":1406555344380,"y":3},{"x":1406728144380,"y":12},{"x":1406900944380,"y":13},{"x":1406987344380,"y":22},{"x":1407073744380,"y":22},{"x":1407246544380,"y":8}]

我更喜欢用Java做这个服务器端。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式扩展点数组:http://jsfiddle.net/7VNwk/4/

    var interval = 172800000, //interval between two points - two days?
        dataLength = data.length, 
        startingPoint = data[dataLength-1].x - 162 * 24 * 3600 * 1000, // half year from last point
        extendedData = [],
        i = 0;

    while(i < dataLength) {
        var actual = data[i];
        if(startingPoint < actual.x) {
            extendedData.push({x: startingPoint, y: 0});  
        } else {
            extendedData.push(actual); 
            i++;
        }
        startingPoint += interval;
    }

    $('#container').highcharts({
        xAxis: {
            type: 'datetime'   
        },
        series: [{
            type: 'scatter',
            data: extendedData
        }]
    });

当然,这取决于你的积分间隔。正如我从您的数据中看到的那样,它大约是2天。