Chart.js V2:在工具提示标签上添加前缀或后缀 [英] Chart.js V2: Add prefix or suffix to tooltip label
问题描述
在Chart.js V1.0中,我将添加 tooltipTemplate:<%if(label){%><%= label%> ;:<%}%> <%='€'+值%> 将欧元符号添加为工具提示标签的前缀。但是,这在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屋!