admin 管理员组文章数量: 1086019
I want to align the y-axis tick labels above the ticks in chartJS.
Here's what I've got so far:
I want the tick labels 0M
, 20M
, and 40M
above the tick labels. Something like this:
Here's my chart config:
new Chart(ctx, {
type: 'bar',
data,
options: {
...
scales: {
xAxes: [{ ... }],
yAxes: [{
stacked: true,
position: 'right',
gridLines: {
drawBorder: false
},
ticks: {
maxTicksLimit: 3
}
}]
}
}
})
I'm not able to find any option in the chart options to do such a thing. Please help
I want to align the y-axis tick labels above the ticks in chartJS.
Here's what I've got so far:
I want the tick labels 0M
, 20M
, and 40M
above the tick labels. Something like this:
Here's my chart config:
new Chart(ctx, {
type: 'bar',
data,
options: {
...
scales: {
xAxes: [{ ... }],
yAxes: [{
stacked: true,
position: 'right',
gridLines: {
drawBorder: false
},
ticks: {
maxTicksLimit: 3
}
}]
}
}
})
I'm not able to find any option in the chart options to do such a thing. Please help
Share Improve this question edited May 19, 2017 at 5:47 Anubhav Dhawan asked Apr 28, 2017 at 9:49 Anubhav DhawanAnubhav Dhawan 1,4876 gold badges19 silver badges37 bronze badges 4-
try to create a new scale type and override
getPixelForTick
method – Rhea Commented Apr 30, 2017 at 16:47 - @Rhea sorry I'm new to ChartJS. Can you post some code? – Anubhav Dhawan Commented May 1, 2017 at 6:13
- Can you add the code or a JSFiddle with the code you are currently using to create the chart? – Tot Zam Commented May 18, 2017 at 3:42
- 1 @TotZam OK, I've added the relevant code for my chart's config – Anubhav Dhawan Commented May 19, 2017 at 5:50
2 Answers
Reset to default 4You can do this by adding an animation
to the options
config:
options: {
animation: {
duration: 1,
onComplete: function() {
var controller = this.chart.controller;
var chart = controller.chart;
var yAxis = controller.scales['y-axis-0'];
yAxis.ticks.forEach(function(value, index) {
var xOffset = chart.width - 40;
var yOffset = ((chart.height - 60) / 2 * index) + 15;
ctx.fillText(value + 'M', xOffset, yOffset);
});
}
}
}
JSFiddle Demo: https://jsfiddle/m5tnkr4n/1/
You may need to adjust the hardcoded numbers in the xOffset
and yOffset
. These numbers will depend on the height and width of your chart, as well as what features you are displaying the in the canvas area. For example, if you remove the xAxes
tick labels, you many need to decrease the -60
since that will make the chart slightly shorter. / 2
works since you set the maxTicksLimit
to 3.
You can adjust the tick position with mirror
, labelOffset
and padding
scales: {
yAxesA: {
id: 'yAxisA',
type: 'linear',
position: 'left',
ticks: {
display: true,
mirror: true,
labelOffset: -5,
padding: 5
}
}
}
本文标签: javascriptHow to position yAxes labels in chartJSStack Overflow
版权声明:本文标题:javascript - How to position yAxes labels in chartJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744044516a2523870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论