【问题标题】:Pause horizontal scrolling in chart.js for real time data在 chart.js 中暂停水平滚动以获取实时数据
【发布时间】:2018-12-07 07:04:38
【问题描述】:

我正在使用带有实时图表的 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
        }
      }

点击暂停按钮后我将pausestartinitially false设置为true

pause() {
    this.pausestart = true;
  }

  start() {
    this.pausestart = false;
  }

我该如何解决这个问题。

【问题讨论】:

    标签: angular chart.js ng2-charts


    【解决方案1】:

    您可以使用@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});
      }
    
      ...
    

    【讨论】:

    • 真棒@nagix 我错过了包含图表对象。现在它的工作谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2021-04-03
    • 2019-04-23
    • 1970-01-01
    • 2019-11-04
    • 2011-10-16
    • 1970-01-01
    • 2017-11-20
    • 2017-06-26
    相关资源
    最近更新 更多