gpt4 book ai didi

javascript - 为什么 ChartJS 条形图不渲染特定值的条形图?

转载 作者:行者123 更新时间:2023-12-01 00:51:29 24 4
gpt4 key购买 nike

我使用的是 ChartJS v 2.8.0

我正在使用以下数据,但它没有显示“4.01”的条形:

数据:[9.24、6.28、5.65、6.74、4.01、17.15、4.01]

var options = {
type: 'bar',
data: {
labels:["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
datasets: [
{
label: '# of Votes',
data:[9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 4.01],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
},
}],
xAxes: [{
}]
}
}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas { background-color : #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="200"></canvas>
</body>

当我使用略有不同的数据时,效果完美:

数据:[9.24、6.28、5.65、6.74、4.01、17.15、3]

var options = {
type: 'bar',
data: {
labels:["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
datasets: [
{
label: '# of Votes',
data:[9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 3],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
},
}],
xAxes: [{
}]
}
}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas { background-color : #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="300"></canvas>
</body>

我做错了什么还是有某种错误?如何解决?

最佳答案

您需要在两个图表的 yAxes.ticks 中使用 beginAtZero: true,这将允许您指定比例从 0 开始。

var options = {
type: 'bar',
data: {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
datasets: [{
label: '# of Votes',
data: [9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 3],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false,
beginAtZero: true
},
}],
xAxes: [{}]
}
}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>

<body>
<canvas id="chartJSContainer" width="600" height="200"></canvas>
</body>
var options = {
type: 'bar',
data: {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
datasets: [{
label: '# of Votes',
data: [9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 4.01],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false,
beginAtZero: true
},
}],
xAxes: [{}]
}
}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>

<body>
<canvas id="chartJSContainer" width="600" height="200"></canvas>
</body>

关于javascript - 为什么 ChartJS 条形图不渲染特定值的条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56934940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com