I have an Angular 7 application and I want to use ng2-charts to draw charts. My application is available here on GitHub.
I followed the guide to install the library which is available here:
npm install --save ng2-charts
npm install --save chart.js
I created a component and I added the following code:
Template
<div style="display: block;" class="chart">
<canvas baseChart
[datasets]="labelMFL"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[chartType]="lineChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
</div>
Component class:
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);
}
}
Then I instantiated it in another component:
<app-card title="Graph" showTextContent="false">
<app-bar-chart></app-bar-chart>
</app-card>
When I start the application, I get the following error:

I thought that there was some issues about chart.js, but I don't know if I have to add it somewhere, like the angular.json file.
Am I missing something in the configuration?
Is there some problems with the version of the libraries?
