How do you hide the title of a chart tooltip?
Asked Answered
M

3

18

I'm using chart js to show grouped bar chart and try to hide the title of the tooltip

Code to generate bar

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

var data = {
    labels: ["Chocolate", "Vanilla", "Strawberry"],
    datasets: [
        {
            label: "Blue",
            backgroundColor: "blue",
            data: [3,7,4]
        },
        {
            label: "Red",
            backgroundColor: "red",
            data: [4,3,5]
        },
        {
            label: "Green",
            backgroundColor: "green",
            data: [7,2,6]
        }
    ]
};

var myBarChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        barValueSpacing: 20,
        scales: {
            yAxes: [{
                ticks: {
                    min: 0,
                }
            }]
        }
    }
});

In the tooltip it showing the labels Chocolate,Vanilla,Strawberry and i have tried to hide the label with following

by setting the titlefontsize to 0 but it doesnt work

tooltipTitleFontSize: 0

and i have tried with the tooltip callbacks it disables the label blue,red,green but i wont need that

 tooltips: {
        callbacks: {
           label: function(tooltipItem) {
                  return tooltipItem.yLabel;
           }
        }
    }

Help me thanks in advance

Morgen answered 19/6, 2017 at 13:55 Comment(2)
#35432233Silviasilviculture
i have tried that but it doesnt workMorgen
S
39

To hide the title of tooltip, you need to return an empty function on tooltips title­'s callback, like so ...

options: {
   tooltips: {
      callbacks: {
         title: function() {}
      }
   },
   ...
}
Schuyler answered 19/6, 2017 at 14:4 Comment(3)
Hi I get an error Types of property 'title' are incompatible. I put it as you mentioned inside callbacksEward
The function needs to return something. Just return null and it should work. See here: https://mcmap.net/q/669001/-i-want-to-hide-the-label-in-a-tooltip-because-it-shows-undefinedHeterodyne
Chart.js 4.4, need to return empty string.Rubetta
V
11

To hide the tooltip title/label, it should be added in the options object for that chart as follows:

options: {
   plugins: {
      tooltip: {
         callbacks: {
            title : () => null // or function () { return null; }
         }
      }
   }
}

Do refer to the documentation to understand better that it should be handled with a custom callback function, and it is not a config that can be set in options directly. https://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip-callbacks

I've mentioned the same in this other thread : https://mcmap.net/q/669001/-i-want-to-hide-the-label-in-a-tooltip-because-it-shows-undefined

Vonnie answered 18/6, 2021 at 11:1 Comment(1)
This method is more accurate otherwise eslint gives the this error. Unexpected empty method 'title'.eslint@typescript-eslint/no-empty-function -- click for detail linkArtist
A
4

According to the Chart.js 4.3.1 documentation for Tooltip Callbacks:

If the callback returns undefined, then the default callback will be used. To remove things from the tooltip callback should return an empty string.

{
    options: {
        plugins: {
            tooltip: {
                // ...other options
                callbacks: {
                    title: () => ''
                }
            }
        }
    }
}
Ave answered 25/7, 2023 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.