【发布时间】:2023-03-19 09:00:01
【问题描述】:
我正在学习如何使用 chart.js,我想要一个显示一天中不同时间(x 轴)的随机值的图表,格式为“h?h:mm”。
在 x 轴上,我想要一小时的固定步长,从上午 12 点 (0:00) 开始,到上午 8 点 (8:00) 结束。
而数据,例如 (x = 4:45, y = 67) 在 te 对应的 x 刻度(4:00 和 5:00 之间的 3/4)
我尝试了以下方法:
var cfg = {
type: 'line',
data: {
labels: ['00:00', '01:35', '02:20', '03:05', '04:07', '04:57', '05:25', '06:08', '07:10'],
datasets: [{
data: [
randomScalingFactor(18, 45),
randomScalingFactor(18, 45),
randomScalingFactor(18, 45),
randomScalingFactor(18, 45),
randomScalingFactor(18, 45),
randomScalingFactor(18, 45),
randomScalingFactor(18, 45),
randomScalingFactor(18, 45),
randomScalingFactor(18, 45)
],
label: "Improved ETA",
borderColor: "#e67e22",
borderWidth: 2,
fill: false,
lineTension: 0
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Improved ETA',
fontSize: 14,
fontFamily: 'Arial',
fontColor: '#000'
},
legend: {
display: false
},
scales: {
xAxes: [{
ticks: {
padding: 12
},
gridLines: {
color: 'rgba(0, 0, 0, 0.06)',
zeroLineColor: 'rgba(0, 0, 0, 0.06)',
drawTicks: false
}
}],
yAxes: [{
ticks: {
suggestedMin: 0,
suggestedMax: 80,
padding: 12
},
scaleLabel: {
display: true,
labelString: 'mins'
},
gridLines: {
color: 'rgba(0, 0, 0, 0.06)',
zeroLineColor: 'rgba(0, 0, 0, 0.06)',
drawTicks: false
}
}]
}
}
};
window.onload = function() {
var ctx = document.getElementById('foo').getContext('2d');
window.myLine = new Chart(ctx, cfg);
};
<div style="width: 600px; height: 400px;">
<canvas id="foo"></canvas>
</div>
但这不是我想要的,因为 X 轴刻度采用标签的值(x 数据)。更糟糕的是,刻度之间的间隔始终相同(例如,从 10:30 到 10:40 与 8:00 到 9:00 之间的间隔相同)。
PD:由于时间图,我还阅读了有关使用 moment.js chart.js bundle 的信息:https://www.chartjs.org/samples/latest/scales/time/line.html
【问题讨论】:
标签: javascript charts chart.js