# Sparkline

> An ultra-compact inline trendline with magnetic scrubber and auto-trend color for table cells and stat cards.

Full chart libraries take up 200px of height, require large bundles, and break layout when dropped into table rows or compact metric strips. Sparkline is an ultra-lightweight hairline trendline that fits in a single line. It computes smooth cubic bezier curves across any container width with zero layout shift, renders an optional delicate gradient wash, and provides an interactive magnetic scrubber that snaps to data points with micro-tooltip readouts on hover. Dynamic auto-trend colors emerald for gains or rose for dips automatically.

## Install

```bash
npx shadcn@latest add @hairline/sparkline
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/sparkline
- Registry item (source): https://hairlineui.com/r/sparkline.json

## Usage

```tsx
import { Sparkline } from "@/components/sparkline";

<Sparkline
  data={[12, 14, 18, 16, 22, 28, 24, 32]}
  height={28}
  variant="area"
  prefix="$"
/>
```

## Table row with 14-day revenue trend

Embed Sparkline directly inside table cells alongside row data. It scales to column width without triggering cell height shifts.

```tsx
// app/customers/customer-row.tsx
"use client";

import { Sparkline } from "@/components/sparkline";

export function CustomerTableRow({ customer }: { customer: any }) {
  return (
    <tr className="border-b border-border text-sm">
      <td className="py-3 px-4 font-medium text-foreground">{customer.name}</td>
      <td className="py-3 px-4 text-muted-foreground">{customer.plan}</td>
      <td className="py-3 px-4 w-32">
        <Sparkline
          data={customer.history}
          height={24}
          variant="area"
          prefix="$"
        />
      </td>
      <td className="py-3 px-4 text-right font-mono text-foreground font-medium">
        ${customer.mrr.toLocaleString()}
      </td>
    </tr>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `data` | `(number \| { value: number; date?: string; label?: string })[]` | – | Array of data points or point objects with value and date. |
| `width` | `number \| string` | `"100%"` | Component width (can be percentage or pixel number). |
| `height` | `number` | `32` | Component height in pixels. |
| `color` | `"auto" \| "primary" \| "muted" \| string` | `"auto"` | Color scheme: auto (green/red based on trend), primary, or custom class. |
| `variant` | `"line" \| "area" \| "bars"` | `"area"` | Visual rendering style: line, area gradient, or bars. |
| `showScrubber` | `boolean` | `true` | Enables magnetic hover scrubber and micro-tooltip. |
| `showLastPoint` | `boolean` | `true` | Displays persistent anchor dot on the latest data point. |
| `strokeWidth` | `number` | `1.35` | Hairline stroke width in pixels. |
| `prefix` | `string` | – | Prefix symbol for tooltip values (e.g. "$"). |
| `suffix` | `string` | – | Suffix symbol for tooltip values (e.g. "%"). |
| `label` | `string` | `"Metric trend"` | Accessible label for assistive tech. |

## States

- **rest**: Clean hairline SVG curve with an optional subtle gradient fill. A subtle dot anchors the latest data point.
- **hover / scrub**: Pointer movement snaps to the nearest data point, rendering a dashed vertical guide and a micro-tooltip with exact values.
- **positive trend**: When the last point is greater than or equal to the first, applies theme emerald tokens.
- **negative trend**: When the last point dips below the first, applies theme rose tokens.
- **bars**: Renders discrete rounded vertical bars with proportional opacity highlighting hovered columns.

## What it handles

Dropping a chart into a table cell usually causes layout jitter, blurry SVG scaling, and missing hover tooltips. Sparkline handles continuous width measurement, smooth cubic curve calculation, and magnetic hover snapping out of the box.

- **Zero layout shift**: Renders cleanly within table cells and flex rows without shifting surrounding cell boundaries or altering line heights.
- **Smooth cubic curves**: Generates tangent-continuous cubic bezier curves rather than harsh jagged polylines.
- **Magnetic scrubber**: Tracks mouse or touch coordinates and snaps to the nearest discrete data point with a guide line and snapped dot.
- **Floating micro-tooltip**: Shows the exact value and optional date timestamp directly above the active point, constrained within viewport bounds.
- **Dynamic auto-trend**: Automatically detects whether the final point is higher than the initial point, applying emerald or rose tokens accordingly.
- **Multiple visual styles**: Switches seamlessly between smooth line, area gradient fill, and discrete vertical bars.

## Replaces

heavy charting libraries (Recharts/Chart.js), SVG path math boilerplate, custom hover tooltip calculations, trend color logic, ResizeObserver setup

## Where it fits

- **Data table rows**: Show 30-day activity, revenue, or usage trends directly inside table cells.
- **Stat & metric cards**: Sit beside a prominent KPI number to provide immediate historical context.
- **Financial watchlists**: Compact stock, crypto, or commodity price tickers with green/red directional indicators.
- **Server & CPU monitors**: Display live CPU load, memory usage, or ping latency across a grid of instances.

## Notes

- Uses ResizeObserver to continuously measure parent container width, re-projecting curves without layout thrashing.
- Scrubber calculations use pure relative coordinate offsets and render tooltips within absolute coordinates.
- Auto-trend color evaluation respects shadcn dark mode with explicit dark: variant tokens.
- Screen readers receive an accessible summary with starting value, ending value, and directional trend description.

License: MIT (https://hairlineui.com/license#components)
