source: node_modules/recharts/es6/util/getChartPointer.js@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

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