Skip to content

Time Scale

Example of a line chart with dates on the x axis, made with the <Line> component and the time scale of Chart.js. A category scale puts its labels at equal distances, while the time scale places each point at its date: the gaps between the dates stay on the chart, and the scale picks the ticks by itself.

Chart.js does not work with dates by itself: the time scale needs a date library and an adapter for it. The example uses date-fns with chartjs-adapter-date-fns:

Terminal window
pnpm add chartjs-adapter-date-fns date-fns

Importing chartjs-adapter-date-fns once sets the adapter for all charts. Without the adapter, the chart throws this error:

Uncaught Error: This method is not implemented: Check that a complete date adapter is provided.

The scale itself is TimeScale, which the example registers with the other parts of the chart. Without it, Chart.js throws the "time" is not a registered scale error.

The x of each point is a date: an ISO string such as '2025-01-03', as in the example, a Date or a timestamp. Dates in another format need time.parser with a date-fns format, such as 'dd.MM.yyyy'. The same formats set how the scale shows dates: time.displayFormats for the ticks of each unit and time.tooltipFormat for the tooltip. time.unit: 'week' puts a tick at the start of each week, and a date-fns locale in the adapters.date.locale option of the scale translates the dates.

The timeseries scale, TimeSeriesScale, takes the same options but spaces the points equally, as a category scale does. See time cartesian axis in the Chart.js documentation.

App.tsx
import {
Chart as ChartJS,
TimeScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
} from 'chart.js'
import 'chartjs-adapter-date-fns'
import { Line } from 'react-chartjs-2'
import { faker } from '@faker-js/faker'
ChartJS.register(
TimeScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
)
export const options = {
responsive: true,
plugins: {
legend: {
position: 'top' as const
},
title: {
display: true,
text: 'Chart.js Line Chart with a Time Scale'
}
},
scales: {
x: {
type: 'time' as const,
time: {
unit: 'week' as const,
// date-fns formats: Jan 5 on the axis, Jan 3, 2025 in the tooltip
displayFormats: {
week: 'MMM d'
},
tooltipFormat: 'PP'
}
}
}
}
// The time scale keeps the gaps between the dates
const dates = [
'2025-01-03',
'2025-01-06',
'2025-01-07',
'2025-01-15',
'2025-01-20',
'2025-01-21',
'2025-02-03',
'2025-02-10',
'2025-02-24',
'2025-02-28'
]
export const data = {
datasets: [
{
label: 'Dataset 1',
data: dates.map(date => ({
x: date,
y: faker.number.int({
min: -1000,
max: 1000
})
})),
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.5)'
},
{
label: 'Dataset 2',
data: dates.map(date => ({
x: date,
y: faker.number.int({
min: -1000,
max: 1000
})
})),
borderColor: 'rgb(53, 162, 235)',
backgroundColor: 'rgba(53, 162, 235, 0.5)'
}
]
}
export function App() {
return (
<Line
options={options}
data={data}
/>
)
}