我有一个Angular 7应用程序,我想使用ng2-charts来绘制图表。 我的应用程序在GitHub上可用。
我按照安装库的指南进行了操作,该指南在这里提供:
我创建了一个组件并添加了以下代码:
模板
组件类:
然后我在另一个组件中实例化它:
当我启动应用程序时,出现以下错误:
我认为可能与chart.js有关,但我不确定是否需要在某个地方添加它,比如angular.json文件中。
配置中是否还缺少一些东西?
库的版本是否存在问题?
我按照安装库的指南进行了操作,该指南在这里提供:
npm install --save ng2-charts
npm install --save chart.js
我创建了一个组件并添加了以下代码:
模板
<div style="display: block;" class="chart">
<canvas baseChart
[datasets]="labelMFL"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[chartType]="lineChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
</div>
组件类:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-bar-chart',
templateUrl: './bar-chart.component.html',
styleUrls: ['./bar-chart.component.less']
})
export class BarChartComponent {
public SystemName: string = "MF1";
firstCopy = false;
// data
public lineChartData: Array<number> = [ 1,8,49,50,51];
public labelMFL: Array<any> = [
{ data: this.lineChartData,
label: this.SystemName
}
];
// labels
public lineChartLabels: Array<any> = ["2018-01-29 10:00:00", "2018-01-29 10:27:00", "2018-01-29 10:28:00", "2018-01-29 10:29:00", "2018-01-29 10:30:00" ];
constructor( ) { }
public lineChartOptions: any = {
responsive: true,
scales : {
yAxes: [{
ticks: {
max : 60,
min : 0,
}
}],
xAxes: [{
min: '2018-01-29 10:08:00', // how to?
// max: '2018-01-29 10:48:00', // how to?
type: 'time',
time: {
unit: 'minute',
unitStepSize: 10,
displayFormats: {
'second': 'HH:mm:ss',
'minute': 'HH:mm:ss',
'hour': 'HH:mm',
},
},
}],
},
};
_lineChartColors:Array<any> = [{
backgroundColor: 'red',
borderColor: 'red',
pointBackgroundColor: 'red',
pointBorderColor: 'red',
pointHoverBackgroundColor: 'red',
pointHoverBorderColor: 'red'
}];
public lineChartType = 'line';
public chartClicked(e: any): void {
console.log(e);
}
public chartHovered(e: any): void {
console.log(e);
}
}
然后我在另一个组件中实例化它:
<app-card title="Graph" showTextContent="false">
<app-bar-chart></app-bar-chart>
</app-card>
当我启动应用程序时,出现以下错误:
![enter image description here](https://istack.dev59.com/agvEO.webp)