Chart Tooltip
Shared tooltip card and positioning helpers used by the visx chart components.
14:00
Measured1.7
Projected1.7
Usage
import {
ChartTooltipCard,
clampTooltipPosition,
useMeasuredSize,
type ChartTooltipRow,
} from '@epilot/spark-ui/chart'function HoverTip({
anchorX,
anchorY,
width,
height,
rows,
}: {
anchorX: number
anchorY: number
width: number
height: number
rows: ChartTooltipRow[]
}) {
const [tipRef, tipSize] = useMeasuredSize<HTMLDivElement>()
const pos = clampTooltipPosition({
anchorX,
anchorY,
width,
height,
tipWidth: tipSize.width,
tipHeight: tipSize.height,
placement: 'beside',
})
return (
<div
ref={tipRef}
className="pointer-events-none absolute"
style={{ left: pos.left, top: pos.top }}
>
<ChartTooltipCard label="14:00" rows={rows} />
</div>
)
}These helpers keep every Spark chart tooltip looking and behaving the same.
Line Chart, Bar Chart,
and the HEMS history charts already use them via @visx/tooltip. Reach for the
primitives when building a custom visx surface that should match.
API Reference
ChartTooltipCard
| Prop | Type | Default | Description |
|---|---|---|---|
label | ReactNode | — | Optional header (usually the hovered time or category). |
rows | ChartTooltipRow[] | — | One coloured-dot row per series. Empty array renders nothing. |
className | string | — | Merges onto the card. |
ChartTooltipRow
| Field | Type | Default | Description |
|---|---|---|---|
key | string | — | React key. |
label | ReactNode | — | Series name. |
color | string | — | CSS color for the swatch (for example var(--orange-a9)). |
value | ReactNode | — | Formatted value on the right. |
marker | "dot" | "dashed-line" | "dot" | dashed-line for dashed series so same-color rows stay distinguishable. |
useMeasuredSize
Returns [ref, { width, height }]. Attach ref to an element that stays
mounted (the tooltip wrapper can be opacity-hidden) so ResizeObserver can
measure it before the first paint of an open tip.
clampTooltipPosition
| Arg | Type | Default | Description |
|---|---|---|---|
anchorX | number | — | Chart-local x the tip should sit near. |
anchorY | number | — | Chart-local y. |
width | number | — | Chart bounds width. |
height | number | — | Chart bounds height. |
tipWidth | number | — | Measured tooltip width. |
tipHeight | number | — | Measured tooltip height. |
gap | number | 12 | Space between the anchor and the tip. |
pad | number | 8 | Minimum inset from every chart edge. |
placement | "above" | "beside" | "above" | above flips below if needed (bars). beside prefers the right side (lines). |
Returns { left, top } in chart-local coordinates. The tip stays fully inside
the chart bounds.
Accessibility
ChartTooltipCardis presentational. Announce values elsewhere if they are required reading; do not make a hover tip the only path to a number.- Keep the measured wrapper in the DOM while closed so size stays accurate when the tip opens.