doutan1857 2013-05-06 21:52
浏览 90
已采纳

在javascript中的Highchart系列更新

I'm trying to update a Highchart series by generating a new array with the current data from the database. But for some reason I can only find info about going through the data one at a time. Once a chart is created, its only re-running the same code again so the labels etc don't change - only the [pointStart] and the [data] change.

Is there a way to update all the data as a whole? I also haven't managed to get a for loop to work correctly.

function generateSeries(data){
    var cData = [];
    var rollup = data.rollupData;
    cData['type'] = 'line';
    cData['pointStart'] = convertDateTime(data.lastMinute);
    cData['pointInterval'] = 60 * 1000;
    for(var i in rollup){
        var x = new Array();
        var v = rollup[i].rttSystem;
        switch(v){
            case('line'): var vn = ' (L)'; break;
            case('node'): var vn = ' (N)'; break;
            case('module'): var vn = ' (M)'; break;
            default: var vn = '';
        }
        x['name'] = rollup[i].rollupName + vn;
        x['data'] = rollup[i].kpiData;
        cData.push(x);
    }
    return cData;
}

the ouput of the array looks like [0: ['name': 'California', 'data': [0.002, 0.003 ...],1: ['name':'Oklahoma', 'data': [0.001, 0.002 ...], 'type': 'line', 'pointStart':'','pointInterval':60 * 1000]

var chart = new Highcharts.Chart({
    chart: {
    ...
    },
    series: generateSeries(data)
    }, function(chart){
        setInterval(function(){
            var newSeries = generateSeries(data);
            // Udate the series again with the current data
        },60000);
    }
});

Update: this does the series but not the pointStart.

if(chart.series.length > 1){
    var s = 0;
    for(var i in newSeries){
        chart.series[s].setData(newSeries[i].data);
        chart.series[s].pointStart = newSeries[i].pointStart;
        s++;
    }
}else{
    chart.series[0].setData(newSeries[0].data);
    chart.series[0].pointStart = newSeries[0].pointStart;
}
  • 写回答

2条回答 默认 最新

  • dongrao9454 2013-05-07 16:18
    关注

    To update a chart I usually do this:

    chart.series[0].update({
        pointStart: newSeries[0].pointStart,
        data: newSeries[0].data
    }, true); //true / false to redraw
    

    Try calling redraw after the update.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退