Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chartjs display label & units when mouse is hover stats

Is it possible to have the label & units when my mouse pointer is hover the chart ? For now there is only the number.

For the example bellow I would like to show :

  • 58% Label1
  • 0% Label2
  • 0% Label3
  • 0% Label4
  • 0% Label5

enter image description here

My options looks like this :

var options = {
      //Boolean - Show a backdrop to the scale label
      scaleShowLabelBackdrop : true,
      //Boolean - Whether to show labels on the scale
      scaleShowLabels : true,
      // Boolean - Whether the scale should begin at zero
      scaleBeginAtZero : true,
      scaleLabel : "<%%= Number(value) + ' %'%>",
      legendTemplate: "<ul class=\"<%%=name.toLowerCase()%>-legend\"><%% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%%=datasets[i].strokeColor%>\"></span><%%if(datasets[i].label){%><%%=datasets[i].label%> <strong><%%=datasets[i].value%></strong><%%}%></li><%%}%></ul>",
      tooltipTemplate: "<%%= value %> Label"
    }

With scaleLabel option I have the % shown on the Y-axis but not on the hover pop-up...

like image 650
ZazOufUmI Avatar asked Dec 01 '22 00:12

ZazOufUmI


2 Answers

I found the solution on the ChartJS repository on Github.

The solution is to use the option multiTooltipTemplate if your graph has multiple data. Otherwise, you should use tooltipTemplate

multiTooltipTemplate: "<%=datasetLabel%> : <%= value %>"  // Regular use
// or
multiTooltipTemplate: "<%%=datasetLabel%> : <%%= value %>"  // Ruby on Rails use

Will give you :

  • Dataset_label : value
like image 162
ZazOufUmI Avatar answered Dec 08 '22 00:12

ZazOufUmI


Try this one, it will work. You just need to check what's the data coming in the label function.

options: {
  tooltips: {
    callbacks: {
      title: function() {
        return "";
      },
      label: function(item, data) {
        var datasetLabel = data.datasets[item.datasetIndex].label || "";
        var dataPoint = item.yLabel;
        return datasetLabel + ": " + dataPoint + "min";
      }
    }
  }
}
like image 43
rohit kumbhar Avatar answered Dec 08 '22 00:12

rohit kumbhar