When hovering over a section I want to see the category its in as the title (A, B, C..) and some other data as the label. No matter what code I put in options.tooltip.callbacks nothing changes. The bellow bit is from the official docs here. I don’t understand what I’m doing wrong. Advice is very appreciated.
JS:
var ctx = document.getElementById('greenhouse').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10'],
datasets: [{
label: "A",
data: [20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
backgroundColor: 'rgba(56, 158, 13, 1)',
},
{
label: "B",
data: [20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
backgroundColor: 'rgba(169, 201, 17, 1)',
},
{
label: "C",
data: [20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
backgroundColor: 'rgba(249, 157, 2, 1)',
},
{
label: "D",
data: [20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
backgroundColor: 'rgba(247, 89, 171, 1)',
},
{
label: "E",
data: [20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
backgroundColor: 'rgba(210, 97, 81, 1)',
},
]
},
options: {
plugins: {
title: {
display: true,
text: 'Greenhouse'
},
tooltip: {
callbacks: {
label: function(data) {
let label = data.dataset.label || '';
if (label) {
label += ': ';
}
if (data.parsed.y !== null) {
label += new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
}).format(data.parsed.y);
}
return label;
}
}
},
},
responsive: true,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true,
}],
},
},
});
HTML:
<link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<div class="container">
<div class="row">
<div class="col">
<canvas id="greenhouse" width="100%"></canvas>
</div>
</div>
</div>
jsfiddle – https://jsfiddle.net/o5jp7aqk/