I am fetching json data in the form:
[2193,3635,8417,0]
from localhost:8080. I want this dataset to act as canvas data in the form of pie chart.
Here is one.html code:
<div>
<canvas
baseChart
[chartType]="'pie'"
[datasets]="pieChartData"
[labels]="pieChartLabels"
[options]="pieChartOptions"
[legend]="true"
[colors]="pieChartColor"
(chartClick)="onChartClick($event)">
</canvas>
</div>
Here is one.ts code:
constructor(private router: Router, private userService: UserService,private http: HttpClient) { }
pieChartOptions = {
responsive: true
}
pieChartLabels = ['PENDING', 'SUCCESS', 'FAIL', 'CREATED'];
// CHART COLOR.
pieChartColor:any = [
{
backgroundColor: ['rgba(30, 169, 224, 0.8)',
'rgba(255,165,0,0.9)',
'rgba(139, 136, 136, 0.9)',
'rgba(255, 161, 181, 0.9)',
]
}
]
pieChartData:any = [
{
data: []
}
];
ngOnInit() {
this.http.get('http://localhost:8080/Graph', {responseType: 'json'}).subscribe(
data => {
this.pieChartData = data as any []; // FILL THE CHART ARRAY WITH DATA.
},
(err: HttpErrorResponse) => {
console.log (err.message);
}
);
}
onChartClick(event: any) {
console.log(event);
}
But while running the code, I am not able to see the pie chart, with labels strikedthrough and getting this console error:
ListUserComponent.html:25 ERROR TypeError: Cannot read property 'length' of undefined
at ng2-charts.js:382
at Array.filter (<anonymous>)
at BaseChartDirective.push../node_modules/ng2-charts/fesm5/ng2-charts.js.BaseChartDirective.ngDoCheck (ng2-charts.js:377)
at checkAndUpdateDirectiveInline (core.js:10100)
at checkAndUpdateNodeInline (core.js:11363)
at checkAndUpdateNode (core.js:11325)
at debugCheckAndUpdateNode (core.js:11962)
at debugCheckDirectivesFn (core.js:11922)
at Object.eval [as updateDirectives] (ListUserComponent.html:25)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:11914)
Is there any mistake in the code? I am not sure of pieChartData properly taking the json object, or is there any other way to fetch json data?
Please, can I get a help?