I am trying to calculate and show total sum of Packed and Unpacked labels
I have calculated the values of labels but, don't know how to insert inside the tooltip as a new label
Because On the new version of Chart.js, I can access tooltip through settings> plugins> tooltip but in callbacks I got only dataset for exact one label
Chart js v3.0.2
var branches_canvas = document.getElementById('branches_canvas').getContext('2d');
var branches = new Chart(branches_canvas, {
type: 'bar',
data: {
labels: ['Org1','Org2','Org3','Org4','Org5'],
datasets: [
{
label: 'Packed',
data: [12,55,77,32,45],
backgroundColor: [
'#94E3EF',
],
hoverOffset: 4
},
{
label: 'Unpacked',
data: [56,88,22,88,40],
backgroundColor: [
'#FFA8A8',
],
}
],
},
options: {
plugins: {
tooltip: {
callbacks: {
label: function(context) {
var label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += context.parsed.y;
}
console.log('total:', context.parsed._stacks.y[0]+context.parsed._stacks.y[1]);
return label;
}
}
},
},
scales: {
x: {
stacked: true,
},
y: {
stacked: true
}
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.3.2/dist/chart.min.js"></script>
<canvas id="branches_canvas"></canvas>