jquery - ng2-charts 实时数据动画

我将 Angular 2 和 ng2-charts 用于需要显示实时数据的仪表板。

目前,我的问题是当我更改与图表关联的数据时,图表会重新绘制所有数据点。由于此数据将逐秒传入,因此这不适合我的目的。

我想做一些看起来像这个演示的东西:http://www.highcharts.com/demo/dynamic-update但我没有 highcharts 的预算。

我的代码如下:

组件.ts:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass } from '@angular/common';

import { CHART_DIRECTIVES } from 'ng2-charts';

@Component({
    selector: 'my-dashboard',
    templateUrl: 'app/dashboard.component.html',
    styleUrls: ['app/dashboard.component.css'],
    directives: [CHART_DIRECTIVES, NgClass, CORE_DIRECTIVES, FORM_DIRECTIVES]
})
export class DashboardComponent {
    // lineChart
    public lineChartData: Array<any> = [
        { data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
        { data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' },
        { data: [18, 48, 77, 9, 100, 27, 40], label: 'Series C' }
    ];
    public lineChartLabels: Array<any> = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
    public lineChartOptions: any = {
        responsiveAnimationDuration: 5000,
        animation: {},
        responsive: true
    };
    public lineChartColours: Array<any> = [
        { // grey
            backgroundColor: 'rgba(148,159,177,0.2)',
            borderColor: 'rgba(148,159,177,1)',
            pointBackgroundColor: 'rgba(148,159,177,1)',
            pointBorderColor: '#fff',
            pointHoverBackgroundColor: '#fff',
            pointHoverBorderColor: 'rgba(148,159,177,0.8)'
        },
        { // dark grey
            backgroundColor: 'rgba(77,83,96,0.2)',
            borderColor: 'rgba(77,83,96,1)',
            pointBackgroundColor: 'rgba(77,83,96,1)',
            pointBorderColor: '#fff',
            pointHoverBackgroundColor: '#fff',
            pointHoverBorderColor: 'rgba(77,83,96,1)'
        },
        { // grey
            backgroundColor: 'rgba(148,159,177,0.2)',
            borderColor: 'rgba(148,159,177,1)',
            pointBackgroundColor: 'rgba(148,159,177,1)',
            pointBorderColor: '#fff',
            pointHoverBackgroundColor: '#fff',
            pointHoverBorderColor: 'rgba(148,159,177,0.8)'
        }
    ];
    public lineChartLegend: boolean = true;
    public lineChartType: string = 'line';

    public randomize(): void {
        let _lineChartData: Array<any> = new Array(this.lineChartData.length);
        for (let i = 0; i < this.lineChartData.length; i++) {
            _lineChartData[i] = { data: new Array(this.lineChartData[i].data.length), label: this.lineChartData[i].label };
            for (let j = 0; j < this.lineChartData[i].data.length; j++) {
                _lineChartData[i].data[j] = Math.floor((Math.random() * 100) + 1);
            }
        }
        this.lineChartData = _lineChartData;
    }

    // events
    public chartClicked(e: any): void {
        console.log(e);
    }

    public chartHovered(e: any): void {
        console.log(e);
    }
}

组件.html:

<div class="row">
    <div class="col-md-6">
        <base-chart class="chart"
                    [datasets]="lineChartData"
                    [labels]="lineChartLabels"
                    [options]="lineChartOptions"
                    [colors]="lineChartColours"
                    [legend]="lineChartLegend"
                    [chartType]="lineChartType"
                    (chartHover)="chartHovered($event)"
                    (chartClick)="chartClicked($event)"></base-chart>
    </div>
    <div class="col-md-6" style="margin-bottom: 10px;">
        <button class="btn btn-default" (click)="randomize()">Randomize Data</button>
    </div>
</div>

有谁知道我怎样才能使它随着数据的变化而平滑地动画化,而不是重新绘制整个图形?

最佳答案

您可以尝试为新数据实现推送方法,如下所示:

public pushOne(): void {
    this.lineChartData.forEach((x, i) => {
      const num = this.generateNumber(i);
      const data: number[] = x.data as number[];
      data.push(num);
    });
    this.lineChartLabels.push(`Label ${this.lineChartLabels.length}`);
  }

您还可以使用@ViewChild 装饰器以编程方式控制图表的渲染(或不渲染)部分:

@ViewChild(BaseChartDirective, { static: true }) chart: BaseChartDirective;

https://stackoverflow.com/questions/39084467/

相关文章:

c# - 正则表达式,匹配任何未包含在

amazon-web-services - 您如何动态授予 AWS Lambda 函数访问资源的权限

asp.net-mvc - 如何在帐户 Controller 之外创建新的用户身份?

php - 使用 cakephp 2.0 在 sitemap.xml 中显示来自数据库的数据

apache - htaccess 不适用于本地主机

.net - 在 WPF .Net 中测量 UI 性能的正确方法

html - 上传多个文件或目录

office365 - outlook.com rest api 不正确的组织者电子邮件

android - 在 Bundle 中传递对象 - ClassNotFoundException

vagrant - 是否可以触发 Packer 的后处理器跳过构建步骤?