|
| 1 | +import Plotly from "plotly.js-dist"; |
| 2 | +import { useCallback, useRef, useState } from "react"; |
| 3 | + |
| 4 | +import { chartTooltipLines } from "./lines"; |
| 5 | + |
| 6 | +import type { ChartTooltipHoverPoint } from "./lines"; |
| 7 | + |
| 8 | +import { |
| 9 | + Tooltip, |
| 10 | + TooltipContent, |
| 11 | + TooltipProvider, |
| 12 | + TooltipTrigger, |
| 13 | +} from "@/components/ui/tooltip"; |
| 14 | + |
| 15 | +export { chartTooltipLines } from "./lines"; |
| 16 | +export type { ChartTooltipHoverPoint } from "./lines"; |
| 17 | + |
| 18 | +/** How far (px) from a point Plotly still reports a hover — larger = stickier */ |
| 19 | +const HOVER_DISTANCE_PX = 40; |
| 20 | +/** Grace period before hiding, so the tooltip survives gaps between points */ |
| 21 | +const HIDE_GRACE_MS = 150; |
| 22 | +/** Distance (px) between the anchor position and the tooltip arrow */ |
| 23 | +const TOOLTIP_OFFSET_PX = 12; |
| 24 | + |
| 25 | +export interface ChartTooltipAnchor { |
| 26 | + left: number; |
| 27 | + top: number; |
| 28 | + lines: string[]; |
| 29 | +} |
| 30 | + |
| 31 | +interface ChartTooltipEmitter { |
| 32 | + on: (event: string, handler: (data: ChartTooltipHoverEvent) => void) => void; |
| 33 | +} |
| 34 | + |
| 35 | +interface ChartTooltipHoverEvent { |
| 36 | + points?: ChartTooltipHoverPoint[]; |
| 37 | + event?: MouseEvent; |
| 38 | +} |
| 39 | + |
| 40 | +export interface ChartTooltipProps { |
| 41 | + /** Anchor position (relative to the chart container) and content lines */ |
| 42 | + anchor: ChartTooltipAnchor | null; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * The design-system tooltip anchored at a chart position. Render inside a |
| 47 | + * `relative` chart container; drive it with `useChartTooltip`. |
| 48 | + */ |
| 49 | +export function ChartTooltip({ anchor }: ChartTooltipProps) { |
| 50 | + if (!anchor) return null; |
| 51 | + return ( |
| 52 | + <TooltipProvider> |
| 53 | + {/* Re-key per anchor so Radix repositions to the moved trigger */} |
| 54 | + <Tooltip open key={`${anchor.left},${anchor.top}`}> |
| 55 | + <TooltipTrigger asChild> |
| 56 | + <span |
| 57 | + aria-hidden |
| 58 | + data-slot="chart-tooltip-anchor" |
| 59 | + className="pointer-events-none absolute size-0" |
| 60 | + style={{ left: anchor.left, top: anchor.top }} |
| 61 | + /> |
| 62 | + </TooltipTrigger> |
| 63 | + <TooltipContent |
| 64 | + side="top" |
| 65 | + sideOffset={TOOLTIP_OFFSET_PX} |
| 66 | + className="pointer-events-none" |
| 67 | + > |
| 68 | + {anchor.lines.map((line, index) => ( |
| 69 | + <div key={`${index}-${line}`}>{line}</div> |
| 70 | + ))} |
| 71 | + </TooltipContent> |
| 72 | + </Tooltip> |
| 73 | + </TooltipProvider> |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +export interface UseChartTooltipOptions { |
| 78 | + /** Axis label used for the default x line (falls back to "X") */ |
| 79 | + xLabel?: string; |
| 80 | + /** Axis label used for the default y line (falls back to "Y") */ |
| 81 | + yLabel?: string; |
| 82 | + /** Override the default line builder entirely */ |
| 83 | + getLines?: (points: ChartTooltipHoverPoint[]) => string[]; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Drives the design-system tooltip from Plotly hover events. |
| 88 | + * |
| 89 | + * ```tsx |
| 90 | + * const { bindTooltip, tooltipElement } = useChartTooltip({ xLabel, yLabel }); |
| 91 | + * // after Plotly.newPlot(...): set trace hoverinfo to "none", then |
| 92 | + * bindTooltip(plotRef.current); |
| 93 | + * // in the (relative-positioned) container: |
| 94 | + * {tooltipElement} |
| 95 | + * ``` |
| 96 | + */ |
| 97 | +export function useChartTooltip(options: UseChartTooltipOptions = {}) { |
| 98 | + const [anchor, setAnchor] = useState<ChartTooltipAnchor | null>(null); |
| 99 | + const hideTimerRef = useRef<ReturnType<typeof setTimeout>>(undefined); |
| 100 | + const optionsRef = useRef(options); |
| 101 | + optionsRef.current = options; |
| 102 | + |
| 103 | + const bindTooltip = useCallback((plotDiv: HTMLElement | null) => { |
| 104 | + if (!plotDiv) return; |
| 105 | + setAnchor(null); |
| 106 | + |
| 107 | + // A larger hover radius makes the tooltip easier to acquire and keep |
| 108 | + void Plotly.relayout(plotDiv, { hoverdistance: HOVER_DISTANCE_PX }); |
| 109 | + |
| 110 | + const emitter = plotDiv as unknown as ChartTooltipEmitter; |
| 111 | + |
| 112 | + emitter.on("plotly_hover", (eventData) => { |
| 113 | + const points = eventData.points ?? []; |
| 114 | + const { getLines, xLabel, yLabel } = optionsRef.current; |
| 115 | + const lines = getLines |
| 116 | + ? getLines(points) |
| 117 | + : chartTooltipLines(points, { xLabel, yLabel }); |
| 118 | + if (lines.length === 0) return; |
| 119 | + |
| 120 | + clearTimeout(hideTimerRef.current); |
| 121 | + const rect = plotDiv.getBoundingClientRect(); |
| 122 | + const mouse = eventData.event; |
| 123 | + setAnchor({ |
| 124 | + left: mouse ? mouse.clientX - rect.left : 0, |
| 125 | + top: mouse ? mouse.clientY - rect.top : 0, |
| 126 | + lines, |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + emitter.on("plotly_unhover", () => { |
| 131 | + clearTimeout(hideTimerRef.current); |
| 132 | + hideTimerRef.current = setTimeout(() => setAnchor(null), HIDE_GRACE_MS); |
| 133 | + }); |
| 134 | + }, []); |
| 135 | + |
| 136 | + return { |
| 137 | + bindTooltip, |
| 138 | + tooltipElement: <ChartTooltip anchor={anchor} />, |
| 139 | + }; |
| 140 | +} |
0 commit comments