How do you hide the title of a chart tooltip?

时间:2017-06-19 13:55:11

标签: javascript jquery chart.js

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

2 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:0)

要隐藏工具提示标题/标签,应将其添加到该图表的选项对象中,如下所示:

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

请参阅文档以更好地理解它应该使用自定义回调函数处理,并且它不是可以直接在选项中设置的配置。 https://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip-callbacks

我在另一个帖子中提到了同样的内容:https://stackoverflow.com/a/68033933/8578337