-
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.
- Loading branch information
Showing
31 changed files
with
923 additions
and
391 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {CHARTKIT_SCROLLABLE_NODE_CLASSNAME} from './common'; | ||
export * from './widget-data'; |
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,5 @@ | ||
export const TooltipDataChunkType = { | ||
BAR_X: 0, | ||
PIE: 1, | ||
SCATTER: 2, | ||
} as const; |
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
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
151 changes: 151 additions & 0 deletions
151
src/plugins/d3/renderer/components/Tooltip/TooltipArea.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,151 @@ | ||
import React from 'react'; | ||
import throttle from 'lodash/throttle'; | ||
import {Delaunay, bisector, pointer, sort} from 'd3'; | ||
|
||
import type { | ||
ChartScale, | ||
OnSeriesMouseMove, | ||
OnSeriesMouseLeave, | ||
ShapeData, | ||
PreparedBarXData, | ||
PreparedScatterData, | ||
} from '../../hooks'; | ||
|
||
type Args = { | ||
boundsWidth: number; | ||
boundsHeight: number; | ||
offsetTop: number; | ||
offsetLeft: number; | ||
shapesData: ShapeData[]; | ||
svgContainer: SVGSVGElement | null; | ||
xScale?: ChartScale; | ||
onSeriesMouseMove?: OnSeriesMouseMove; | ||
onSeriesMouseLeave?: OnSeriesMouseLeave; | ||
}; | ||
|
||
type CalculationType = 'x-primary' | 'delaunay'; | ||
|
||
// https://d3js.org/d3-selection/joining#selection_data | ||
const isNodeContainsData = (node?: Element): node is Element & {__data__: ShapeData} => { | ||
return Boolean(node && '__data__' in node); | ||
}; | ||
|
||
const getCalculationType = (shapesData: ShapeData[]): CalculationType => { | ||
if (shapesData.every((d) => d.series.type === 'bar-x')) { | ||
return 'x-primary'; | ||
} | ||
|
||
if (shapesData.every((d) => d.series.type === 'scatter')) { | ||
return 'delaunay'; | ||
} | ||
|
||
throw new Error('This type of series does not supported for tooltip yet'); | ||
}; | ||
|
||
export const TooltipArea = (args: Args) => { | ||
const { | ||
boundsWidth, | ||
boundsHeight, | ||
offsetTop, | ||
offsetLeft, | ||
shapesData, | ||
svgContainer, | ||
xScale, | ||
onSeriesMouseMove, | ||
onSeriesMouseLeave, | ||
} = 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]); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const delaunay = React.useMemo(() => { | ||
return calculationType === 'delaunay' | ||
? new Delaunay( | ||
new Float64Array( | ||
(shapesData as PreparedScatterData[]).map((d) => [d.cx, d.cy]).flat(), | ||
), | ||
) | ||
: undefined; | ||
}, [shapesData, calculationType]); | ||
|
||
const handleXprimaryMouseMove: React.MouseEventHandler<SVGRectElement> = (e) => { | ||
const {left, top} = rectRef.current?.getBoundingClientRect() || {left: 0, top: 0}; | ||
const [x, y] = pointer(e, svgContainer); | ||
const isXLinearOrTimeScale = xScale && 'invert' in xScale; | ||
const xPosition = x - left - (isXLinearOrTimeScale ? 0 : offsetLeft); | ||
const xDataIndex = bisector((d) => d).center(xData, xPosition); | ||
const xNodes = Array.from( | ||
rectRef.current?.parentElement?.querySelectorAll(`[x="${xData[xDataIndex]}"]`) || [], | ||
); | ||
|
||
if (xNodes.length === 1 && isNodeContainsData(xNodes[0])) { | ||
onSeriesMouseMove?.({ | ||
hovered: [xNodes[0].__data__], | ||
pointerPosition: [x - offsetLeft, y - offsetTop], | ||
}); | ||
} else if (xNodes.length > 1 && xNodes.every(isNodeContainsData)) { | ||
const yData = xNodes.map((node) => (node.__data__ as PreparedBarXData).y); | ||
const yPosition = y - top; | ||
const yDataIndex = bisector((d) => d).center(yData, yPosition); | ||
|
||
if (xNodes[yDataIndex]) { | ||
xNodes.reverse(); | ||
onSeriesMouseMove?.({ | ||
hovered: [xNodes[yDataIndex].__data__], | ||
pointerPosition: [x - offsetLeft, y - offsetTop], | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const handleDelaunayMouseMove: React.MouseEventHandler<SVGRectElement> = (e) => { | ||
console.log(document.elementFromPoint(e.clientX, e.clientY)); | ||
// const {left, top} = rectRef.current?.getBoundingClientRect() || {left: 0, top: 0}; | ||
// const [x, y] = pointer(e, svgContainer); | ||
// const dataIndex = delaunay?.find(x - left, y - top, prevIndex) || -1; | ||
// if (shapesData[dataIndex]) { | ||
// prevIndex = dataIndex; | ||
// // console.log(shapesData[dataIndex]); | ||
// onSeriesMouseMove?.({ | ||
// hovered: [shapesData[dataIndex]], | ||
// pointerPosition: [x - offsetLeft, y - offsetTop], | ||
// }); | ||
// } | ||
}; | ||
|
||
const handleMouseMove: React.MouseEventHandler<SVGRectElement> = (e) => { | ||
switch (calculationType) { | ||
case 'x-primary': { | ||
handleXprimaryMouseMove(e); | ||
return; | ||
} | ||
case 'delaunay': { | ||
handleDelaunayMouseMove(e); | ||
} | ||
} | ||
}; | ||
|
||
const throttledHandleMouseMove = throttle(handleMouseMove, 50); | ||
|
||
const handleMouseLeave = () => { | ||
throttledHandleMouseMove.cancel(); | ||
onSeriesMouseLeave?.(); | ||
}; | ||
|
||
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
Oops, something went wrong.