admin管理员组文章数量:1332872
I have looked the documentation and similar questions here but I don't seem to find a working solution to my problem.
I'm using Chart.js v.2.1.6, and I have a Bar Chart with percentage values stored as numbers (already multiplied by 100). I need both y-axis labels and tooltips to display the %
sign after the values.
Someone can shed some light on that matter?
Here you have my code:
var data = {
"labels": ["Label1", "Label2", "Label3", "Label4", "Label5"],
"datasets": [{
"label": "Variation",
"data": ["56", "-82.3", "25.7", "32.2", "49.99"],
"borderWidth": 1,
"backgroundColor": "rgba(231, 76, 60, 0.2)",
"borderColor": "rgba(231, 76, 60, 1)"
}]
};
var myBarChart = new Chart($("#myCanvas"), {
type: 'bar',
data: data,
maintainAspectRatio: false
});
And a fiddle: /
EDIT: SOLVED
I found the solution thanks to miquelarranz, find the updated fiddle:
/
I have looked the documentation and similar questions here but I don't seem to find a working solution to my problem.
I'm using Chart.js v.2.1.6, and I have a Bar Chart with percentage values stored as numbers (already multiplied by 100). I need both y-axis labels and tooltips to display the %
sign after the values.
Someone can shed some light on that matter?
Here you have my code:
var data = {
"labels": ["Label1", "Label2", "Label3", "Label4", "Label5"],
"datasets": [{
"label": "Variation",
"data": ["56", "-82.3", "25.7", "32.2", "49.99"],
"borderWidth": 1,
"backgroundColor": "rgba(231, 76, 60, 0.2)",
"borderColor": "rgba(231, 76, 60, 1)"
}]
};
var myBarChart = new Chart($("#myCanvas"), {
type: 'bar',
data: data,
maintainAspectRatio: false
});
And a fiddle: https://jsfiddle/tdjk3ncs/
EDIT: SOLVED
I found the solution thanks to miquelarranz, find the updated fiddle:
https://jsfiddle/tdjk3ncs/7/
Share Improve this question edited Jul 7, 2016 at 10:52 Mumpo asked Jul 4, 2016 at 8:50 MumpoMumpo 5467 silver badges16 bronze badges 2- Have a look at here stackoverflow./questions/28568773/… – Santhosh Commented Jul 4, 2016 at 9:04
-
@SanKrish I did, I included
tooltipTemplate
andmultiTooltipTemplate
options as they mention but I didn't seem to work in my case... Maybe that only works with Line Chart, and not Bar Chart – Mumpo Commented Jul 4, 2016 at 9:19
1 Answer
Reset to default 6If you want to add %
after the values of the Y-Axis you can do it using scales in your chart configuration. Your code will look like this:
var myBarChart = new Chart($("#myCanvas"), {
type: 'bar',
data: data,
maintainAspectRatio: false,
options: {
scales: {
yAxes: [{
ticks: {
// Create scientific notation labels
callback: function(value, index, values) {
return value + ' %';
}
}
}]
}
}
});
Documentation about scales
Fiddle updated with the %
: Fiddle
And if you want to modify the text displayed in the tooltips you can easily change it using callback. You can find more information here Tooltip Callbacks
本文标签: javascriptFormat Bar Chart39s yAxis labels in ChartjsStack Overflow
版权声明:本文标题:javascript - Format Bar Chart's yAxis labels in Chart.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742309826a2450643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论