Display time elapsed since first data point #10230
-
Is there an easy way to display ticks' labels showing the time elapsed from the first data point instead of just formatting the tick date and time? Basically I want to show 0:00 on the first data point and then one tick every minute like: 1:00, 2:00… |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
@Slion you should leverage on |
Beta Was this translation helpful? Give feedback.
-
Actually I could not get it to work with date scale. I had to change it to linear scale and have something like: scales: {
x: {
type: 'linear',
title: {
display: false,
text: 'Date'
},
ticks: {
autoSkip: true,
// We don't want to rotate labels
maxRotation: 0,
minRotation: 0,
// We want one tick every minute, which is 60000 milliseconds
stepSize: 60000,
// See: https://codepen.io/stockinail/pen/WNXVjqg
callback: function (value, index, ticks) {
if (index > 0) {
const first = ticks[0].value;
const date = ticks[index].value;
// in minutes
const diff = (date - first) / 60000;
return Math.round(diff) + ':00';
}
return '0:00';
}
}
}
} With date scale you can't start with 0:00 as your axis origin unless your data set starts exactly at the minute I guess. |
Beta Was this translation helpful? Give feedback.
@Slion you should leverage on
ticks.callback
on the scale. https://www.chartjs.org/docs/latest/axes/labelling.html#creating-custom-tick-formatsCodepen: https://codepen.io/stockinail/pen/WNXVjqg