import * as React from "react" import { cn } from "../lib/utils" export { useElementWidth } from "../hooks/use-element-width" export interface HoverState { readonly item: T /** Якорь подсказки относительно контейнера графика. */ readonly x: number readonly y: number } /** Одна подсказка на весь график: координаты считаются от контейнера с `relative`. */ export function useChartHover() { const [hover, setHover] = React.useState | null>(null) const bind = React.useCallback((item: T) => { const move = (e: React.MouseEvent) => { const box = (e.currentTarget as Element).closest("[data-chart-root]")?.getBoundingClientRect() if (box) setHover({ item, x: e.clientX - box.left, y: e.clientY - box.top }) } return { onMouseEnter: move, onMouseMove: move, onMouseLeave: () => setHover(null) } }, []) return { hover, bind, clear: () => setHover(null) } } /** Плашка подсказки: справа от курсора, у правого края — слева. */ export function ChartTooltip({ x, y, width, className, children, }: { x: number y: number width: number className?: string children: React.ReactNode }) { const flip = x > width * 0.6 return (
{children}
) } /** Строка подсказки: цветная метка, подпись, значение справа. */ export function TooltipRow({ color, label, value }: { color?: string; label: React.ReactNode; value: React.ReactNode }) { return (
{color && } {label} {value}
) } /** Легенда: всегда при двух и более сериях. */ export function ChartLegend({ items, className, }: { items: readonly { label: string; color: string; value?: React.ReactNode }[] className?: string }) { return (
    {items.map((item) => (
  • {item.label} {item.value !== undefined && {item.value}}
  • ))}
) }