Skip to content

Commit

Permalink
Add option for positioning tooltip for LineCanvas
Browse files Browse the repository at this point in the history
* Defaults to the existing behavior to be at the cursor, but optionally can be placed at the point that the data in the tooltip is representing
  • Loading branch information
bdknox committed Jun 5, 2023
1 parent 31ce26a commit cb42971
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
24 changes: 21 additions & 3 deletions packages/line/src/LineCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const LineCanvas = ({
onMouseLeave,
onClick,
tooltip,
tooltipPosition = LineCanvasDefaultProps.tooltipPosition,

canvasRef,
}) => {
Expand Down Expand Up @@ -276,20 +277,37 @@ const LineCanvas = ({
[canvasEl, margin, innerWidth, innerHeight, delaunay]
)

const { showTooltipFromEvent, hideTooltip } = useTooltip()
const { showTooltipAt, showTooltipFromEvent, hideTooltip } = useTooltip()

const handleMouseHover = useCallback(
event => {
const point = getPointFromMouseEvent(event)
setCurrentPoint(point)

if (point) {
showTooltipFromEvent(createElement(tooltip, { point }), event)
if (tooltipPosition === 'point') {
showTooltipAt(
createElement(tooltip, { point }),
[point.x + margin.left, point.y + margin.top],
'top'
)
} else {
showTooltipFromEvent(createElement(tooltip, { point }), event)
}
} else {
hideTooltip()
}
},
[getPointFromMouseEvent, setCurrentPoint, showTooltipFromEvent, hideTooltip, tooltip]
[
getPointFromMouseEvent,
setCurrentPoint,
showTooltipAt,
showTooltipFromEvent,
hideTooltip,
tooltip,
tooltipPosition,
margin,
]
)

const handleMouseLeave = useCallback(
Expand Down
1 change: 1 addition & 0 deletions packages/line/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,5 @@ export const LineDefaultProps = {
export const LineCanvasDefaultProps = {
...commonDefaultProps,
pixelRatio: typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1,
tooltipPostion: 'cursor',
}
2 changes: 2 additions & 0 deletions website/src/data/components/line/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ export default {

enableCrosshair: true,
crosshairType: 'bottom-left',

tooltipPosition: 'cursor',
}
26 changes: 24 additions & 2 deletions website/src/data/components/line/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const props: ChartProperty[] = [
If you use a time scale, you must provide a time format
as values are converted to Date objects.
Under the hood, nivo uses [d3-format](https://github.com/d3/d3-format),
please have a look at it for available formats, you can also pass a function
which will receive the raw value and should return the formatted one.
Expand Down Expand Up @@ -163,7 +163,7 @@ const props: ChartProperty[] = [
If you use a time scale, you must provide a time format
as values are converted to Date objects.
Under the hood, nivo uses [d3-format](https://github.com/d3/d3-format),
please have a look at it for available formats, you can also pass a function
which will receive the raw value and should return the formatted one.
Expand Down Expand Up @@ -446,6 +446,28 @@ const props: ChartProperty[] = [
type: 'Function',
required: false,
},
{
key: 'tooltipPosition',
flavors: ['canvas'],
group: 'Interactivity',
help: `Tooltip position when hovering`,
type: 'string',
required: false,
defaultValue: defaults.tooltipPosition,
control: {
type: 'choices',
choices: [
{
label: 'cursor',
value: 'cursor',
},
{
label: 'point',
value: 'point',
},
],
},
},
{
key: 'enableSlices',
group: 'Interactivity',
Expand Down

0 comments on commit cb42971

Please sign in to comment.