Skip to content

Zoom and Pan

Example of a line chart that zooms and pans, made with the <Line> component and chartjs-plugin-zoom. Install the plugin next to Chart.js:

Terminal window
pnpm add chartjs-plugin-zoom

The plugin does not register itself: the example registers it with ChartJS.register, along with the parts of Chart.js, so every chart gets it. Its options are in options.plugins.zoom: pan moves the chart with a drag of the mouse or a finger, zoom zooms it with the wheel and a pinch, and limits with 'original' keeps both within the data. Both work only along the x axis here, with mode: 'x': the plugin then leaves vertical swipes to the page, so a phone can still scroll past the chart. The wheel zooms only with Ctrl held, set by modifierKey: 'ctrl', and scrolls the page otherwise. See options in the plugin documentation.

The plugin adds methods to the Chart.js instance, such as resetZoom, zoom and getZoomLevel. The ref of the chart gets the instance, so the button calls chartRef.current?.resetZoom(). Importing the plugin also adds its types: TypeScript knows options.plugins.zoom and the new methods.

Versions of the plugin before 3 load Hammer.js, which reads window as soon as it is imported, so they fail on the server, for example in Next.js, with ReferenceError: window is not defined. Version 3 handles gestures with Pointer Events, without Hammer.js, and imports on the server.

App.tsx
import { useRef } from 'react'
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip
} from 'chart.js'
import zoomPlugin from 'chartjs-plugin-zoom'
import { Line } from 'react-chartjs-2'
import { faker } from '@faker-js/faker'
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
zoomPlugin
)
export const options = {
responsive: true,
plugins: {
title: {
display: true,
text: 'Chart.js Line Chart with Zoom and Pan'
},
zoom: {
// Only along the x axis: a vertical swipe on a phone still scrolls the page
pan: {
enabled: true,
mode: 'x' as const
},
zoom: {
wheel: {
enabled: true,
// The wheel scrolls the page, and Ctrl + wheel zooms the chart
modifierKey: 'ctrl' as const
},
pinch: {
enabled: true
},
mode: 'x' as const
},
limits: {
// No panning or zooming out past the data
x: {
min: 'original' as const,
max: 'original' as const
}
}
}
}
}
const labels = Array.from({
length: 100
}, (_, index) => `Day ${index + 1}`)
export const data = {
labels,
datasets: [
{
label: 'Dataset 1',
// A wave with noise
data: labels.map((_, index) => Math.round(Math.sin(index / 10) * 500) + faker.number.int({
min: -100,
max: 100
})),
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.5)'
}
]
}
export function App() {
const chartRef = useRef<ChartJS<'line'>>(null)
return (
<div
style={{
width: '100%',
fontFamily: 'sans-serif',
fontSize: 14,
textAlign: 'center'
}}
>
<Line
ref={chartRef}
options={options}
data={data}
/>
<p>
Drag to pan, zoom with Ctrl + wheel or a pinch.
{' '}
<button
type='button'
onClick={() => chartRef.current?.resetZoom()}
>
Reset zoom
</button>
</p>
</div>
)
}