gpt4 book ai didi

javascript - Chart.js:水平堆积条形图 x 轴的最小值

转载 作者:行者123 更新时间:2023-11-29 15:20:49 24 4
gpt4 key购买 nike

我正在使用 Chart.js 生成水平堆积条形图。该图表目前看起来像这样: enter image description here

此图表向用户显示了他们应该在多少年后修复房屋的特定组件。我正在尝试将其更改为用户应该在哪一年进行恢复。将当前年份添加到值结果如下: enter image description here

如果我可以将 x 轴的起始值设置为当前年份,这几乎就是我所需要的。我试图像这样设置最小值:

options: {
scales: {
xAxes: [{
ticks: {
min: 2017
},
...

不幸的是,结果根本没有像这样显示数据集:

enter image description here

我尝试了添加当前年份和设置最小值的所有组合,但没有产生有用的图表。

在下面你可以看到我当前的源代码:

var mainChart_ctx = document.getElementById("main_chart").getContext("2d");

var mainChart_config = {
type: 'horizontalBar',
data: {
labels: ['Kellerdecke', 'Fenster', 'Außenwand', 'Erdberührter Boden', 'Dach'],
datasets: [{
label: 'Beginn ab heute',
backgroundColor: 'transparent',
data: [4, 21, 25, 25, 25],
borderColor: '#666',
borderWidth: 1
},
{
label: 'Sanierungsdauer',
backgroundColor: '#ffcc00',
data: [2, 5, 5, 5, 5],
borderColor: '#666',
borderWidth: 1
},
{
label: 'Mittlere Restlebensdauer',
backgroundColor: 'orange',
data: [39, 0, 38, 51, 37],
borderColor: '#666',
borderWidth: 1
},
{
label: 'Maximale Restlebensdauer',
backgroundColor: 'orangered',
data: [20, 0, 0, 0, 0],
borderColor: '#666',
borderWidth: 1
}
]
},
options: {
tooltips: {
enabled: true
},
legend: {
display: false
},
title: {
display: true,
text: 'Sanierungsfahrplan',
fontSize: 24
},
scales: {
xAxes: [{
ticks: {
min: 0 /* Todo: change to current year? */
},
stacked: true,
scaleLabel: {
display: true,
labelString: 'Jahre',
fontSize: 16
}
}],
yAxes: [{
ticks: {
stepSize: 10
},
stacked: false,
scaleLabel: {
display: true,
labelString: 'Bauteil',
fontSize: 16
},
}]
}
}
};

mainChart = new Chart(mainChart_ctx, mainChart_config)
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"></script>

<canvas id="main_chart"></canvas>

最佳答案

我能够使用这样的回调获得合适的结果:

xAxes: [{
ticks: {
min: 0,
callback: function (value, index, values) {
return value + 2017;
}
},
...
]

关于javascript - Chart.js:水平堆积条形图 x 轴的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43893700/

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