npm install chart.js ng2-charts
import { Component } from '@angular/core';
import { Chart } from 'chart.js';
import { ChartType, ChartDataSets } from 'chart.js';
import { Color, Label } from 'ng2-charts';
export class ChartComponent {
chartData: ChartDataSets[] = [{ data: [], label: 'Data' }];
chartLabels: Label[] = [];
chartOptions: any = {
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
};
chartType: ChartType = 'line';
chartColors: Color[] = [
{
borderColor: 'black',
backgroundColor: 'rgba(255,255,0,0.28)',
},
];
chartPlugins = [];
}
constructor(private http: HttpClient, private zone: NgZone) {}
updateChartData() {
this.http.get('http://localhost:3000/data').subscribe((data: any) => {
this.chartData[0].data = data;
this.chartLabels = Array.from(
{ length: data.length },
(_, i) => `Label ${i}`
);
this.zone.run(() => {
this.chart.update();
});
});
}
ngOnInit() {
this.chart = new Chart('my-chart', {
type: this.chartType,
data: {
labels: this.chartLabels,
datasets: this.chartData,
},
options: this.chartOptions,
plugins: this.chartPlugins,
上一篇:Angular13+jest+Angularlibrarynotworking
下一篇:Angular13-BrowserAuthError:interaction_in_progress:Interactioniscurrentlyinprogress