I have created a Chart JS 2 graph that have two datasets.
When I hover over a line I get only the current line's data but I want to see the other line's data too.
How can I add all the data from the specific date in the tooltip?
You can achieve this using afterBody callback function of tooltips ... options: { tooltips: { callbacks: { afterBody: function(t, d) { return 'loss 15%'; // return a string that you wish to append } } }, ... } it works, but there is also one more query for you.
To achieve this, we first add a table within the tooltip. The first column contains the categories ("women", "men"), and the second one contains the bars. In this second column, we then add HTML <div> tags and define the width of these boxes with our numerical columns.
Specifying the tooltip typeisHtml: true in the chart options passed to the draw() call, which will also allow you to create Tooltip Actions.
Use multiple labels configuration to display 3 labels per data, one for the index , one for the label and one for the value . Move the mouse over the chart to display label ids.
To add all the data from a specific label (date) in the tooltip, you need to set tooltips mode
to index
in your chart options, like so :
options: {
tooltips: {
mode: 'index'
},
...
}
ᴅᴇᴍᴏ
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'LINE 1',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(0, 119, 290, 0.2)',
borderColor: 'rgba(0, 119, 290, 0.6)'
}, {
label: 'LINE 2',
data: [4, 2, 3, 5, 1],
backgroundColor: 'rgba(0, 119, 290, 0.1)',
borderColor: 'rgba(0, 119, 290, 0.6)'
}]
},
options: {
tooltips: {
mode: 'index'
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With