【发布时间】:2022-01-20 05:10:53
【问题描述】:
图表在我的项目中使用 Api 获取数据进行初始化。当页面打开但条形图未打开时,Chart.js 饼图会使用数据进行初始化。我需要点击图例标签。之后我可以看到结果。 当我查看控制台时,它会出现以下错误。我无法理解这个问题。
错误
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)
组件.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();
}
}
【问题讨论】:
标签: angular chart.js asp.net-core-webapi angular-services