Chartjs display label & units when mouse is hover stats

我只是一个虾纸丫 提交于 2020-01-14 14:21:14

问题


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

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...


回答1:


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



回答2:


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";
      }
    }
  }
}


来源:https://stackoverflow.com/questions/30334965/chartjs-display-label-units-when-mouse-is-hover-stats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!