Chart.js V2:为工具提示标签添加前缀或后缀 [英] Chart.js V2: Add prefix or suffix to tooltip label
问题描述
在 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">
这篇关于Chart.js V2:为工具提示标签添加前缀或后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!