I'm learning how to use chart.js and I want a graph that displays random values in different hours of the day (x axis) with format "h?h:mm".
In the x axis I want a fixed step of one hour, starting at 12 AM (0:00) and ending at 8 AM (8:00).
And the data, for example (x = 4:45, y = 67) in te corresponding x tick (3/4 between 4:00 and 5:00)
I tried the following:
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>
But is not what I want, because the X axis ticks take the values of the labels (x data). And what is even worse the space between ticks is always the same (e.g from 10:30 to 10:40 there is the same space as 8:00 to 9:00).
PD: I also have read about using moment.js chart.js bundle because of the time graph: https://www.chartjs.org/samples/latest/scales/time/line.html