[英]ChartJS bar not showing up for simple data points
Here's a simple chart.js horizontal bar chart I'm using:这是我正在使用的一个简单的 chart.js 水平条形图:
var myBarChart = new Chart(ctx,{
type: 'horizontalBar',
data: {
labels: [
"Foo",
"Bar",
"Baz"
],
datasets: [
{
data: [725000, 600000, 900000],
backgroundColor: [
"#ccf6ec",
"#ff6654",
"#009784"
],
hoverBackgroundColor: [
"#ccf6ec",
"#ff6654",
"#009784"
]
}]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: false
}],
yAxes: [{
display: false
}],
}
}
});
If I change the values to be closer to each other I see the three bars.如果我将值更改为彼此更接近,我会看到三个条形。 I need it to draw the bars so they are always visible, even if the values are widely different.
我需要它来绘制条形,以便它们始终可见,即使值相差很大。
Is there a configuration I missed?有没有我错过的配置? How can I fix this?
我怎样才能解决这个问题?
For some reason, chart.js uses the smallest amount in data as minimum ticks value.出于某种原因,chart.js 使用数据中的最小量作为最小刻度值。 Simply adding
ticks.min: 0
apparently solves the issue:只需添加
ticks.min: 0
显然可以解决问题:
var ctx = document.getElementById('chart') var myBarChart = new Chart(ctx, { type: 'horizontalBar', data: { labels: [ "Foo", "Bar", "Baz" ], datasets: [{ data: [725000, 600000, 900000], backgroundColor: [ "#ccf6ec", "#ff6654", "#009784" ], hoverBackgroundColor: [ "#ccf6ec", "#ff6654", "#009784" ] }] }, options: { legend: { display: false }, scales: { xAxes: [{ display: false, ticks: { min: 0 } }], yAxes: [{ display: false }], } } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> <canvas id="chart" width="400" height="400"></canvas>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.