Highcharts - 使用ajax更新图表

时间:2013-05-21 10:42:55

标签: javascript jquery highcharts

这是我的随机数据示例图表 http://jsfiddle.net/Fw4PZ/

$(function () {
    $(document).ready(function() {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        var chart;
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'area',
                marginRight: 10,
                events: {
                    load: function() {

                        // set up the updating of the chart each second
                        var series = this.series[1];
                        setInterval(function() {
                            var x = (new Date()).getTime(), // current time
                                y = Math.random();
                            series.addPoint([x, y], true);
                        }, 1000);
                    }
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Random data',
                data: []
            },{
                name: 'Random data',
                type: 'spline',
                data: []
            }]
        });
    });

});

如何更新每个系列,不仅仅是[1]?

                load: function() {

                    // set up the updating of the chart each second
                    var series = this.series[0];
                    setInterval(function() {
                        var x = (new Date()).getTime(), // current time
                            y = Math.random();
                        series.addPoint([x, y], true);
                    }, 1000);

                    // set up the updating of the chart each second
                    var series = this.series[1];
                    setInterval(function() {
                        var x = (new Date()).getTime(), // current time
                            y = Math.random();
                        series.addPoint([x, y], true);
                    }, 1000);
                }

不起作用......

第二件事......如何使用ajax更新这些系列? (我需要有2个区域和4个样条线)

更新 所以我改变了这个

        events: {
            load: function() {

                // set up the updating of the chart each second
                var series = this.series;
                setInterval(function() {
                    var x = (new Date()).getTime(), // current time
                        y = Math.random();

                    series[0].addPoint([x, y], false);
                    series[1].addPoint([x, y], false);
                    series[2].addPoint([x, y], false);
                    series[3].addPoint([x, y], false);
                    series[4].addPoint([x, y], false);
                    series[5].addPoint([x, y], true);
                }, 5000);
            }
        }

但它崩溃了我的浏览器:\ http://jsfiddle.net/2tmRB/1/(注意那个链接!) 我做错了什么?

1 个答案:

答案 0 :(得分:3)

示例:

var series = this.series;
setInterval(function() {
  var x = (new Date()).getTime(), // current time
  y = Math.random(), 
  y1 = Math.random();
  series[0].addPoint([x, y], false);
  series[1].addPoint([x, y1], true);
}, 1000);

实例:http://jsfiddle.net/Fw4PZ/1/

关于AJAX - 只需在setInterval中调用一些getJSON()或类似的东西,然后使用addPoint()将该响应中的值添加到图表中(如上例所示)。