Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChartJS add tooltip to a grouped bar chart

Iam trying to add an euro sign to the tooltips of my grouped bar chart using ChartJS. Snipped:

tooltips: {
   mode: 'label',
   callbacks: {
      label: function(tooltipItem, data) {
         return data['datasets'][0]['data'][tooltipItem['index']] + '€';
      }
   }
}

This code works for my linechart, but not for my grouped bar chart. I want my bar chart to look like the following, when I hover it:

enter image description here

But there is no euro sign in my chart, it just display its value. What am I doing wrong?

Thank you.

** Edit

So my full options looked like the following:

options: {
            title: {
                display: true,
                text: 'Title',
            },
            scales: {
                yAxes: [{
                    scaleLabel: {
                        display: true,
                        labelString: 'Wert in €'
                    }
                }],
                xAxes: [{
                    scaleLabel: {
                        display: true,
                        labelString: 'Zeitintervall'
                    }
                }]
            },
            tooltips: {
                mode: 'label',
                    callbacks: {
                        label: function(tooltipItem, data) {
                            return data['datasets'][0]['data'][tooltipItem['index']] + '€';
                        }
                    }
            }
        }

As soon as i removed the scales, it is showing the euro sign. So my options now look like the following:

options: {
            title: {
                display: true,
                text: 'Title'
            },
            tooltips: {
                mode: 'label',
                callbacks: {
                    label: function(tooltipItem, data) {
                        return data['datasets'][0]['data'][tooltipItem['index']] + ' €';
                    }
                }
            }
        }

But now i got another problem, it shows the same value for two different bars: enter image description here

You can see clearly that the values are not the same. Whats the problem here?

like image 336
Moritz Schmidt Avatar asked Dec 20 '17 21:12

Moritz Schmidt


2 Answers

This can be achieved using the following tooltips label callback function :

tooltips: {
   mode: 'label',
   callbacks: {
      label: function(t, d) {
         var dstLabel = d.datasets[t.datasetIndex].label;
         var yLabel = t.yLabel;
         return dstLabel + ': ' + yLabel + ' €';
      }
   }
}

FYI: This has nothing to do with scales. It would work perfectly fine along with scales

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩

var myChart = new Chart(ctx, {
   type: 'bar',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
      datasets: [{
         label: 'DST1',
         backgroundColor: '#3e95cd',
         data: [3, 2, 4, 5, 1]
      }, {
         label: 'DST2',
         backgroundColor: '#8e5ea2',
         data: [2, 4, 1, 2, 5]
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 1
            }
         }]
      },
      title: {
         display: true,
         text: 'Title'
      },
      tooltips: {
         mode: 'label',
         callbacks: {
            label: function(t, d) {
               var dstLabel = d.datasets[t.datasetIndex].label;
               var yLabel = t.yLabel;
               return dstLabel + ': ' + yLabel + ' €';
            }
         }
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.js"></script>
<canvas id="ctx"></canvas>
like image 109
ɢʀᴜɴᴛ Avatar answered Nov 19 '22 22:11

ɢʀᴜɴᴛ


A bit late to the party, but I have recently discovered JavaScript's built-in formatter for currencies, that saves you having to play about with strings and could be helpful here, and is reusable elsewhere in your code!:

const gbp = new Intl.NumberFormat('en-GB', {
    style: 'currency',
    currency: 'GBP',
    minimumFractionDigits: 2
});

Being English I have of course done it in GBP, but I found that changing the en-GB to de-DE and the 'GBP' to 'EUR' worked absolutely fine. Even formatting the decimal points and thousand separators correctly.

EDIT: including how to actually give the formatter a number to format would be useful wouldn't it!

gbp.format(10000); // Returns £10,000.00
like image 42
Gavin Avatar answered Nov 19 '22 22:11

Gavin