Issue
i have been trying to use the Chart.js in Ionic 5 by looking this tutorial (https://ichi.pro/es/ionic-5-charts-graphs-usando-la-biblioteca-chartjs-102145521332905) But I get this error:
The code is almost identical except for the file names. Don't really understand why it happens, any help is welcome.
import { Component, AfterViewInit, ViewChild , ElementRef } from '@angular/core';
import {Chart} from "chart.js";
@Component({
selector: 'app-tab3',
templateUrl: 'tab3.page.html',
styleUrls: ['tab3.page.scss']
})
export class Tab3Page implements AfterViewInit{
@ViewChild('lineCanvas') private lineCanvas: ElementRef;
lineChart: any;
constructor() { }
ngAfterViewInit() {
this.lineChartMethod();
}
lineChartMethod() {
this.lineChart = new Chart(this.lineCanvas.nativeElement, {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December'],
datasets: [
{
label: 'Sell per week',
fill: false,
//lineTension: 0.1,
backgroundColor: 'rgba(75,192,192,0.4)',
borderColor: 'rgba(75,192,192,1)',
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: 'rgba(75,192,192,1)',
pointBackgroundColor: '#fff',
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: 'rgba(75,192,192,1)',
pointHoverBorderColor: 'rgba(220,220,220,1)',
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [65, 59, 80, 81, 56, 55, 40, 10, 5, 50, 10, 15],
spanGaps: false,
}
]
}
});
}
}
Here's the HTML
<ion-content [fullscreen]="true">
<div class="ion-padding">
<ion-card>
<ion-card-header>
Line Chart
</ion-card-header>
<ion-card-content>
<canvas #lineCanvas style="position: relative; height:20vh; width:40vw"></canvas>
</ion-card-content>
</ion-card>
</div>
</ion-content>
Solution
Chart.js need to register the controller before loading datas,but whatever the underneath mechanics, here is from the chart.js docs the proper way to init Chart in typescript :
A short registration format is also available to quickly register everything.
import { Chart, registerables } from 'chart.js'; Chart.register(...registerables);
And finally there is an separate path to do just the above for you, in one line:
import Chart from 'chart.js/auto';
source :https://www.chartjs.org/docs/master/getting-started/integration.html
Answered By - Patrick Ciseran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.