Chart.js V2:为工具提示标签添加前缀或后缀前缀、后缀、提示、标签

由网友(大胆的往前走)分享简介:在 Chart.js V1.0 中,我会添加 tooltipTemplate: ": " 添加欧元符号作为工具提示标签的前缀.但是,这在 V2 中不再有效.有谁知道做到这一点的新方法?好像没找到.In Chart.js...

在 Chart.js V1.0 中,我会添加 tooltipTemplate: "<%if (label){%><%=label %>: <%}%><%= '€' + value %>" 添加欧元符号作为工具提示标签的前缀.但是,这在 V2 中不再有效.有谁知道做到这一点的新方法?好像没找到.

In Chart.js V1.0, I would add tooltipTemplate: "<%if (label){%><%=label %>: <%}%><%= '€' + value %>" to add a euro symbol as prefix to the tooltip label. However, this no longer works in V2. Does anybody know the new way to do accomplish this? I can't seem to find it.

非常感谢!

推荐答案

在 V2.0 中,不推荐使用 tooltipTemplate 选项.相反,您可以使用回调来修改显示的工具提示.这里有一个使用回调的示例 您可以在 文档中找到可能的回调在 Chart.defaults.global.tooltips

In the V2.0 the tooltipTemplate option is deprecated. Instead you can use callbacks to modify the displayed tooltips. There is a sample for the usage of callbacks here and you can find the possible callbacks in the documentation under Chart.defaults.global.tooltips

在你的情况下,我会做以下事情:

In your case I would do the following:

window.myLine = new Chart(chart, {
    type: 'line',
    data: lineChartData,
    options: {
            tooltips: {
                enabled: true,
                mode: 'single',
                callbacks: {
                    label: function(tooltipItems, data) { 
                        return tooltipItems.yLabel + ' €';
                    }
                }
            },
     }       
  });

别忘了设置 HTML 元标记:

Don't forget to set the HTML meta tag:

<meta charset="UTF-8">
阅读全文

相关推荐

最新文章