I want to hide the label in a tooltip because it shows undefined
Asked Answered
P

5

2

I am using chart.js to show a line chart. How can I hide a tooltip label for a chart.js line chart? The label in the tooltip is showing undefined so I want to hide the label (please see the screenshot)?

Perhaps there is a way to modify tooltip where I can show only the legends value in tooltip? My code is as follows:

  myLine = new Chart(ctx).Line(lineChartData, {
      type: 'line',
      responsive: true,
      scaleShowGridLines : false,
      bezierCurve : false,
      animationEasing: "linear",
      tooltipEvents: ["mousemove", "touchstart", "touchmove"],
      showTooltips: true,
      scaleLineColor: "rgba(0,0,0,.8)",
  });

enter image description here

Pendulous answered 16/2, 2016 at 11:46 Comment(1)
I want to hide the label only in tootlip is there any way.Pendulous
C
6

Just set the tooltipTitleFontSize to 0 in your options.


Preview

enter image description here


Script

myLine = new Chart(ctx).Line(lineChartData, {
    ...
    tooltipTitleFontSize: 0
});
Cy answered 16/2, 2016 at 17:42 Comment(1)
It doesn't work, I am using v2.7.1, is there any other way to do so ?Thorite
D
1

I know I am late, but I guess it still has merit to add this one.

check this : https://mcmap.net/q/650038/-how-do-you-hide-the-title-of-a-chart-tooltip

It uses a function to hide the title:

options:{
   tooltips:{
     callbacks:{
        title: ()=>{}
     }
   }
}
Danelledanete answered 16/9, 2019 at 18:25 Comment(0)
G
1

Setting the title's font size to zero is not ideal as you will still see an (ugly) extra space on top of the tooltip, as if the title line is still there - which honestly is what you'd expect.

Instead, I used Yumin Gui's answer but had to return null for it to work: `

tooltips: {
  callbacks: {
    title: () => null,
  },
},

The result is exactly as in the pie charts (which does not have a title in its default tooltips).

Glottal answered 4/6, 2020 at 8:44 Comment(1)
Thanks! This is the only solution that worked, it's important that something is returned even null and therefore this is the correct answer.Washerman
C
1

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.

Cryoscopy answered 18/6, 2021 at 10:58 Comment(0)
S
0

The accepted answer won't work in newer versions of chart.js, as Aman said in comments, what you can use now is this:

tooltips: { titleFontSize: 0 }

Example:

var bar_chart = document.getElementById('bar_canvas').getContext('2d');
window.myBar = new Chart(bar_chart, {
    type: 'bar',
    data: bar_chart_data,
    options: {
        tooltips: {
            titleFontSize: 0,
            bodyFontSize: 14,
        }
    }
});
Shanon answered 5/9, 2019 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.