Skip to content

Commit 0aabe33

Browse files
committed
fix: cursor bubble alignment
1 parent ca09405 commit 0aabe33

File tree

1 file changed

+65
-57
lines changed

1 file changed

+65
-57
lines changed

src/components/Cursor.tsx

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import React from 'react'
1+
import React from 'react';
22
//
3-
import { translate } from '../utils/Utils'
3+
import { translate } from '../utils/Utils';
44

5-
import useChartContext from '../hooks/useChartContext'
6-
import useLatest from '../hooks/useLatest'
5+
import useChartContext from '../hooks/useChartContext';
6+
import useLatest from '../hooks/useLatest';
77

88
const getLineBackgroundColor = dark =>
9-
dark ? 'rgba(255,255,255,.3)' : 'rgba(0, 26, 39, 0.3)'
9+
dark ? 'rgba(255,255,255,.3)' : 'rgba(0, 26, 39, 0.3)';
1010

1111
const getBackgroundColor = dark =>
12-
dark ? 'rgba(255,255,255,.9)' : 'rgba(0, 26, 39, 0.9)'
12+
dark ? 'rgba(255,255,255,.9)' : 'rgba(0, 26, 39, 0.9)';
1313

1414
export default function Cursor({ primary }) {
1515
const {
@@ -20,10 +20,10 @@ export default function Cursor({ primary }) {
2020
gridX,
2121
gridY,
2222
dark,
23-
} = useChartContext()
23+
} = useChartContext();
2424

25-
const resolvedFocused = focused || latestFocused
26-
const cursor = primary ? primaryCursor : secondaryCursor
25+
const resolvedFocused = focused || latestFocused;
26+
const cursor = primary ? primaryCursor : secondaryCursor;
2727

2828
const {
2929
showLine,
@@ -34,67 +34,71 @@ export default function Cursor({ primary }) {
3434
axis,
3535
siblingAxis,
3636
resolvedShow,
37-
} = cursor || {}
37+
} = cursor || {};
3838

3939
const latestValue = useLatest(
4040
resolvedValue,
4141
typeof resolvedValue !== 'undefined'
42-
)
42+
);
4343

44-
const previousShowRef = React.useRef()
44+
const previousShowRef = React.useRef();
4545
React.useEffect(() => {
46-
previousShowRef.current = resolvedShow
47-
}, [resolvedShow])
46+
previousShowRef.current = resolvedShow;
47+
}, [resolvedShow]);
4848

4949
if (!cursor) {
50-
return null
50+
return null;
5151
}
5252

5353
// Should we animate?
54-
const animated = snap || axis.type === 'ordinal'
54+
const animated = snap || axis.type === 'ordinal';
5555

5656
// Get the sibling range
57-
const siblingRange = siblingAxis.scale.range()
57+
const siblingRange = siblingAxis.scale.range();
5858

59-
let x
60-
let y
61-
let x1
62-
let x2
63-
let y1
64-
let y2
65-
let alignPctX
66-
let alignPctY
59+
let x;
60+
let y;
61+
let x1;
62+
let x2;
63+
let y1;
64+
let y2;
65+
let alignPctX;
66+
let alignPctY;
6767

6868
// Vertical alignment
6969
if (axis.vertical) {
70-
y = axis.scale(latestValue)
71-
x1 = siblingRange[0]
72-
x2 = siblingRange[1]
73-
y1 = y - 1
74-
y2 = y + axis.cursorSize + 1
70+
y = axis.scale(latestValue);
71+
y1 = y - 1;
72+
y2 = y + axis.cursorSize + 1;
7573
if (axis.position === 'left') {
76-
alignPctX = -100
77-
alignPctY = -50
74+
x1 = siblingRange[0];
75+
x2 = siblingRange[1];
76+
alignPctX = -100;
77+
alignPctY = -50;
7878
} else {
79-
alignPctX = 0
80-
alignPctY = -50
79+
x1 = siblingRange[1];
80+
x2 = siblingRange[0];
81+
alignPctX = 0;
82+
alignPctY = -50;
8183
}
8284
} else {
83-
x = axis.scale(latestValue)
84-
x1 = x - 1
85-
x2 = x + axis.cursorSize + 1
86-
y1 = siblingRange[0]
87-
y2 = siblingRange[1]
85+
x = axis.scale(latestValue);
86+
x1 = x - 1;
87+
x2 = x + axis.cursorSize + 1;
8888
if (axis.position === 'top') {
89-
alignPctX = -500
90-
alignPctY = -100
89+
y1 = siblingRange[0];
90+
y2 = siblingRange[1];
91+
alignPctX = -500;
92+
alignPctY = -100;
9193
} else {
92-
alignPctX = -50
93-
alignPctY = 0
94+
y1 = siblingRange[1];
95+
y2 = siblingRange[0];
96+
alignPctX = -50;
97+
alignPctY = 0;
9498
}
9599
}
96100

97-
const renderProps = { ...cursor }
101+
const renderProps = { ...cursor };
98102

99103
renderProps.formattedValue = String(
100104
axis.vertical
@@ -112,30 +116,34 @@ export default function Cursor({ primary }) {
112116
: latestValue
113117
)
114118
: ''
115-
)
119+
);
116120

117-
const lineStartX = Math.min(x1, x2)
118-
const lineStartY = Math.min(y1, y2)
119-
const lineEndX = Math.max(x1, x2)
120-
const lineEndY = Math.max(y1, y2)
121+
const lineStartX = Math.min(x1, x2);
122+
const lineStartY = Math.min(y1, y2);
123+
const lineEndX = Math.max(x1, x2);
124+
const lineEndY = Math.max(y1, y2);
121125
const bubbleX =
122126
axis.vertical && axis.RTL
123127
? lineEndX
124-
: x1 + (!axis.vertical ? (x2 - x1) / 2 : 0) + (!axis.vertical ? 1 : 0)
128+
: x1 + (!axis.vertical ? (x2 - x1) / 2 : 0) + (!axis.vertical ? 1 : 0);
125129
const bubbleY =
126130
!axis.vertical && axis.RTL
127131
? lineStartY
128-
: y1 + (axis.vertical ? (y2 - y1) / 2 : 0) + (axis.vertical ? 1 : 0)
132+
: y1 + (axis.vertical ? (y2 - y1) / 2 : 0) + (axis.vertical ? 1 : 0);
129133

130-
const lineHeight = Math.max(lineEndY - lineStartY, 0)
131-
const lineWidth = Math.max(lineEndX - lineStartX, 0)
134+
// if (!axis.vertical) {
135+
// console.log({ bubbleX, bubbleY });
136+
// }
132137

133-
let animateCoords
138+
const lineHeight = Math.max(lineEndY - lineStartY, 0);
139+
const lineWidth = Math.max(lineEndX - lineStartX, 0);
140+
141+
let animateCoords;
134142
if (previousShowRef.current === resolvedShow) {
135-
animateCoords = true
143+
animateCoords = true;
136144
}
137145

138-
let renderedChildren = render(renderProps)
146+
let renderedChildren = render(renderProps);
139147

140148
return (
141149
<div
@@ -196,5 +204,5 @@ export default function Cursor({ primary }) {
196204
</div>
197205
) : null}
198206
</div>
199-
)
207+
);
200208
}

0 commit comments

Comments
 (0)