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

chartjs / chartjs-plugin-zoom / 11843695559

14 Nov 2024 06:56PM UTC coverage: 83.292%. Remained the same
11843695559

Pull #848

github

web-flow
Merge 0aee1e87c into c1985b1d3
Pull Request #848: Bump express from 4.18.2 to 4.21.1

263 of 330 branches covered (79.7%)

Branch coverage included in aggregate %.

415 of 484 relevant lines covered (85.74%)

1503.32 hits per line

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

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

3
export const getModifierKey = opts => opts && opts.enabled && opts.modifierKey;
320✔
4
export const keyPressed = (key, event) => key && event[key + 'Key'];
100✔
5
export const keyNotPressed = (key, event) => key && !event[key + 'Key'];
220✔
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) {
424!
15
    return true;
×
16
  } else if (typeof mode === 'string') {
424✔
17
    return mode.indexOf(dir) !== -1;
376✔
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') {
552!
27
    mode = mode({chart});
×
28
  }
29
  if (typeof mode === 'string') {
552✔
30
    return {x: mode.indexOf('x') !== -1, y: mode.indexOf('y') !== -1};
288✔
31
  }
32

33
  return {x: false, y: false};
264✔
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;
272✔
59
  const scaleIds = Object.keys(scales);
272✔
60
  for (let i = 0; i < scaleIds.length; i++) {
272✔
61
    const scale = scales[scaleIds[i]];
544✔
62
    if (y >= scale.top && y <= scale.bottom && x >= scale.left && x <= scale.right) {
544✔
63
      return scale;
8✔
64
    }
65
  }
66
  return null;
264✔
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 || {};
272!
80
  const scale = getScaleUnderPoint(point, chart);
272✔
81

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

85
  // Convert deprecated overScaleEnabled to new scaleEnabled.
86
  if (overScaleMode) {
272✔
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]) {
272✔
97
    return [scale];
8✔
98
  }
99

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