Sphere
Log in Join
☆ 5

chart.js time series x-axis shows wrong ticks

Chart.js 4.4.1, React wrapper (react-chartjs-2 5.2). Time series chart with data points over a 6-hour window. The x-axis (type: 'time') shows weird tick labels - sometimes just hours without context, sometimes duplicate dates, and the spacing is uneven. I registered the adapters (chartjs-adapter-date-fns) and set parsing: false with data as {x: timestamp, y: value}. My timestamps are unix seconds (e.g. 1700000000). Is Chart.js expecting milliseconds? Also ticks are too dense - how do I control the time unit shown (hours vs days) based on zoom?

⚑ Report

4 replies

☆ 0
T tabz ·2026-11-11#1

Chart.js time scale expects milliseconds since epoch, not seconds - your 1700000000 is being interpreted as milliseconds, which is 1970, so the ticks are wrong. Multiply by 1000 when building your data (there's no built-in auto-convert). Once timestamps are right, control ticks with scales: { x: { type: 'time', time: { unit: 'hour', displayFormats: { hour: 'HH:mm' } }, ticks: { maxTicksLimit: 8 } } }. For adaptive units, set time.unit dynamically based on the visible range.

⚑ Report
☆ 0
4 404 ·2026-11-13#2

Milliseconds - that explains it. I multiplied timestamps by 1000 and the axis is correct now. For adaptive units (show days when zoomed out, hours when zoomed in), is there a built-in way or do I set unit dynamically based on the visible range? Also the tooltip still shows the raw number, not a formatted date.

⚑ Report
☆ 0
E eventual ·2026-11-15#3

You can compute the range from chart.scales.x.min/max in an onZoomComplete or onHover callback and set chart.options.scales.x.time.unit accordingly, then chart.update(). Many people just let the adapter auto-pick the unit by leaving unit unset - with date-fns it often chooses sensible ticks based on range automatically. Remove the explicit unit and see. For tooltips, use plugins.tooltip.callbacks.title to format with date-fns format().

⚑ Report
☆ 0
R red ·2026-11-16#4

Removing the explicit time.unit let the adapter pick appropriate ticks automatically - days when zoomed out, minutes when zoomed in. That was simpler than a callback. Added a tooltip title callback with date-fns format for readable dates. The whole problem was seconds vs milliseconds; everything else was wrong because of that. If your Chart.js time axis looks insane, check the magnitude of your timestamps first.

⚑ Report
Log in to reply.