I am currently experiencing a problem with plotting a ChartJs bar chart in my Angular application using data retrieved from an API response.
Below is the code snippet:
<div class="canvas">
<canvas id="canvas" baseChart [data]="barChartData" [options]="barChartOptions" [plugins]="barChartPlugins"
[legend]="barChartLegend" [type]="'bar'">
</canvas>
</div>
this.barChartData = new Chart('canvas', {
type:"bar",
data: {
labels: ["Private", "NASHA", "NHIS", "Others"],
datasets:[{
data: [
this.apiResponse?.Private,
this.apiResponse?.data_1
],
label: 'hey',
backgroundColor: [ '#E8E2F4']
}]
},
options:{
scales: {
y:{
beginAtZero:true
}
}
}
})
The issue lies in the fact that the chart is not populating. When I console log the API response before creating the chart, the values are as expected. However, after creating the chart, the values return undefined.
What could be causing this issue and how can I resolve it?