• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chartjs / chartjs-plugin-zoom / 12162401025

04 Dec 2024 02:59PM UTC coverage: 88.064% (+0.2%) from 87.903%
12162401025

Pull #907

github

web-flow
Merge 771d52450 into 4aa6d11ae
Pull Request #907: refactor: convert to typescript

262 of 331 branches covered (79.15%)

Branch coverage included in aggregate %.

675 of 733 new or added lines in 9 files covered. (92.09%)

675 of 733 relevant lines covered (92.09%)

1624.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.77
/src/plugin.ts
1
import Hammer from 'hammerjs'
2
import { addListeners, computeDragRect, removeListeners } from './handlers'
3
import { hammerOptionsChanged, startHammer, stopHammer } from './hammer'
4
import {
5
  pan,
6
  zoom,
7
  resetZoom,
8
  zoomScale,
9
  getZoomLevel,
10
  getInitialScaleBounds,
11
  getZoomedScaleBounds,
12
  isZoomedOrPanned,
13
  isZoomingOrPanning,
14
  zoomRect,
15
} from './core'
16
import { panFunctions, zoomFunctions, zoomRectFunctions } from './scale.types'
17
import { getState, removeState } from './state'
18
import { version } from '../package.json'
19
import type { Chart, ChartEvent } from 'chart.js'
20
import type { ZoomPluginOptions } from './options'
21
import { defaults } from './defaults'
22

23
function draw(chart: Chart, caller: string, options: ZoomPluginOptions) {
24
  const dragOptions = options.zoom?.drag
9,504✔
25
  const { dragStart, dragEnd } = getState(chart)
9,504✔
26

27
  if (dragOptions?.drawTime !== caller || !dragStart || !dragEnd) {
9,504✔
28
    return
9,460✔
29
  }
30
  const { left, top, width, height } = computeDragRect(
44✔
31
    chart,
32
    options.zoom?.mode,
33
    { dragStart, dragEnd },
34
    dragOptions.maintainAspectRatio
35
  )
36
  const ctx = chart.ctx
44✔
37

38
  ctx.save()
44✔
39
  ctx.beginPath()
44✔
40
  ctx.fillStyle = dragOptions.backgroundColor || 'rgba(225,225,225,0.3)'
44✔
41
  ctx.fillRect(left, top, width, height)
44✔
42

43
  if (dragOptions.borderWidth) {
44✔
44
    ctx.lineWidth = dragOptions.borderWidth
8✔
45
    ctx.strokeStyle = dragOptions.borderColor || 'rgba(225,225,225)'
8!
46
    ctx.strokeRect(left, top, width, height)
8✔
47
  }
48
  ctx.restore()
44✔
49
}
50

51
const bindApi = (chart: Chart) => {
4✔
52
  chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition)
336✔
53
  chart.zoom = (args, transition) => zoom(chart, args, transition)
332✔
54
  chart.zoomRect = (p0, p1, transition) => zoomRect(chart, p0, p1, transition)
332✔
55
  chart.zoomScale = (id, range, transition) => zoomScale(chart, id, range, transition)
332✔
56
  chart.resetZoom = (transition) => resetZoom(chart, transition)
332✔
57
  chart.getZoomLevel = () => getZoomLevel(chart)
332✔
58
  chart.getInitialScaleBounds = () => getInitialScaleBounds(chart)
332✔
59
  chart.getZoomedScaleBounds = () => getZoomedScaleBounds(chart)
332✔
60
  chart.isZoomedOrPanned = () => isZoomedOrPanned(chart)
332✔
61
  chart.isZoomingOrPanning = () => isZoomingOrPanning(chart)
332✔
62
}
63

64
export default {
65
  id: 'zoom',
66

67
  version,
68

69
  defaults,
70

71
  start(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
72
    const state = getState(chart)
332✔
73
    state.options = options
332✔
74

75
    if (Object.prototype.hasOwnProperty.call(options.zoom, 'enabled')) {
332!
NEW
76
      console.warn(
×
77
        'The option `zoom.enabled` is no longer supported. Please use `zoom.wheel.enabled`, `zoom.drag.enabled`, or `zoom.pinch.enabled`.'
78
      )
79
    }
80
    if (
332✔
81
      Object.prototype.hasOwnProperty.call(options.zoom, 'overScaleMode') ||
656✔
82
      Object.prototype.hasOwnProperty.call(options.pan, 'overScaleMode')
83
    ) {
84
      console.warn(
8✔
85
        'The option `overScaleMode` is deprecated. Please use `scaleMode` instead (and update `mode` as desired).'
86
      )
87
    }
88

89
    if (Hammer) {
332✔
90
      startHammer(chart, options)
332✔
91
    }
92

93
    bindApi(chart)
332✔
94
  },
95

96
  beforeEvent(
97
    chart: Chart,
98
    { event }: { event: ChartEvent; replay: boolean; cancelable: true; inChartArea: boolean }
99
  ): boolean | void {
100
    if (isZoomingOrPanning(chart)) {
4✔
101
      // cancel any event handling while panning or dragging
102
      return false
4✔
103
    }
104
    // cancel the next click or mouseup after drag or pan
NEW
105
    if (event.type === 'click' || event.type === 'mouseup') {
×
NEW
106
      const state = getState(chart)
×
NEW
107
      if (state.filterNextClick) {
×
NEW
108
        state.filterNextClick = false
×
NEW
109
        return false
×
110
      }
111
    }
112
  },
113

114
  beforeUpdate(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
115
    const state = getState(chart)
2,372✔
116
    const previousOptions = state.options
2,372✔
117
    state.options = options
2,372✔
118

119
    // Hammer needs to be restarted when certain options change.
120
    if (hammerOptionsChanged(previousOptions, options)) {
2,372!
NEW
121
      stopHammer(chart)
×
NEW
122
      startHammer(chart, options)
×
123
    }
124

125
    addListeners(chart, options)
2,372✔
126
  },
127

128
  beforeDatasetsDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
129
    draw(chart, 'beforeDatasetsDraw', options)
2,376✔
130
  },
131

132
  afterDatasetsDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
133
    draw(chart, 'afterDatasetsDraw', options)
2,376✔
134
  },
135

136
  beforeDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
137
    draw(chart, 'beforeDraw', options)
2,376✔
138
  },
139

140
  afterDraw(chart: Chart, _args: unknown, options: ZoomPluginOptions) {
141
    draw(chart, 'afterDraw', options)
2,376✔
142
  },
143

144
  stop(chart: Chart) {
145
    removeListeners(chart)
12,808✔
146

147
    if (Hammer) {
12,808✔
148
      stopHammer(chart)
12,808✔
149
    }
150
    removeState(chart)
12,808✔
151
  },
152

153
  panFunctions,
154
  zoomFunctions,
155
  zoomRectFunctions,
156
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc