@foxglove/chartjs-plugin-zoom

中文文档
2.1.0发布于 2025/04/11MITTypeScriptESM + CJS近 30 天下载 2319

Plugin that enables zoom and pan functionality in Chart.js charts.

原始使用文档
文档目录7 节
  1. Installation
  2. Configuration
  3. API
  4. chart.resetZoom()
  5. Documentation
  6. Contributing
  7. License

chartjs-plugin-zoom

A zoom and pan plugin for Chart.js >= 3.0.0-beta.13.

For Chart.js 2.6.0 to 2.9.x support, use version 0.7.7 of this plugin.

Panning can be done via the mouse or with a finger. Zooming is done via the mouse wheel or via a pinch gesture. Hammer.js is used for gesture recognition.

Live Codepen Demo

Installation

Run npm install --save chartjs-plugin-zoom to install with npm.

If including via a <script> tag, make sure to include Hammer.js as well:

<script src="https://cdn.jsdelivr.net/npm/chart.js@next"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@next"></script>

Configuration

To configure the zoom and pan plugin, you can simply add new config options to your chart config.

plugins: {
 zoom: {
  // Container for pan options
  pan: {
   // Boolean to enable panning
   enabled: true,

   // Panning directions. Remove the appropriate direction to disable
   // Eg. 'y' would only allow panning in the y direction
   // A function that is called as the user is panning and returns the
   // available directions can also be used:
   //   mode: function({ chart }) {
   //     return 'xy';
   //   },
   mode: 'xy',

   // Which of the enabled panning directions should only be available
   // when the mouse cursor is over one of scale.
   overScaleMode: 'xy',

   rangeMin: {
    // Format of min pan range depends on scale type
    x: null,
    y: null
   },
   rangeMax: {
    // Format of max pan range depends on scale type
    x: null,
    y: null
   },

   // On category scale, factor of pan velocity
   speed: 20,

   // Minimal pan distance required before actually applying pan
   threshold: 10,

   // Function called while the user is panning
   onPan: function({chart}) { console.log(`I'm panning!!!`); },
   // Function called once panning is completed
   onPanComplete: function({chart}) { console.log(`I was panned!!!`); },
   // Function called when pan fails because modifier key was not detected.
   // event is the a hammer event that failed - see https://hammerjs.github.io/api#event-object
   onPanRejected: function({chart, event}) { console.log(`I didn't start panning!`); }
  },

  // Container for zoom options
  zoom: {
   // Boolean to enable zooming
   enabled: true,

   // Enable drag-to-zoom behavior
   drag: true,

   // Drag-to-zoom effect can be customized
   // drag: {
   //   borderColor: 'rgba(225,225,225,0.3)'
   //   borderWidth: 5,
   //   backgroundColor: 'rgb(225,225,225)',
   //   animationDuration: 0
   // },

   // Zooming directions. Remove the appropriate direction to disable
   // Eg. 'y' would only allow zooming in the y direction
   // A function that is called as the user is zooming and returns the
   // available directions can also be used:
   //   mode: function({ chart }) {
   //     return 'xy';
   //   },
   mode: 'xy',

   // Which of the enabled zooming directions should only be available
   // when the mouse cursor is over one of scale.
   overScaleMode: 'xy',

   rangeMin: {
    // Format of min zoom range depends on scale type
    x: null,
    y: null
   },
   rangeMax: {
    // Format of max zoom range depends on scale type
    x: null,
    y: null
   },

   // Speed of zoom via mouse wheel
   // (percentage of zoom on a wheel event)
   speed: 0.1,

   // Minimal zoom distance required before actually applying zoom
   threshold: 2,

   // On category scale, minimal zoom level before actually applying zoom
   sensitivity: 3,

   // Function called while the user is zooming
   onZoom: function({chart}) { console.log(`I'm zooming!!!`); },
   // Function called once zooming is completed
   onZoomComplete: function({chart}) { console.log(`I was zoomed!!!`); },
   // Function called when wheel input occurs without modifier key
   onZoomRejected: function({chart, event}) { console.log(`I didn't start zooming!`); }
  }
 }
}

API

chart.resetZoom()

Programmatically resets the zoom to the default state. See samples/zoom-time.html for an example.

Documentation

You can find documentation for Chart.js at www.chartjs.org/docs.

Examples for this plugin are available in the samples folder.

Prior to v0.4.0, this plugin was known as 'Chart.Zoom.js'. Old versions are still available on npm under that name.

Contributing

Before submitting an issue or a pull request to the project, please take a moment to look over the contributing guidelines first.

License

chartjs-plugin-zoom.js is available under the MIT license.

版本与发布记录

@foxglove/chartjs-plugin-zoom 共发布 4 个版本,首次发布于 2022/01/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2025/04/111.6 年前
2.0.52025/03/211.7 年前
2.0.42022/01/064 年前
2.0.32022/01/064 年前

关于 @foxglove/chartjs-plugin-zoom 的常见问题

@foxglove/chartjs-plugin-zoom 是什么?

Plugin that enables zoom and pan functionality in Chart.js charts.

如何安装 @foxglove/chartjs-plugin-zoom?

在项目目录运行 npm install @foxglove/chartjs-plugin-zoom,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:chart.js(^3.0.0-beta.13)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@foxglove/chartjs-plugin-zoom 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@foxglove/chartjs-plugin-zoom 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@foxglove/chartjs-plugin-zoom 可以商用吗?

@foxglove/chartjs-plugin-zoom 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@foxglove/chartjs-plugin-zoom 的下载量有多少?

根据 npm 官方下载统计,@foxglove/chartjs-plugin-zoom 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,319 次,日均约 77 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@foxglove/chartjs-plugin-zoom 还在维护吗?

最新版本 2.1.0 发布于 2025/04/11(1.6 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/01/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。