1 | const getArea = (a, b) => a * b;
|
2 | const getPointDistance = (a, b) => Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2));
|
3 | export const getElementVisibleWidth = (elementWidth, xOffset, ScreenWidth) => {
|
4 |
|
5 | if (xOffset >= 0) {
|
6 | return xOffset + elementWidth <= ScreenWidth
|
7 | ? elementWidth
|
8 | : ScreenWidth - xOffset;
|
9 | }
|
10 |
|
11 | return elementWidth - xOffset;
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const getTooltipCoordinate = (x, y, width, height, ScreenWidth, ScreenHeight, tooltipWidth, tooltipHeight, withPointer) => {
|
35 |
|
36 | const center = [
|
37 | x + getElementVisibleWidth(width, x, ScreenWidth) / 2,
|
38 | y + height / 2,
|
39 | ];
|
40 | const pOne = [center[0], 0];
|
41 | const pTwo = [ScreenWidth, center[1]];
|
42 | const pThree = [center[0], ScreenHeight];
|
43 | const pFour = [0, center[1]];
|
44 |
|
45 | const vOne = getPointDistance(center, pOne);
|
46 | const vTwo = getPointDistance(center, pTwo);
|
47 | const vThree = getPointDistance(center, pThree);
|
48 | const vFour = getPointDistance(center, pFour);
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | const areas = [
|
55 | getArea(vOne, vFour),
|
56 | getArea(vOne, vTwo),
|
57 | getArea(vTwo, vThree),
|
58 | getArea(vThree, vFour),
|
59 | ].map((each, index) => ({ area: each, id: index }));
|
60 | const sortedArea = areas.sort((a, b) => b.area - a.area);
|
61 |
|
62 | const dX = 0.001;
|
63 | const dY = height / 2;
|
64 |
|
65 | const directionCorrection = [
|
66 | [-1, -1],
|
67 | [1, -1],
|
68 | [1, 1],
|
69 | [-1, 1],
|
70 | ];
|
71 | const deslocateReferencePoint = [
|
72 | [-tooltipWidth, -tooltipHeight],
|
73 | [0, -tooltipHeight],
|
74 | [0, 0],
|
75 | [-tooltipWidth, 0],
|
76 | ];
|
77 |
|
78 | const qIndex = sortedArea[0].id;
|
79 | const getWithPointerOffsetY = () => withPointer ? 10 * directionCorrection[qIndex][1] : 0;
|
80 | const getWithPointerOffsetX = () => withPointer ? center[0] - 18 * directionCorrection[qIndex][0] : center[0];
|
81 | const newX = getWithPointerOffsetX() +
|
82 | (dX * directionCorrection[qIndex][0] + deslocateReferencePoint[qIndex][0]);
|
83 | return {
|
84 | x: constraintX(newX, qIndex, center[0], ScreenWidth, tooltipWidth),
|
85 | y: center[1] +
|
86 | (dY * directionCorrection[qIndex][1] +
|
87 | deslocateReferencePoint[qIndex][1]) +
|
88 | getWithPointerOffsetY(),
|
89 | };
|
90 | };
|
91 | const constraintX = (newX, qIndex, x, ScreenWidth, tooltipWidth) => {
|
92 | switch (qIndex) {
|
93 |
|
94 | case 0:
|
95 | case 3: {
|
96 | const maxWidth = newX > ScreenWidth ? ScreenWidth - 10 : newX;
|
97 | return newX < 1 ? 10 : maxWidth;
|
98 | }
|
99 |
|
100 | case 1:
|
101 | case 2: {
|
102 | const leftOverSpace = ScreenWidth - newX;
|
103 | return leftOverSpace >= tooltipWidth
|
104 | ? newX
|
105 | : newX - (tooltipWidth - leftOverSpace + 10);
|
106 | }
|
107 | default: {
|
108 | return 0;
|
109 | }
|
110 | }
|
111 | };
|
112 | export default getTooltipCoordinate;
|