Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add option for positioning tooltip for LineCanvas #2357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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