I'm having trouble getting the datalabels plugin to work with Chart.js. No labels are showing up on my graph, even though I am using version 2.9.0 of Chart.js.
Here is my code:
var ctx = this.pieChartRef.nativeElement;
this.pieChart = new Chart(ctx, {
type: 'pie',
data: {
labels: Object.keys(pieData),
datasets: [
{
data: Object.values(pieData),
backgroundColor: random,
},
],
},
options: {
legend: {
position: 'bottom',
display: true,
},
plugins: {
datalabels: {
color: '#fff',
display: true,
formatter: function (value, ctx) {
let sum = 0;
let dataArr: number[] = ctx.chart.data.datasets[0]
.data as number[];
dataArr.map((data) => {
sum += data;
});
return ((value * 100) / sum).toFixed(2) + '%';
},
},
},
},
});
Even when I simplify the formatter code to just return value + '%';
, still no datalabels appear. Can anyone offer some guidance? I'm at a loss!
Appreciate any help in advance.