如何使用Highcharts实现这种从右向左动态移动的效果呢?

How to use Highcharts to implement this dynamically shifting from right to left effect?

我正在使用 Highcharts 绘制动态数据图表。

我找到了这个官方demohttp://jsfiddle.net/vn9bk80j/

$(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(), // current time
                            y = Math.random();
                        series.addPoint([x, y], true, false);
                    }, 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: []
        }]
    });
  });
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

图表 window 大小不一,看起来像

但是,我需要固定图window的大小,这个图中的数据从一开始就整体从右向左移动。就像下图一样

如何用Highcharts得到图2的效果?

您可以使用空点创建空 space,然后只需使用 shift=true 添加品脱,参见:http://jsfiddle.net/vn9bk80j/1/

function getEmptyData() {
  var interval = 1000, // 1 second,
    numberOfPoints = 50,
    now = (new Date()).getTime(),
    min = now - interval * numberOfPoints,
    points = [];

  while (min <= now) {
    points.push([min, null]); // set null points
    min += interval;
  }
  return points;
}

和方法用例:

  series: [{
    name: 'Random data',
    data: getEmptyData()
  }]

shift=true 示例:

series.addPoint([x, y], true, true);