'Chart.js - How to display the percentage and an image at the same time on top of chart slice?
Been around and around this, even tried to use to plugins but this doenst seem to work.
Basically and want to display two percentage and an image on top of every slice of the chart.
I was only able to display one or the other and I dont know what is wrong.
Can someone help me out? It's the first time I'm using this.
<div style=' display: flex;
justify-content: center;
align-items: center;' class="chartCard">
<div style='width: 400px; height: 400px;' class="chartBox">
<canvas id="myChart"></canvas>
</div>
</div>
const data = {
labels: ["Cruelty Free", "Fair Trade", "Local Production", "Non-Toxic", "Recycling", "Social Projects", "Sustainable Innovation", 'Handmade', 'Vegan'],
datasets: [{
label: 'Weekly Sales',
data: [{{ cruel }}, {{ fair }}, {{ local }}, {{ toxic }}, {{ rec }}, {{ pro }}, {{ in }}, {{ hand }}, {{ vegan }}],
backgroundColor: [
"#2ecc71",
"#5b8a66",
"#6faa7d",
"#9ce6ae",
"#344e3a",
"#243528",
"#216c33",
'#abf0bb',
'#e2fce9'
],
}]
};
// config
const config = {
type: 'doughnut',
data,
options: {
plugins:{
datalabels: {
formatter: (value, ctx) => {
let datasets = ctx.chart.data.datasets;
if (datasets.indexOf(ctx.dataset) === datasets.length - 1) {
let sum = datasets[0].data.reduce((a, b) => a + b, 0);
let percentage = Math.round((value / sum) * 100) + '%';
return percentage;
} else {
return percentage;
}
},
color: '#fff',
},
tooltip:{
enbled: false
},
},
},
labels: {
render: 'image',
textMargin: -60,
images: [
null,
null,
{
src: 'https://i.stack.imgur.com/9EMtU.png',
width: 20,
height: 20
},
null
]
},
},
};
{% endif %}
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
