对于图表 js 2 中的负值,如何从右侧 0 开始 Y 轴?
How to start Y Axis Line at 0 from right for negative values in chart js 2?
我想知道是否有任何选项可以在图表 js 2.6 中设置,以便在条形图中,对于负值,Y 轴从右到左开始。
目前,负值是这样显示的(fiddle 中的#2 图表):
https://jsfiddle.net/j6nqt4oo/1/
参考代码:
var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: ['A', 'B'],
datasets: [{
data: [-10, -20]
}]
},
options: {
responsive: false,
showDatapoints: true,
scales: {
maintainAspectRatio: true,
responsive: false,
xAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: false
}
}],
yAxes: [{
gridLines: {
display: false
}
}]
},
legend: {
display: false
}
}
但这就是我想要的:(Y 轴网格线在 0 处,A 和 B 在右边)
您需要将 position
属性 设置为 right
for y-axis ticks ,在你的第二张图表中......
...
yAxes: [{
position: 'right',
gridLines: {
display: false
}
}]
...
我想知道是否有任何选项可以在图表 js 2.6 中设置,以便在条形图中,对于负值,Y 轴从右到左开始。
目前,负值是这样显示的(fiddle 中的#2 图表): https://jsfiddle.net/j6nqt4oo/1/
参考代码:
var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: ['A', 'B'],
datasets: [{
data: [-10, -20]
}]
},
options: {
responsive: false,
showDatapoints: true,
scales: {
maintainAspectRatio: true,
responsive: false,
xAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: false
}
}],
yAxes: [{
gridLines: {
display: false
}
}]
},
legend: {
display: false
}
}
但这就是我想要的:(Y 轴网格线在 0 处,A 和 B 在右边)
您需要将 position
属性 设置为 right
for y-axis ticks ,在你的第二张图表中......
...
yAxes: [{
position: 'right',
gridLines: {
display: false
}
}]
...