Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add all data in the tooltip of Chart JS

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.

enter image description here

How can I add all the data from the specific date in the tooltip?

like image 770
Menelaos Vergis Avatar asked Aug 05 '17 09:08

Menelaos Vergis


People also ask

How do I add more data to 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.

How do I add a chart in tooltips?

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.

How do I add a tooltip to a Google chart?

Specifying the tooltip typeisHtml: true in the chart options passed to the draw() call, which will also allow you to create Tooltip Actions.

How do you add multiple labels in Chartjs?

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.


1 Answers

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>
like image 179
ɢʀᴜɴᴛ Avatar answered Sep 25 '22 01:09

ɢʀᴜɴᴛ