23

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"

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

3 Answers 3

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"
});
Sign up to request clarification or add additional context in comments.

1 Comment

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
6

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) {
                        var 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;
                    }
                }
            }
        }
    }
);

Comments

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, ",");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.