Highcharts,停止动态样条曲线图的渲染

问题描述 投票:0回答:1

我按照HighCharts文档在Angular中创建了一个动态样条曲线图。

我希望图表在单击按钮时停止渲染。

HTML:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<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>

JAVASCRIPT:

Highcharts.chart('container', {
    chart: {
        type: 'spline',
        animation: Highcharts.svg,
        events: {
            load: function () {

                // set up the updating of the chart each second
                var series = this.series[0];
                setInterval(function () {
                        y = Math.random();
                    series.addPoint(y, true, true);
                }, 1000);
            }
        }
    },
    title: {
        text: 'Live random data'
    },
    yAxis: {
        title: {
            text: 'Value'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    series: [{
        name: 'Random data',
        data: [12, 27.5, 31.4, 32.1 ]
    }]
});

JSFiddle上的图表示例:

http://jsfiddle.net/cau4mgfp/2/

javascript angular ionic-framework graph highcharts
1个回答
0
投票

它可以使用clearInterval()停止

function stopFn() {
  clearInterval(interVal);
}

function startFn() {
  var series = chart.series[0];
  interVal = setInterval(function() {
    y = Math.random();
    series.addPoint(y, true, true);
  }, 1000);
}

var interVal;
var chart;
chart = new Highcharts.chart('container', {
  chart: {
    type: 'spline',
    animation: Highcharts.svg,
    events: {
      load: function() {

        // set up the updating of the chart each second
        var series = this.series[0];
        interVal = setInterval(function() {
          y = Math.random();
          series.addPoint(y, true, true);
        }, 1000);
      }
    }
  },
  title: {
    text: 'Live random data'
  },
  yAxis: {
    title: {
      text: 'Value'
    },
    plotLines: [{
      value: 0,
      width: 1,
      color: '#808080'
    }]
  },
  series: [{
    name: 'Random data',
    data: [12, 27.5, 31.4, 32.1]
  }]
});

function stopFn() {

  clearInterval(interVal);
}

function startFn() {

  var series = chart.series[0];
  interVal = setInterval(function() {
    y = Math.random();
    series.addPoint(y, true, true);
  }, 1000);
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<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>
<button onclick="stopFn()">
Stop
</button>
<button onclick="startFn()">
Start
</button>
© www.soinside.com 2019 - 2024. All rights reserved.