如何使用Highcharts实现从右到左动态的动态转换?

时间:2016-03-18 02:22:11

标签: javascript jquery dynamic highcharts shift

我使用Highcharts绘制带有动态数据的图表。

我找到了这个官方演示。http://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>

图表窗口大小是变化的,看起来像the offcial dynamic effect here

Howerver,我需要修复图形窗口大小,此图形中的数据从一开始就从右向左移动。就像下面的图片enter image description here

一样

如何使用Highcharts获得图片2中的效果?

1 个答案:

答案 0 :(得分:3)

您可以使用空点来创建空白空间,然后只需使用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);