【问题标题】:Highcharts addPoint() calling in quick interval causing shape collapse of graphHighcharts addPoint() 在快速间隔中调用导致图形形状崩溃
【发布时间】:2018-06-28 19:36:51
【问题描述】:

// To load chart
loadChart(): void {
    let initialCardiacSamples = this.cardiacSamples;
    this.ecgChart = Highcharts.chart(this.el.nativeElement, {
      chart: {
        type: 'line',
        backgroundColor: '#4d2700',
      },
      title: {
        text: 'ECG graph using Highcharts JS',
        style: {
          color: '#ffffff'
        }
      },
      xAxis: {
        type: 'number',
        tickPixelInterval: 150
      },
      yAxis: {
        title: {
          text: 'Amplitude',
          style: {
            color: '#ffffff'
          }
        },
        gridLineWidth: 0,
        minorGridLineWidth: 0,
        max: 600
      },
      series: [{
        name: 'ECG data',
        data: initialCardiacSamples,
        color: '#ffffff'
      }]
    });
  }
  
  // To update chart/ Calling update for every 300mili seconds here to take next data point
  updateChart(): void {
    let xAxisPoint = this.ecgChunkSize;
    this.interval = setInterval(() => {
    // Call addData() function to add next trace point to the graph
    this.addDataPoint(this.ecgChunkSize, xAxisPoint); 
    this.ecgChunkSize = this.ecgChunkSize + 1;
    xAxisPoint = xAxisPoint + 1;
    if(this.ecgChunkSize == this.ecgFileSize) {
    this.ecgChunkSize = 0;
    }
    }, 300);
}

// Method to add data and update the chart to make the graph real time
addDataPoint(index: number, xAxisPoint: number) : void {
  let iy = this.cardiacSamples[index].y;
  this.ecgChart.series[0].addPoint([xAxisPoint, iy], true, true);
  //Calling addDataPoint() above with redraw chart and shift as true
 }
.chart-container {
    display: block; 
    margin:0; 
    position: relative;
    height: calc(100vh - 450px - 40px);
    padding-top: 0;
}
<div #highchartsEcg class="chart-container"></div>

在这里,我每 300 毫秒用 addPoint() 调用 updateChart()。有了这个间隔,它会非常慢,但会保持一定的图形形状。但是,如果我以 Normal graph shape with 300ms interval Graph shape collapse with 100ms interval

【问题讨论】:

  • 这里没有放完整的代码。但是插入的代码足以理解初始加载和定期更新图表的场景。实际上是通过signalR从后端获取数据。

标签: javascript angular highcharts highcharts-ng angular2-highcharts


【解决方案1】:

我猜这和github上的问题一样:https://github.com/highcharts/highcharts/issues/8548

这可能是由动画引起的 - 默认情况下,动画持续时间为 1 秒。如果您每 ~ 100 毫秒更新一次图表,那么也要更改动画持续时间:

this.ecgChart.series[0].addPoint([xAxisPoint, iy], true, true, { duration: 95 });

注意: 动画持续时间应该小于更新间隔,所以我们确定动画会结束。

【讨论】:

  • Fus ...感谢您的解决方案。是的,这与我在 GitHub 以及问题部分中提出的问题相同。设置动画持续时间后它工作正常。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多