-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(D3 plugin): move tooltip management to d3 events (#300)
* feat(D3 plugin): move tooltip management to d3 events * fix: TooltipTriggerArea fixes * fix: add window page offset * fix: remove TooltipDataChunkType * fix: fix d3 data management * fix: other review fixes * fix: remove scatter stroke on hover * fix: remove redundant comment
- Loading branch information
Showing
31 changed files
with
1,002 additions
and
482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/plugins/d3/renderer/components/Tooltip/TooltipTriggerArea.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import React from 'react'; | ||
import throttle from 'lodash/throttle'; | ||
import {bisector, pointer, sort} from 'd3'; | ||
import type {Dispatch} from 'd3'; | ||
|
||
import type {ShapeData, PreparedBarXData, PointerPosition} from '../../hooks'; | ||
import {extractD3DataFromNode, isNodeContainsD3Data} from '../../utils'; | ||
import type {NodeWithD3Data} from '../../utils'; | ||
|
||
const THROTTLE_DELAY = 50; | ||
|
||
type Args = { | ||
boundsWidth: number; | ||
boundsHeight: number; | ||
dispatcher: Dispatch<object>; | ||
offsetTop: number; | ||
offsetLeft: number; | ||
shapesData: ShapeData[]; | ||
svgContainer: SVGSVGElement | null; | ||
}; | ||
|
||
type CalculationType = 'x-primary' | 'none'; | ||
|
||
const isNodeContainsData = (node?: Element): node is NodeWithD3Data<ShapeData> => { | ||
return isNodeContainsD3Data(node); | ||
}; | ||
|
||
const getCalculationType = (shapesData: ShapeData[]): CalculationType => { | ||
if (shapesData.every((d) => d.series.type === 'bar-x')) { | ||
return 'x-primary'; | ||
} | ||
|
||
return 'none'; | ||
}; | ||
|
||
export const TooltipTriggerArea = (args: Args) => { | ||
const {boundsWidth, boundsHeight, dispatcher, offsetTop, offsetLeft, shapesData, svgContainer} = | ||
args; | ||
const rectRef = React.useRef<SVGRectElement>(null); | ||
const calculationType = React.useMemo(() => { | ||
return getCalculationType(shapesData); | ||
}, [shapesData]); | ||
const xData = React.useMemo(() => { | ||
return calculationType === 'x-primary' | ||
? sort(new Set((shapesData as PreparedBarXData[]).map((d) => d.x))) | ||
: []; | ||
}, [shapesData, calculationType]); | ||
|
||
const handleXprimaryMouseMove: React.MouseEventHandler<SVGRectElement> = (e) => { | ||
const {left, top} = rectRef.current?.getBoundingClientRect() || {left: 0, top: 0}; | ||
const [pointerX, pointerY] = pointer(e, svgContainer); | ||
const barWidthOffset = (shapesData[0] as PreparedBarXData).width / 2; | ||
const xPosition = pointerX - left - barWidthOffset - window.pageXOffset; | ||
const xDataIndex = bisector((d) => d).center(xData, xPosition); | ||
const xNodes = Array.from( | ||
rectRef.current?.parentElement?.querySelectorAll(`[x="${xData[xDataIndex]}"]`) || [], | ||
); | ||
|
||
let hoverShapeData: ShapeData[] | undefined; | ||
|
||
if (xNodes.length === 1 && isNodeContainsData(xNodes[0])) { | ||
hoverShapeData = [extractD3DataFromNode(xNodes[0])]; | ||
} else if (xNodes.length > 1 && xNodes.every(isNodeContainsData)) { | ||
const yPosition = pointerY - top - window.pageYOffset; | ||
const xyNode = xNodes.find((node, i) => { | ||
const {y, height} = extractD3DataFromNode(node) as PreparedBarXData; | ||
if (i === xNodes.length - 1) { | ||
return yPosition <= y + height; | ||
} | ||
return yPosition >= y && yPosition <= y + height; | ||
}); | ||
|
||
if (xyNode) { | ||
hoverShapeData = [extractD3DataFromNode(xyNode)]; | ||
} | ||
} | ||
|
||
if (hoverShapeData) { | ||
const position: PointerPosition = [pointerX - offsetLeft, pointerY - offsetTop]; | ||
dispatcher.call('hover-shape', e.target, hoverShapeData, position); | ||
} | ||
}; | ||
|
||
const handleMouseMove: React.MouseEventHandler<SVGRectElement> = (e) => { | ||
switch (calculationType) { | ||
case 'x-primary': { | ||
handleXprimaryMouseMove(e); | ||
return; | ||
} | ||
} | ||
}; | ||
|
||
const throttledHandleMouseMove = throttle(handleMouseMove, THROTTLE_DELAY); | ||
|
||
const handleMouseLeave = () => { | ||
throttledHandleMouseMove.cancel(); | ||
dispatcher.call('hover-shape', {}, undefined); | ||
}; | ||
|
||
return ( | ||
<rect | ||
ref={rectRef} | ||
width={boundsWidth} | ||
height={boundsHeight} | ||
fill="transparent" | ||
onMouseMove={throttledHandleMouseMove} | ||
onMouseLeave={handleMouseLeave} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './axis'; | ||
export * from './legend'; | ||
export * from './series-options'; |
Oops, something went wrong.