Skip to content
WANCRAFT

G.117Data & Feedback

Sparkline

A word-sized chart for tables and text: line, area or bars, coloured by trend, with a computed summary and nearest-value tooltip.

NewReactSVGTailwindbasic

Overview

Sparkline draws a series at the height of the text around it, so it sits in a table cell or the middle of a sentence. Choose a monotone line (smooth without overshooting the data), a soft area or bars; bounds are automatic or fixed. It colours itself by trend, adds an arrow and the change when asked, and works out a text summary for screen readers such as “Trend up 12% over 30 days”. A dot marks the latest value, hovering, tapping or arrowing through it shows the nearest value, and it draws in once it scrolls into view. The chart stretches to any width without distorting strokes or the dot.

01Installation

Method 01 · Registry

CLI verified

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

npx shadcn@latest add https://wancraft.my/r/sparkline.json

Method 02 · Copy and paste

  1. 1. No extra dependencies — React and Tailwind CSS only.
  2. 2. Copy the source from the Code tab into components/ui/sparkline.tsx.
  3. 3. Import it and adjust props.
Dependencies
None
Peer
react ≥ 18, tailwindcss ≥ 4
Added
13 Oct 2026
Updated
—

02Usage

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

example.tsx
import { Sparkline } from "@/components/ui/sparkline";

export function SparklineExample() {
  const visits = [212, 230, 225, 241, 238, 260, 255, 281, 276, 302, 298, 317];

  return (
    <p className="text-sm">
      <Sparkline data={visits} label="Visits" period="weeks" />
    </p>
  );
}

03Props

PropTypeDefaultDescription
datanumber[]—Required. Values, oldest first.
variant"line" | "area" | "bar""line"Chart type.
min / maxnumber—Fixed bounds; automatic when omitted.
curve"smooth" | "linear""smooth"Monotone curve or straight segments.
widthnumber | string"6em"CSS width; "100%" makes it responsive.
heightnumber | string"1.25em"CSS height; the default matches the text around it.
strokeWidthnumber1.5Line width in px (never scaled).
showDotbooleantrueDot on the latest value.
showValuebooleanfalseLatest value, arrow and change after the chart.
colorBy"trend" | "accent""trend"Colour by direction or always the accent.
accent / upColor / downColorstring"#FF7448" / "#2FBF71" / "#E5484D"Colours.
animatebooleantrueDraw in when scrolled into view.
durationnumber900Draw-in length in ms.
interactivebooleantrueHover, tap and keyboard tooltip.
labelstring—Metric name used in the accessible summary.
periodstring"days"What a point represents (plural), e.g. “over 30 days”.
labelsstring[]—Per-point labels (e.g. dates) for the tooltip.
locale / formatValuestring / (v: number) => string"en-US"Number formatting.

04Features

  • 01Line, area and bar variants at text height
  • 02Monotone smoothing that never overshoots the data
  • 03Automatic or fixed bounds, responsive to any width without distorting strokes
  • 04Trend colour plus arrow and change text, never colour alone
  • 05Nearest-value tooltip on hover, tap and keyboard
  • 06Draws in once when it scrolls into view
motion · tweeninput · hoverinput · focusinput · keyboardinput · viewport

05Accessibility

  • The chart is role="img" with a computed summary, e.g. “Visits: Trend up 50% over 12 weeks, latest 317, range 212 to 317”.
  • It is focusable: arrow keys, Home and End move through the points, Escape hides the tooltip, and each step is announced politely.
  • Direction is shown by an arrow and percentage as well as colour; the trend colour is deepened on light backgrounds.
  • With prefers-reduced-motion it appears fully drawn, without animation.