Skip to content

Options ​

The options for chartjs-plugin-zoom should be placed in options.plugins.zoom in chart.js configuration.

The options are split in three sub-objects, limits, pan and zoom.

js
export const chart = new Chart('id', {
  type: 'bar',
  data: {},
  options: {
    plugins: {
      zoom: {
        pan: {
          // pan options and/or events
        },
        limits: {
          // axis limits
        },
        zoom: {
          // zoom options and/or events
        }
      }
    }
  }
})

Pan ​

Pan Options ​

NameTypeDefaultDescription
enabledbooleanfalseEnable panning
mode'x'|'y'|'xy''xy'Allowed panning directions
modifierKey'ctrl'|'alt'|'shift'|'meta'nullModifier key required for panning with mouse
scaleMode'x'|'y'|'xy'undefinedEnable panning over a scale for that axis (regardless of mode)
overScaleMode'x'|'y'|'xy'undefinedEnable panning over a scale for that axis (but only if mode is also enabled), and disables panning along that axis otherwise. Deprecated.
thresholdnumber10Minimal pan distance required before actually applying pan

Pan Events ​

NameArgumentsDescription
onPan{chart}Called while the chart is being panned
onPanComplete{chart}Called once panning is completed
onPanRejected{chart,event}Called when panning is rejected due to missing modifier key. event is the normalized gesture event that failed
onPanStart{chart,event,point}Called when panning is about to start. If this callback returns false, panning is aborted and onPanRejected is invoked

Zoom ​

Zoom Options ​

NameTypeDefaultDescription
wheelWheelOptionsOptions of the mouse wheel behavior
dragDragOptionsOptions of the drag-to-zoom behavior
pinchPinchOptionsOptions of the pinch behavior
mode'x'|'y'|'xy''xy'Allowed zoom directions
scaleMode'x'|'y'|'xy'undefinedWhich of the enabled zooming directions should only be available when the mouse cursor is over a scale for that axis
overScaleMode'x'|'y'|'xy'undefinedAllowed zoom directions when the mouse cursor is over a scale for that axis (but only if mode is also enabled), and disables zooming along that axis otherwise. Deprecated; use scaleMode instead.

Wheel options ​

NameTypeDefaultDescription
enabledbooleanfalseEnable zooming via mouse wheel
speednumber0.1Factor of zoom speed applied per notch of a mouse wheel
modifierKey'ctrl'|'alt'|'shift'|'meta'nullModifier key required for zooming via mouse wheel

The first wheel event zooms at once. The events that follow it before the next animation frame are applied together in that frame, around the position of the latest one, so a stream of events updates the chart, and calls onZoom, once per frame.

Drag options ​

NameTypeDefaultDescription
enabledbooleanfalseEnable drag-to-zoom
backgroundColorColor'rgba(225,225,225,0.3)'Fill color
borderColorColor'rgba(225,225,225)'Stroke color
borderWidthnumber0Stroke width
drawTimestringbeforeDatasetsDrawWhen the dragging box is drawn on the chart
thresholdnumber0Minimal zoom distance required before actually applying zoom
modifierKey'ctrl'|'alt'|'shift'|'meta'nullModifier key required for drag-to-zoom
maintainAspectRatiobooleanundefinedMaintain aspect ratio of the chart

Draw Time ​

The drawTime option for zooming determines where in the chart lifecycle the drag box drawing occurs. Four potential options are available:

OptionNotes
'beforeDraw'Occurs before any drawing takes place
'beforeDatasetsDraw'Occurs after drawing of axes, but before datasets
'afterDatasetsDraw'Occurs after drawing of datasets but before items such as the tooltip
'afterDraw'After other drawing is completed

Pinch options ​

NameTypeDefaultDescription
enabledbooleanfalseEnable zooming via pinch

Zoom Events ​

NameArgumentsDescription
onZoom{chart}Called while the chart is being zoomed
onZoomComplete{chart}Called once zooming is completed
onZoomRejected{chart,event}Called when zoom is rejected due to missing modifier key. event is the native event that failed
onZoomStart{chart,event,point}Called when zooming is about to start. If this callback returns false, zooming is aborted and onZoomRejected is invoked

Touch devices ​

When panning or pinch zooming is enabled, the plugin sets the touch-action style of the canvas so that touch gestures reach the chart instead of scrolling or zooming the page. It claims only the directions those gestures use:

Directions usedtouch-actionOn the chart
x onlypan-yA vertical swipe scrolls the page
y onlypan-xA horizontal swipe scrolls the page
bothnoneThe page does not scroll

The directions used are those of pan.mode and pan.scaleMode when panning is enabled, and of zoom.mode when pinch zooming is enabled. A modifierKey only applies to the mouse, so it does not free a direction for scrolling. The value is recomputed on every chart update.

To keep a page scrollable on a phone when the chart fills most of the screen, limit these to one axis. The browser's own pinch zoom is always turned off over the chart, in all three cases.

Limits ​

Limits options define the limits per axis for pan and zoom.

Limit options ​

NameTypeDescription
xScaleLimitsLimits for x-axis
yScaleLimitsLimits for y-axis

If you're using multiple or custom axes (scales), you can define limits for those, too.

js
export const chart = new Chart('id', {
  type: 'line',
  data: {},
  options: {
    scales: {
      y: {
        min: 20,
        max: 80,
      },
      y2: {
        position: 'right',
        min: -5,
        max: 5
      }
    },
    plugins: {
      zoom: {
        limits: {
          y: {min: 0, max: 100},
          y2: {min: -5, max: 5}
        },
      }
    }
  }
})

Scale Limits ​

NameTypeDescription
min`number'original'`
max`number'original'`
minRangenumberMinimum allowed range (max - min). This defines the max zoom level.

You may use the keyword 'original' in place of a numeric limit to instruct chartjs-plugin-zoom to use the limits the scale would have without any zooming or panning. A bound set in the scale options stays as it is; one the scale derives from the data follows the data, so data that arrives while the chart is zoomed can be reached.

MIT Licensed