Skip to content

Commit 8c39fba

Browse files
authored
Fix drag rectangle calculation when in shadow BOM (#732)
The edges of the drag rectangle were calculated incorrectly when the chart is placed inside a shadow BOM (eg. inside a web component). Chart.js itself already has fixes for that (see PRs #7225 and #8082). Its getRelativePosition() function always returns the correct coordinates. So import and use this function from Chart.js. Co-authored-by: Marco von Rosenberg <[email protected]>
1 parent f6f178a commit 8c39fba

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/handlers.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {directionEnabled, debounce, keyNotPressed, getModifierKey, keyPressed} from './utils';
22
import {zoom, zoomRect} from './core';
3-
import {callback as call} from 'chart.js/helpers';
3+
import {callback as call, getRelativePosition} from 'chart.js/helpers';
44
import {getState} from './state';
55

66
function removeHandler(chart, type) {
@@ -49,11 +49,7 @@ function keyDown(chart, event) {
4949
function zoomStart(chart, event, zoomOptions) {
5050
const {onZoomStart, onZoomRejected} = zoomOptions;
5151
if (onZoomStart) {
52-
const {left: offsetX, top: offsetY} = event.target.getBoundingClientRect();
53-
const point = {
54-
x: event.clientX - offsetX,
55-
y: event.clientY - offsetY
56-
};
52+
const point = getRelativePosition(event, chart);
5753
if (call(onZoomStart, [{chart, event, point}]) === false) {
5854
call(onZoomRejected, [{chart, event}]);
5955
return false;
@@ -81,20 +77,22 @@ export function mouseDown(chart, event) {
8177
addHandler(chart, window.document, 'keydown', keyDown);
8278
}
8379

84-
export function computeDragRect(chart, mode, beginPoint, endPoint) {
85-
const {left: offsetX, top: offsetY} = beginPoint.target.getBoundingClientRect();
80+
export function computeDragRect(chart, mode, beginPointEvent, endPointEvent) {
8681
const xEnabled = directionEnabled(mode, 'x', chart);
8782
const yEnabled = directionEnabled(mode, 'y', chart);
8883
let {top, left, right, bottom, width: chartWidth, height: chartHeight} = chart.chartArea;
8984

85+
const beginPoint = getRelativePosition(beginPointEvent, chart);
86+
const endPoint = getRelativePosition(endPointEvent, chart);
87+
9088
if (xEnabled) {
91-
left = Math.min(beginPoint.clientX, endPoint.clientX) - offsetX;
92-
right = Math.max(beginPoint.clientX, endPoint.clientX) - offsetX;
89+
left = Math.min(beginPoint.x, endPoint.x);
90+
right = Math.max(beginPoint.x, endPoint.x);
9391
}
9492

9593
if (yEnabled) {
96-
top = Math.min(beginPoint.clientY, endPoint.clientY) - offsetY;
97-
bottom = Math.max(beginPoint.clientY, endPoint.clientY) - offsetY;
94+
top = Math.min(beginPoint.y, endPoint.y);
95+
bottom = Math.max(beginPoint.y, endPoint.y);
9896
}
9997
const width = right - left;
10098
const height = bottom - top;

0 commit comments

Comments
 (0)