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

chartjs / chartjs-plugin-zoom / 12024969001

26 Nov 2024 06:17AM UTC coverage: 88.324% (+4.8%) from 83.508%
12024969001

push

github

web-flow
fix: pinch events (#905)

246 of 294 branches covered (83.67%)

Branch coverage included in aggregate %.

4 of 4 new or added lines in 1 file covered. (100.0%)

518 of 571 relevant lines covered (90.72%)

1903.13 hits per line

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

90.14
/src/utils.js
1
import {each} from 'chart.js/helpers';
2

3
export const getModifierKey = opts => opts && opts.enabled && opts.modifierKey;
368✔
4
export const keyPressed = (key, event) => key && event[key + 'Key'];
112✔
5
export const keyNotPressed = (key, event) => key && !event[key + 'Key'];
256✔
6

7
/**
8
 * @param {string|function} mode can be 'x', 'y' or 'xy'
9
 * @param {string} dir can be 'x' or 'y'
10
 * @param {import('chart.js').Chart} chart instance of the chart in question
11
 * @returns {boolean}
12
 */
13
export function directionEnabled(mode, dir, chart) {
14
  if (mode === undefined) {
1,248!
15
    return true;
×
16
  } else if (typeof mode === 'string') {
1,248✔
17
    return mode.indexOf(dir) !== -1;
1,200✔
18
  } else if (typeof mode === 'function') {
48✔
19
    return mode({chart}).indexOf(dir) !== -1;
48✔
20
  }
21

22
  return false;
×
23
}
24

25
function directionsEnabled(mode, chart) {
26
  if (typeof mode === 'function') {
2,160!
27
    mode = mode({chart});
×
28
  }
29
  if (typeof mode === 'string') {
2,160✔
30
    return {x: mode.indexOf('x') !== -1, y: mode.indexOf('y') !== -1};
1,092✔
31
  }
32

33
  return {x: false, y: false};
1,068✔
34
}
35

36
/**
37
 * Debounces calling `fn` for `delay` ms
38
 * @param {function} fn - Function to call. No arguments are passed.
39
 * @param {number} delay - Delay in ms. 0 = immediate invocation.
40
 * @returns {function}
41
 */
42
export function debounce(fn, delay) {
43
  let timeout;
44
  return function() {
8✔
45
    clearTimeout(timeout);
4✔
46
    timeout = setTimeout(fn, delay);
4✔
47
    return delay;
4✔
48
  };
49
}
50

51
/**
52
 * Checks which axis is under the mouse cursor.
53
 * @param {{x: number, y: number}} point - the mouse location
54
 * @param {import('chart.js').Chart} [chart] instance of the chart in question
55
 * @return {import('chart.js').Scale}
56
 */
57
function getScaleUnderPoint({x, y}, chart) {
58
  const scales = chart.scales;
1,076✔
59
  const scaleIds = Object.keys(scales);
1,076✔
60
  for (let i = 0; i < scaleIds.length; i++) {
1,076✔
61
    const scale = scales[scaleIds[i]];
2,128✔
62
    if (y >= scale.top && y <= scale.bottom && x >= scale.left && x <= scale.right) {
2,128✔
63
      return scale;
32✔
64
    }
65
  }
66
  return null;
1,044✔
67
}
68

69
/**
70
 * Evaluate the chart's mode, scaleMode, and overScaleMode properties to
71
 * determine which axes are eligible for scaling.
72
 * options.overScaleMode can be a function if user want zoom only one scale of many for example.
73
 * @param options - Zoom or pan options
74
 * @param {{x: number, y: number}} point - the mouse location
75
 * @param {import('chart.js').Chart} [chart] instance of the chart in question
76
 * @return {import('chart.js').Scale[]}
77
 */
78
export function getEnabledScalesByPoint(options, point, chart) {
79
  const {mode = 'xy', scaleMode, overScaleMode} = options || {};
1,076!
80
  const scale = getScaleUnderPoint(point, chart);
1,076✔
81

82
  const enabled = directionsEnabled(mode, chart);
1,076✔
83
  const scaleEnabled = directionsEnabled(scaleMode, chart);
1,076✔
84

85
  // Convert deprecated overScaleEnabled to new scaleEnabled.
86
  if (overScaleMode) {
1,076✔
87
    const overScaleEnabled = directionsEnabled(overScaleMode, chart);
8✔
88
    for (const axis of ['x', 'y']) {
8✔
89
      if (overScaleEnabled[axis]) {
16✔
90
        scaleEnabled[axis] = enabled[axis];
8✔
91
        enabled[axis] = false;
8✔
92
      }
93
    }
94
  }
95

96
  if (scale && scaleEnabled[scale.axis]) {
1,076✔
97
    return [scale];
8✔
98
  }
99

100
  const enabledScales = [];
1,068✔
101
  each(chart.scales, function(scaleItem) {
1,068✔
102
    if (enabled[scaleItem.axis]) {
2,136✔
103
      enabledScales.push(scaleItem);
1,940✔
104
    }
105
  });
106
  return enabledScales;
1,068✔
107
}
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