Skip to content

Commit 33ba619

Browse files
joshkelkurkle
andauthored
Documentation and type definitions for zoomFunctions and panFunctions (#534)
* Add type definitions for `zoomFunctions` and `panFunctions` * Add documentation for zoomFunctions and panFunctions * Update docs/guide/developers.md Co-authored-by: Jukka Kurkela <jukka.kurkela@gmail.com> Co-authored-by: Jukka Kurkela <jukka.kurkela@gmail.com>
1 parent bb7b8fc commit 33ba619

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = {
7575
'usage',
7676
'options',
7777
'animations',
78+
'developers',
7879
],
7980
'/samples/': [
8081
'basic',

docs/guide/developers.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Developers
2+
3+
You can extend chartjs-plugin-zoom with support for [custom scales](https://www.chartjs.org/docs/latest/developers/axes.html) by using the zoom plugin's `zoomFunctions` and `panFunctions` members. These objects are indexed by scale types (scales' `id` members) and give optional handlers for zoom and pan functionality.
4+
5+
```js
6+
import {Scale} from 'chart.js';
7+
import zoomPlugin from 'chartjs-plugin-zoom';
8+
9+
class MyScale extends Scale {
10+
/* extensions ... */
11+
}
12+
MyScale.id = 'myScale';
13+
MyScale.defaults = defaultConfigObject;
14+
15+
zoomPlugin.zoomFunctions.myScale = (scale, zoom, center, limits) => false;
16+
zoomPlugin.panFunctions.myScale = (scale, delta, limits) => false;
17+
```
18+
19+
The zoom and pan functions take the following arguments:
20+
21+
| Name | Type | For | Description
22+
| ---- | ---- | --- | ----------
23+
| `scale` | `Scale` | Zoom, Pan | The custom scale instance (usually derived from `Chart.Scale`)
24+
| `zoom` | `number` | Zoom | The zoom fraction; 1.0 is unzoomed, 0.5 means zoomed in to 50% of the original area, etc.
25+
| `center` | `{x, y}` | Zoom | Pixel coordinates of the center of the zoom operation. `{x: 0, y: 0}` is the upper left corner of the chart's canvas.
26+
| `pan` | `number` | Pan | Pixel amount to pan by
27+
| `limits` | [Limits](./options#limits) | Zoom, Pan | Zoom and pan limits (from chart options)
28+
29+
For examples, see chartjs-plugin-zoom's [default zoomFunctions and panFunctions handling for standard Chart.js axes](https://github.com/chartjs/chartjs-plugin-zoom/blob/v1.0.1/src/scale.types.js#L128).

types/index.d.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Plugin, ChartType, Chart, Scale, UpdateMode } from 'chart.js';
1+
import { Plugin, ChartType, Chart, Scale, UpdateMode, ScaleTypeRegistry } from 'chart.js';
22
import { DistributiveArray } from 'chart.js/types/utils';
3-
import { ZoomPluginOptions } from './options';
3+
import { LimitOptions, ZoomPluginOptions } from './options';
44

55
type Point = { x: number, y: number };
66
type ZoomAmount = number | Partial<Point> & { focalPoint?: Point };
77
type PanAmount = number | Partial<Point>;
88
type ScaleRange = { min: number, max: number };
99

1010
declare module 'chart.js' {
11-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12-
interface PluginOptionsByType<TType extends ChartType> {
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
interface PluginOptionsByType<TType extends ChartType> {
1313
zoom: ZoomPluginOptions;
1414
}
1515

@@ -26,7 +26,19 @@ declare module 'chart.js' {
2626
}
2727
}
2828

29-
declare const Zoom: Plugin;
29+
export type ZoomFunction = (scale: Scale, zoom: number, center: Point, limits: LimitOptions) => boolean;
30+
export type PanFunction = (scale: Scale, delta: number, limits: LimitOptions) => boolean;
31+
32+
type ScaleFunctions<T> = {
33+
[scaleType in keyof ScaleTypeRegistry]?: T | undefined;
34+
} & {
35+
default: T;
36+
};
37+
38+
declare const Zoom: Plugin & {
39+
zoomFunctions: ScaleFunctions<ZoomFunction>;
40+
panFunctions: ScaleFunctions<PanFunction>;
41+
};
3042

3143
export default Zoom;
3244

0 commit comments

Comments
 (0)