Charts are initiliazing with the Api fetch data in my project.Chart.js pie chart initiliaze with data when page is opened but bar chart is not. I need to click the legend labels.After that I can see the result. When I look at the console it gives the following error. I couldn't understand the problem.
error
at parseVisibleItems (core.interaction.js:39)
at getIntersectItems (core.interaction.js:55)
at indexMode (core.interaction.js:117)
at Chart.getElementsAtEventForMode (core.controller.js:643)
at Chart.handleEvent (core.controller.js:863)
at Chart.eventHandler (core.controller.js:821)
at listener (core.controller.js:758)
at HTMLCanvasElement.proxies.<computed> (platform.dom.js:410)
at ZoneDelegate.invokeTask (zone.js:406)
at Object.onInvokeTask (core.js:28664)
component.ts
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Data } from '@angular/router';
import { NbThemeService, NbColorHelper } from '@nebular/theme';
import { SharedService } from '../../../@core/utils/shared.service';
import { Chart } from 'chart.js';
@Component({
selector: 'ngx-chartjs-bar-yearlyTL',
template: `<canvas id="bartlcanvas"></canvas>`,
})
export class ChartjsYearBarComponent implements OnInit, OnDestroy {
options: any;
data: Data[];
dataArrayTL = [];
dataLabel1 = [];
chart = [];
themeSubscription: any;
constructor(private service :SharedService,private theme: NbThemeService) {}
getTL(){
this.service.getYearlyTLIncome().subscribe((result: Data[]) =>
{ result.forEach(x => {
this.dataLabel1.push(x.YIL);
this.dataArrayTL.push(x.DOVIZLI);
});
});
}
ngOnInit():void{
this.themeSubscription = this.theme.getJsTheme().subscribe(config => {
const colors: any = config.variables;
const chartjs: any = config.variables.chartjs;
this.getTL();
this.chart = new Chart('bartlcanvas',{
type: 'bar',
data: {
labels: this.dataLabel1,
datasets: [{
data:this.dataArrayTL,
label: 'TL',
backgroundColor: NbColorHelper.hexToRgbA(colors.primaryLight, 0.8),
borderColor: colors.info
}],
},
options : {
maintainAspectRatio: false,
responsive: true,
legend: {
labels: {
fontColor: chartjs.textColor,
},
},
scales: {
xAxes: [
{
gridLines: {
display: true,
color: chartjs.axisLineColor,
},
ticks: {
fontColor: chartjs.textColor,
},
},
],
yAxes: [
{
gridLines: {
display: true,
color: chartjs.axisLineColor,
},
ticks: {
fontColor: chartjs.textColor,
},
}
],
},
}
});
});
}
ngOnDestroy(): void {
this.themeSubscription.unsubscribe();
}
}