2

Here is my sample chart with random data 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: []
            }]
        });
    });

});

How to update each series, not only [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);
                }

Doesn't work...

And 2nd thing... how to update those series using ajax? (I need to have 2 areas and 4 splines)

UPDATE So i change this

        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);
            }
        }

but it crashes my browser :\ http://jsfiddle.net/2tmRB/1/ (watch out with that link!) What I'm doing wrong?

4

1 回答 1

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()(如上例所示)。

于 2013-05-21T11:46:12.743 回答