首页 > 解决方案 > 在 chart.js 中暂停水平滚动以获取实时数据

问题描述

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

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单击暂停按钮后,我最初设置为 false 将其设置为 true

pause() {
    this.pausestart = true;
  }

  start() {
    this.pausestart = false;
  }

我该如何解决这个问题。

标签: angularchart.jsng2-charts

解决方案


您可以获取BaseChartDirectiveusing @ViewChild,然后使用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});
  }

  ...

推荐阅读