22

How do i edit chartjs tooltip to add customized strings in tooltips

enter image description here

For Example: I want to change the Tooltip like "January: 28 Files" or just "28 Files"

Sudharshan
  • 3,634
  • 2
  • 27
  • 27
  • 1
    there is a tooltip configuration HTML that is available as an option when you define the Global Configuration Options: http://www.chartjs.org/docs/#getting-started-global-chart-configuration – nicholaswmin Sep 08 '14 at 12:10
  • @NicholasKyriakides didn't notice that. Now i can change the template tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %> Files" Thanks :) – Sudharshan Sep 08 '14 at 12:17

3 Answers3

11

Redefine default global tooltip template as follows:

Chart.defaults.global.tooltipTemplate =
  "<%if (label){%><%=label%>: <%}%><%= value %>";

Here is another example:

var ctx = document.getElementById("canvas").getContext("2d");

var myBarChart = new Chart(ctx).Bar(data, {
        tooltipTemplate: "<%= value %> Files"
});
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Sudharshan
  • 3,634
  • 2
  • 27
  • 27
  • This solution doesn't work for V2 of chart.js. If you are in trouble, please take a look to my answer or on this [link](http://stackoverflow.com/questions/34720530/chart-js-v2-add-prefix-or-suffix-to-tooltip-label) –  Apr 28 '17 at 09:21
4

The great previous answers do not work with chartjs 3. This example is from the official documentation:

const chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
    plugins: {
        tooltip: {
            callbacks: {
                label: function(context) {
                    const label = context.dataset.label || '';

                    if (label) {
                        label += ': ';
                    }
                    if (context.parsed.y !== null) {
                        label += new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(context.parsed.y);
                    }
                    return label;
                }}}}}});
mm_
  • 1,566
  • 2
  • 18
  • 37
1

Drawing from other responses I've found that helped me, apparently the label properties can be set to functions, For example, to format currency:

var overrides = {
  // show lables as currency
  scaleLabel: toCurrency,

  // String - Template string for single tooltips
  tooltipTemplate: toCurrency,

  // String - Template string for multiple tooltips
  multiTooltipTemplate: toCurrency
}

function toCurrency(label) {
    return '$' + label.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
John Bonfardeci
  • 466
  • 3
  • 10