如何在动态更新的图表上更新高价指标

时间:2017-12-17 11:59:44

标签: javascript highcharts highstock

指标仅适用于历史'图表的一部分,但在出现新点时不会更新。 http://jsfiddle.net/yp6ocybe/

    series: [{
            id: 'rand',
        name: 'Random data',
        data: (function () {
            // generate an array of random data
            var data = [],
                time = (new Date()).getTime(),
                i;

            for (i = -999; i <= 0; i += 1) {
                data.push([
                    time + i * 1000,
                    Math.round(Math.random() * 100)
                ]);
            }
            return data;
        }())
    },
    {
            type: 'sma',
            linkedTo: 'rand',
            name: 'SMA (14)'
        }, {
            type: 'sma',
            linkedTo: 'rand',
            name: 'SMA (50)',
            params: {
                period: 50
            }
        }]
});

1 个答案:

答案 0 :(得分:0)

我找到的最佳解决方案是在更新主系列时在SMA系列上手动调用update

load: function () {
  // set up the updating of the chart each second
  var series = this.series[0];
  var series1 = this.series[1];
  var series2 = this.series[2];
  setInterval(function () {
    var x = (new Date()).getTime(), // current time
        y = Math.round(Math.random() * 100);
    series.addPoint([x, y], true, true);
    //call update by passing the old options and redraw = true
    series1.update(series1.options, true);
    series2.update(series2.options, true);
  }, 1000);
}

它至少在我的机器上顺利停止动画。

http://jsfiddle.net/yp6ocybe/3/

Highcharts.setOptions({
    global: {
        useUTC: false
    }
});

// Create the chart
Highcharts.stockChart('container', {
    chart: {
        events: {
          load: function () {

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

    rangeSelector: {
        buttons: [{
            count: 1,
            type: 'minute',
            text: '1M'
        }, {
            count: 5,
            type: 'minute',
            text: '5M'
        }, {
            type: 'all',
            text: 'All'
        }],
        inputEnabled: false,
        selected: 0
    },

    title: {
        text: 'Live random data'
    },

    exporting: {
        enabled: false
    },

    series: [{
    		id: 'rand',
        name: 'Random data',
        data: (function () {
            // generate an array of random data
            var data = [],
                time = (new Date()).getTime(),
                i;

            for (i = -999; i <= 0; i += 1) {
                data.push([
                    time + i * 1000,
                    Math.round(Math.random() * 100)
                ]);
            }
            return data;
        }())
    },
    {
            type: 'sma',
            linkedTo: 'rand',
            name: 'SMA (14)'
        }, {
            type: 'sma',
            linkedTo: 'rand',
            name: 'SMA (50)',
            params: {
                period: 50
            }
        }]
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/indicators/indicators.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>