在 chart.js 中暂停水平滚动以获得实时数据

Pause horizontal scrolling in chart.js for real time data

我正在开发带有实时图表的 chart.js 插件,所以我想暂停滚动图表,但是当我动态调用暂停时它不起作用。

pausestart:boolean;

plugins: {
      streaming: {
          onRefresh: function(chart: any) {
            chart.data.datasets.forEach(function(dataset: any) {
              dataset.data.push({
                x: Date.now(),
                y: Math.random()
              });
            });
          },
          duration: 12000,
          refresh: 100,
          delay: 1000,
          frameRate: 60,
          pause: this.pausestart
        }
      }

我正在设置 pausestart在单击暂停按钮后将其设置为真

最初为假
pause() {
    this.pausestart = true;
  }

  start() {
    this.pausestart = false;
  }

我该如何解决这个问题。

您可以使用 @ViewChild 获得 BaseChartDirective,然后使用 chart 属性 设置 pause 选项并调用 update() .

import { Component, ViewChild } from '@angular/core';
import { BaseChartDirective } from 'ng2-charts';

export class AppComponent {
  @ViewChild(BaseChartDirective) chart: BaseChartDirective;

  pause() {
    this.chart.chart.options.plugins.streaming.pause = true;
    this.chart.chart.update({duration: 0});
  }

  start() {
    this.chart.chart.options.plugins.streaming.pause = false;
    this.chart.chart.update({duration: 0});
  }

  ...