简体   繁体   English

Chartjs在标签中以不同单位显示多个信息

[英]Chartjs display multiple info with different units in label

I have known that how to show multiple info. 我知道如何显示多个信息。 with same unit in the label by 标签中具有相同单位的
multiTooltipTemplate: "<%=datasetLabel%> : <%= value %>%" multiTooltipTemplate:“ <%= datasetLabel%>:<%=值%>%”
and the result as following: 结果如下:
[]InfoA:50% [] InfoA:50%
[]InfoB:50% [] InfoB:50%

But I'm now try to show infos with different units such as: 但是我现在尝试显示具有不同单位的信息,例如:
[]InfoA:50% [] InfoA:50%
[]InfoB:50$ [] InfoB:50 $

How can I do it? 我该怎么做?

Changing Tooltip Template By Label 按标签更改工具提示模板

You can use the label property of the valuesObject to figure out the index and use that to pick your symbol, like so 您可以使用valuesObject的label属性来找出索引并使用它来选择符号,如下所示

Chart.mySymbols = ['!', '@', '#', '$', '%', '^', '&'];

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx).Line(window.data, {
    multiTooltipTemplate: "<%=datasetLabel%> : <%= value %><%= Chart.mySymbols[window.data.labels.indexOf(label)] %>"
});

Notice that both data and mySymbols collection are properties of globally referencible objects ( window and Chart in this case, but it could be any global object). 请注意, datamySymbols集合都是全局可引用对象的属性(在这种情况下为windowChart ,但可以是任何全局对象)。 This is because only the valuesObject is injected into the template function. 这是因为只有valuesObject被注入到template函数中。 Unless you want to change the library code, using global objects would be the way to do it (however, do note that its not good design). 除非您想更改库代码,否则使用全局对象将是这样做的方法(但是,请注意,这不是一个好的设计)。


Fiddle - http://jsfiddle.net/z1nfqhfn/ 小提琴-http: //jsfiddle.net/z1nfqhfn/


Changing Tooltip Template By DataSet 通过数据集更改工具提示模板

If you want to do a similar thing by dataset, it would be 如果您想按数据集做类似的事情,那将是

Chart.mySymbols = ['°C', '%'];

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx).Line(window.data, {
    multiTooltipTemplate: "<%=datasetLabel%> : <%= value %><%= Chart.mySymbols[window.data.datasets.map(function(e) { return e.label }).indexOf(datasetLabel)] %>"
});

Fiddle - http://jsfiddle.net/fnu0dyd2/ 小提琴-http: //jsfiddle.net/fnu0dyd2/

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

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