要解决Angular中Chart.js图表未显示的问题,可以按照以下步骤进行:
npm install chart.js ngx-charts --save
import { Component } from '@angular/core';
import { ChartOptions, ChartType, ChartDataSets } from 'chart.js';
import { Label } from 'ng2-charts';
@Component({
selector: 'app-chart',
templateUrl: './chart.component.html',
styleUrls: ['./chart.component.css']
})
export class ChartComponent {
public barChartOptions: ChartOptions = {
responsive: true,
};
public barChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
public barChartType: ChartType = 'bar';
public barChartLegend = true;
public barChartPlugins = [];
public barChartData: ChartDataSets[] = [
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
{ data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }
];
}
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ChartComponent } from './chart.component';
@NgModule({
declarations: [
ChartComponent
],
imports: [
CommonModule
],
exports: [
ChartComponent
]
})
export class ChartModule { }
通过以上步骤,你应该可以在Angular中成功显示Chart.js图表了。如果图表仍然无法显示,可以检查浏览器的开发者工具控制台是否有任何错误信息,以帮助进一步排查问题。