-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathplugin.ts
More file actions
157 lines (134 loc) · 4.58 KB
/
plugin.ts
File metadata and controls
157 lines (134 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Hammer from 'hammerjs'
import { addListeners, computeDragRect, removeListeners } from './handlers'
import { hammerOptionsChanged, startHammer, stopHammer } from './hammer'
import {
pan,
zoom,
resetZoom,
zoomScale,
getZoomLevel,
getInitialScaleBounds,
getZoomedScaleBounds,
isZoomedOrPanned,
isZoomingOrPanning,
isZoomingOrPanningState,
zoomRect,
} from './core'
import { panFunctions, zoomFunctions, zoomRectFunctions } from './scale.types'
import { getState, removeState } from './state'
import { version } from '../package.json'
import type { Chart, ChartEvent } from 'chart.js'
import type { ZoomPluginOptions } from './options'
import { defaults } from './defaults'
function draw(chart: Chart, caller: string, options: ZoomPluginOptions) {
const dragOptions = options.zoom?.drag
const { dragStart, dragEnd } = getState(chart)
if (dragOptions?.drawTime !== caller || !dragStart || !dragEnd) {
return
}
const { left, top, width, height } = computeDragRect(
chart,
options.zoom?.mode,
{ dragStart, dragEnd },
dragOptions.maintainAspectRatio
)
const ctx = chart.ctx
ctx.save()
ctx.beginPath()
ctx.fillStyle = dragOptions.backgroundColor || 'rgba(225,225,225,0.3)'
ctx.fillRect(left, top, width, height)
if (dragOptions.borderWidth) {
ctx.lineWidth = dragOptions.borderWidth
ctx.strokeStyle = dragOptions.borderColor || 'rgba(225,225,225)'
ctx.strokeRect(left, top, width, height)
}
ctx.restore()
}
const bindApi = (chart: Chart) => {
chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition, "api")
chart.zoom = (args, transition) => zoom(chart, args, transition)
chart.zoomRect = (p0, p1, transition) => zoomRect(chart, p0, p1, transition)
chart.zoomScale = (id, range, transition) => zoomScale(chart, id, range, transition)
chart.resetZoom = (transition) => resetZoom(chart, transition)
chart.getZoomLevel = () => getZoomLevel(chart)
chart.getInitialScaleBounds = () => getInitialScaleBounds(chart)
chart.getZoomedScaleBounds = () => getZoomedScaleBounds(chart)
chart.isZoomedOrPanned = () => isZoomedOrPanned(chart)
chart.isZoomingOrPanning = () => isZoomingOrPanning(chart)
}
export default {
id: 'zoom',
version,
defaults,
start(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
const state = getState(chart)
state.options = options
if (Object.prototype.hasOwnProperty.call(options.zoom, 'enabled')) {
console.warn(
'The option `zoom.enabled` is no longer supported. Please use `zoom.wheel.enabled`, `zoom.drag.enabled`, or `zoom.pinch.enabled`.'
)
}
if (
Object.prototype.hasOwnProperty.call(options.zoom, 'overScaleMode') ||
Object.prototype.hasOwnProperty.call(options.pan, 'overScaleMode')
) {
console.warn(
'The option `overScaleMode` is deprecated. Please use `scaleMode` instead (and update `mode` as desired).'
)
}
if (Hammer) {
startHammer(chart, options)
}
bindApi(chart)
},
beforeEvent(
chart: Chart,
{ event }: { event: ChartEvent; replay: boolean; cancelable: true; inChartArea: boolean }
): boolean | void {
const state = getState(chart)
if (isZoomingOrPanningState(state)) {
// cancel any event handling while panning or dragging
return false
}
// cancel the next click or mouseup after drag or pan
if (event.type === 'click' || event.type === 'mouseup') {
if (state.filterNextClick) {
state.filterNextClick = false
return false
}
}
},
beforeUpdate(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
const state = getState(chart)
const previousOptions = state.options
state.options = options
// Hammer needs to be restarted when certain options change.
if (hammerOptionsChanged(previousOptions, options)) {
stopHammer(chart)
startHammer(chart, options)
}
addListeners(chart, options)
},
beforeDatasetsDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
draw(chart, 'beforeDatasetsDraw', options)
},
afterDatasetsDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
draw(chart, 'afterDatasetsDraw', options)
},
beforeDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
draw(chart, 'beforeDraw', options)
},
afterDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
draw(chart, 'afterDraw', options)
},
stop(chart: Chart) {
removeListeners(chart)
if (Hammer) {
stopHammer(chart)
}
removeState(chart)
},
panFunctions,
zoomFunctions,
zoomRectFunctions,
}