-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bump): Add mesh to bump chart (#2496)
* add mes to bump chart * use margin from nivo core * pass active point ids state to custom layers * fix lockfile * remove unnecessary castings * set active point behaviour back to active series * change tooltipAnchor for useMesh * unify handler props * fix webpage example * upload lockfile
- Loading branch information
1 parent
3f090bc
commit 43db298
Showing
15 changed files
with
26,179 additions
and
34,297 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { MouseEvent } from 'react' | ||
import { createElement, memo, useCallback } from 'react' | ||
import { Margin } from '@nivo/core' | ||
import { useTooltip } from '@nivo/tooltip' | ||
import { Mesh as BaseMesh } from '@nivo/voronoi' | ||
import { | ||
BumpCommonProps, | ||
BumpDatum, | ||
BumpPoint, | ||
BumpPointMouseHandler, | ||
BumpSerieExtraProps, | ||
} from './types' | ||
|
||
interface MeshProps<Datum extends BumpDatum, ExtraProps extends BumpSerieExtraProps> { | ||
points: BumpPoint<Datum, ExtraProps>[] | ||
width: number | ||
height: number | ||
margin: Margin | ||
setActivePointIds: (ids: string[]) => void | ||
setActiveSerieIds: (ids: string[]) => void | ||
onMouseEnter?: BumpPointMouseHandler<Datum, ExtraProps> | ||
onMouseMove?: BumpPointMouseHandler<Datum, ExtraProps> | ||
onMouseLeave?: BumpPointMouseHandler<Datum, ExtraProps> | ||
onClick?: BumpPointMouseHandler<Datum, ExtraProps> | ||
tooltip: BumpCommonProps<Datum, ExtraProps>['pointTooltip'] | ||
debug: boolean | ||
} | ||
|
||
const InnerMesh = <Datum extends BumpDatum, ExtraProps extends BumpSerieExtraProps>({ | ||
points, | ||
width, | ||
height, | ||
margin, | ||
setActivePointIds, | ||
setActiveSerieIds, | ||
onMouseEnter, | ||
onMouseMove, | ||
onMouseLeave, | ||
onClick, | ||
tooltip, | ||
debug, | ||
}: MeshProps<Datum, ExtraProps>) => { | ||
const { showTooltipAt, hideTooltip } = useTooltip() | ||
|
||
const handleMouseEnter = useCallback( | ||
(point: BumpPoint<Datum, ExtraProps>, event: MouseEvent) => { | ||
showTooltipAt( | ||
createElement(tooltip, { point }), | ||
[point.x + margin.left, point.y ?? 0 + margin.top], | ||
'top' | ||
) | ||
setActivePointIds([point.id]) | ||
setActiveSerieIds([point.serie.id]) | ||
onMouseEnter && onMouseEnter(point, event) | ||
}, | ||
[ | ||
showTooltipAt, | ||
tooltip, | ||
margin.left, | ||
margin.top, | ||
setActivePointIds, | ||
setActiveSerieIds, | ||
onMouseEnter, | ||
] | ||
) | ||
|
||
const handleMouseMove = useCallback( | ||
(point: BumpPoint<Datum, ExtraProps>, event: MouseEvent) => { | ||
showTooltipAt( | ||
createElement(tooltip, { point }), | ||
[point.x + margin.left, point.y ?? 0 + margin.top], | ||
'top' | ||
) | ||
setActivePointIds([point.id]) | ||
setActiveSerieIds([point.serie.id]) | ||
onMouseMove && onMouseMove(point, event) | ||
}, | ||
[ | ||
showTooltipAt, | ||
tooltip, | ||
margin.left, | ||
margin.top, | ||
setActivePointIds, | ||
setActiveSerieIds, | ||
onMouseMove, | ||
] | ||
) | ||
|
||
const handleMouseLeave = useCallback( | ||
(point: BumpPoint<Datum, ExtraProps>, event: MouseEvent) => { | ||
hideTooltip() | ||
setActivePointIds([]) | ||
setActiveSerieIds([]) | ||
onMouseLeave && onMouseLeave(point, event) | ||
}, | ||
[hideTooltip, onMouseLeave, setActivePointIds, setActiveSerieIds] | ||
) | ||
|
||
const handleClick = useCallback( | ||
(point: BumpPoint<Datum, ExtraProps>, event: MouseEvent) => { | ||
onClick && onClick(point, event) | ||
}, | ||
[onClick] | ||
) | ||
|
||
return ( | ||
<BaseMesh | ||
nodes={points} | ||
width={width} | ||
height={height} | ||
onMouseEnter={handleMouseEnter} | ||
onMouseMove={handleMouseMove} | ||
onMouseLeave={handleMouseLeave} | ||
onClick={handleClick} | ||
debug={debug} | ||
/> | ||
) | ||
} | ||
|
||
export const Mesh = memo(InnerMesh) as typeof InnerMesh |
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.