| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.getChartPointer = void 0;
|
|---|
| 7 | /**
|
|---|
| 8 | * Computes the chart coordinates from the mouse event.
|
|---|
| 9 | *
|
|---|
| 10 | * The coordinates are relative to the top-left corner of the chart,
|
|---|
| 11 | * where the top-left corner of the chart is (0, 0).
|
|---|
| 12 | * Moving right, the x-coordinate increases, and moving down, the y-coordinate increases.
|
|---|
| 13 | *
|
|---|
| 14 | * The coordinates are rounded to the nearest integer and are including a CSS transform scale.
|
|---|
| 15 | * So a chart that's scaled will return the same coordinates as a chart that's not scaled.
|
|---|
| 16 | *
|
|---|
| 17 | * @param event The mouse event from React event handlers
|
|---|
| 18 | * @return chartPointer The chart coordinates relative to the top-left corner of the chart
|
|---|
| 19 | */
|
|---|
| 20 | var getChartPointer = event => {
|
|---|
| 21 | var rect = event.currentTarget.getBoundingClientRect();
|
|---|
| 22 | var scaleX = rect.width / event.currentTarget.offsetWidth;
|
|---|
| 23 | var scaleY = rect.height / event.currentTarget.offsetHeight;
|
|---|
| 24 | return {
|
|---|
| 25 | /*
|
|---|
| 26 | * Here it's important to use:
|
|---|
| 27 | * - event.clientX and event.clientY to get the mouse position relative to the viewport, including scroll.
|
|---|
| 28 | * - pageX and pageY are not used because they are relative to the whole document, and ignore scroll.
|
|---|
| 29 | * - rect.left and rect.top are used to get the position of the chart relative to the viewport.
|
|---|
| 30 | * - offsetX and offsetY are not used because they are relative to the offset parent
|
|---|
| 31 | * which may or may not be the same as the clientX and clientY, depending on the position of the chart in the DOM
|
|---|
| 32 | * and surrounding element styles. CSS position: relative, absolute, fixed, will change the offset parent.
|
|---|
| 33 | * - scaleX and scaleY are necessary for when the chart element is scaled using CSS `transform: scale(N)`.
|
|---|
| 34 | */
|
|---|
| 35 | chartX: Math.round((event.clientX - rect.left) / scaleX),
|
|---|
| 36 | chartY: Math.round((event.clientY - rect.top) / scaleY)
|
|---|
| 37 | };
|
|---|
| 38 | };
|
|---|
| 39 | exports.getChartPointer = getChartPointer; |
|---|