如果您使用 Angular Chart.js,并且发现数据集不会改变,您可以尝试以下方法:
首先,确保您使用的是 Angular Chart.js 版本。然后,您可以尝试在图表组件中添加一个 *ngIf
条件,将其绑定到数据集。这将强制组件重新渲染并更新数据集。例如:
另外,请确保您正确地使用了 Angular 的变更检测机制来检测数据集的变化。如果您手动更改了数据集而不使用 ngOnChanges()
,则可能需要手动触发变化检测。例如:
import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { ChartDataSets, ChartOptions } from 'chart.js';
import { Label } from 'ng2-charts';
@Component({
selector: 'app-line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.css']
})
export class LineChartComponent implements OnInit, OnChanges {
@Input() data: ChartDataSets[];
@Input() labels: Label[];
public options: ChartOptions = {
responsive: true,
maintainAspectRatio: false
};
public chartType = 'line';
constructor() { }
ngOnInit(): void {
}
ngOnChanges(changes: SimpleChanges) {
if (changes.data && !changes.data.firstChange) {
// 手动触发变化检测
this.ngOnInit();
}
}
}
上一篇:Angular产品图像压缩