gpt4 book ai didi

jquery - ng2-charts 实时数据动画

转载 作者:行者123 更新时间:2023-12-05 07:48:00 25 4
gpt4 key购买 nike

我将 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;

关于jquery - ng2-charts 实时数据动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39084467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com