简体   繁体   English

当鼠标悬停统计时,Chartjs会显示标签和单位

[英]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 58%标签1
  • 0% Label2 0%Label2
  • 0% Label3 0%Label3
  • 0% Label4 0%Label4
  • 0% Label5 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... 使用scaleLabel选项,我在Y轴上显示%,但在悬停弹出窗口中没有显示...

I found the solution on the ChartJS repository on Github . 在Github上的ChartJS存储库中找到了解决方案。

The solution is to use the option multiTooltipTemplate if your graph has multiple data . 如果图表有多个数据 ,解决方法是使用multiTooltipTemplate选项。 Otherwise, you should use tooltipTemplate 否则,您应该使用tooltipTemplate

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

Will give you : 会给你 :

  • Dataset_label : value Dataset_label:值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM