Skip to content
WANCRAFT

G.27Data & Feedback

Interactive Chart Tooltip

A responsive SVG line chart with a tracking crosshair, spring-led tooltip and keyboard inspection.

ReactMotionSVGTailwindadvanced

Overview

Interactive Chart Tooltip is a dependency-light line/area chart that measures its container with ResizeObserver and redraws crisply at any width. A crosshair snaps to the nearest point, the selection dot and tooltip follow on a spring, and the tooltip reports the value with its change from the previous point (arrow, sign and colour). Smooth mode uses monotone cubic interpolation, so curves never overshoot the data. Keyboard users focus the chart and step through points with the arrow keys while a polite live region reads each value, and a data table is always present for screen readers — with an optional toggle to show it visually.

01Installation

Method 01 · Registry

CLI verified

Uses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/chart-tooltip.tsx and adds its dependencies.

npx shadcn@latest add https://wancraft.my/r/chart-tooltip.json

Method 02 · Copy and paste

  1. 1. Install the dependencies below.
  2. 2. Copy the source from the Code tab into components/ui/chart-tooltip.tsx.
  3. 3. Import it and adjust props.
npm install motion
Dependencies
motion
Peer
react ≥ 18, tailwindcss ≥ 4
Added
1 Oct 2026
Updated
—

02Usage

The default configuration. Change properties above to generate a tailored example in the Usage tab.

example.tsx
import { InteractiveChart, type ChartDatum } from "@/components/ui/chart-tooltip";

export function InteractiveChartExample() {
  const data: ChartDatum[] = [
    { label: "Jan", value: 18400 },
    { label: "Feb", value: 21250 },
    { label: "Mar", value: 19800 },
    { label: "Apr", value: 26100 },
    { label: "May", value: 24900 },
    { label: "Jun", value: 31200 },
  ];

  const currency = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0 });

  return (
    <InteractiveChart
      data={data}
      label="Monthly revenue"
      valueFormatter={(v) => currency.format(v)}
    />
  );
}

03Props

PropTypeDefaultDescription
data{ label: string; value: number }[]—Series in display order.
accentstring"#FF7448"Line, dot, area and focus colour.
curve"linear" | "smooth""smooth"Smooth uses monotone cubic interpolation (no overshoot).
showAreabooleantrueGradient fill under the line.
showGridbooleantrueHorizontal grid lines at nice tick values.
showDotsbooleanfalseMarker on every data point.
heightnumber260Plot height in px; width follows the container.
animatebooleantrueDraw the line in on mount (skipped with reduced motion).
showTableTogglebooleantrueButton that reveals the data table visually.
labelstring"Chart"Accessible name and table caption.
valueFormatter(value: number) => string—Formats values in the tooltip, table and announcements.
tickFormatter(value: number) => string—Formats y-axis ticks (defaults to compact notation).

04Features

  • 01ResizeObserver-driven width with nice y-axis ticks
  • 02Monotone smooth curve or straight segments
  • 03Crosshair, spring-animated dot and edge-aware tooltip
  • 04Change from the previous point with arrow, sign and colour
  • 05Re-renders only when the nearest point changes
motion · springmotion · tweeninput · pointerinput · hoverinput · keyboardinput · focus

05Accessibility

  • The plot is a focusable group; Left/Right step through points, Home/End jump to the ends, Escape clears.
  • Each selection is announced through a polite live region, e.g. “Apr: $26,100, up 31.8% from Mar”.
  • A full data table is always available to screen readers; the toggle (aria-expanded) shows it visually.
  • A visually hidden description summarises the range, lowest and highest values.
  • Change direction is shown with an arrow and sign, not colour alone; motion is disabled with prefers-reduced-motion.