I have a piechart with four labels:
var data = [{
data: [50, 55, 60, 33],
labels: [\"India\", \"China\", \"US\", \"Canada\"],
backgroundColor: [
You could use the tooltip with an Array reducer to perform the percentage calculation and display it.
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
try {
let label = ' ' + data.labels[tooltipItem.index] || '';
if (label) {
label += ': ';
}
const sum = data.datasets[0].data.reduce((accumulator, curValue) => {
return accumulator + curValue;
});
const value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
label += Number((value / sum) * 100).toFixed(2) + '%';
return label;
} catch (error) {
console.log(error);
}
}
}
}