Arun Kumaresh
Arun Kumaresh

Reputation: 6311

How do you hide the title of a chart tooltip?

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

Upvotes: 18

Views: 20146

Answers (3)

Unmitigated
Unmitigated

Reputation: 89139

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: () => ''
                }
            }
        }
    }
}

Upvotes: 4

Divyesh Parmar
Divyesh Parmar

Reputation: 391

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://stackoverflow.com/a/68033933/8578337

Upvotes: 11

ɢʀᴜɴᴛ
ɢʀᴜɴᴛ

Reputation: 32859

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() {}
      }
   },
   ...
}

Upvotes: 39

Related Questions