实时动态更新曲线图,x轴时间s随数据的变化而变化

时间:2022-04-06 14:50:59

$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });
        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {
                        // set up the updating of the chart each second
                        var series = this.series[0];
                        setInterval(function () {
                            var x = (new Date()).getTime().getSecond, // current time
                                y = Math.random();
                            series.addPoint([x, y], true);
                        }, 1000);
                    }
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                tickInterval:(new Date()).getTime().getSecond
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                        this.x + '<br/>' +
                        Highcharts.numberFormat(this.y, 2);
                },
                crosshairs:[true,true]//显示十指线
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Random data',
                data: (function () {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime().getSecond,
                        i;
                    for (i = -19; i <= 0; i += 1) {
                        data.push({
                            x: time ,
                            y: Math.random()
                        });
                    }
                    return data;
                }())
            }]
        });
    });
});

实时动态更新曲线图,x轴时间s随数据的变化而变化