Skip to content

Commit a1a529c

Browse files
authoredJun 17, 2022
fix(tooltip): fix positioning when charts are rendered inside an scaled div (#2034)
* Fix tooltip positioning when charts are rendered inside an scaled div In a normal situation mouse enter / mouse leave events capture the position ok. But when the chart is inside a scaled element with a CSS transform like: `transform: scale(2);` tooltip are not positioned ok. Comparing original width `box.width` agains scaled width give us the scaling factor to calculate ok mouse position. * Fix tooltip positioning on line slice tooltips When a Nivo chart is inside an scaled div with something like `transform: scale(2)` for example tooltips inside slides are not positoned correctly. Taking into accoun `offsetWidth` fix the issue
1 parent 5985ab4 commit a1a529c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed
 

‎packages/core/src/lib/interactivity/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ export * from './detect'
1212
export const getRelativeCursor = (el, event) => {
1313
const { clientX, clientY } = event
1414
const bounds = el.getBoundingClientRect()
15+
const box = el.getBBox()
1516

16-
return [clientX - bounds.left, clientY - bounds.top]
17+
// In a normal situation mouse enter / mouse leave events
18+
// capture the position ok. But when the chart is inside a scaled
19+
// element with a CSS transform like: `transform: scale(2);`
20+
// tooltip are not positioned ok.
21+
// Comparing original width `box.width` agains scaled width give us the
22+
// scaling factor to calculate ok mouse position
23+
const scaling = box.width === bounds.width ? 1 : box.width / bounds.width
24+
return [(clientX - bounds.left) * scaling, (clientY - bounds.top) * scaling]
1725
}

‎packages/tooltip/src/hooks.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ export const useTooltipHandlers = (container: MutableRefObject<HTMLDivElement>)
2626
const showTooltipFromEvent: TooltipActionsContextData['showTooltipFromEvent'] = useCallback(
2727
(content: JSX.Element, event: MouseEvent, anchor: TooltipAnchor = 'top') => {
2828
const bounds = container.current.getBoundingClientRect()
29-
const x = event.clientX - bounds.left
30-
const y = event.clientY - bounds.top
29+
const offsetWidth = container.current.offsetWidth
30+
// In a normal situation mouse enter / mouse leave events
31+
// capture the position ok. But when the chart is inside a scaled
32+
// element with a CSS transform like: `transform: scale(2);`
33+
// tooltip are not positioned ok.
34+
// Comparing original width `offsetWidth` agains scaled
35+
// width give us the scaling factor to calculate
36+
// ok mouse position
37+
const scaling = offsetWidth === bounds.width ? 1 : offsetWidth / bounds.width
38+
const x = (event.clientX - bounds.left) * scaling
39+
const y = (event.clientY - bounds.top) * scaling
3140

3241
if (anchor === 'left' || anchor === 'right') {
3342
if (x < bounds.width / 2) anchor = 'right'

0 commit comments

Comments
 (0)
Please sign in to comment.