# Sparkline

> A compact trend line with the latest reading and a native disclosure for the full data.

- Kind: ui · Category: Data display · Interactivity: native
- Install: `npx shadcn@latest add @haze/sparkline`
- File: `src/components/haze/ui/sparkline.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/sparkline

## Use when
- A small stat card needs recent trend context
- Readers need exact numeric samples as well as the line

## Avoid when
- The values correspond to unrelated categories. Use bar-list.
- Multiple series or axes need detailed comparison.

## Alternatives
- [stat](https://hazeui.com/ui/stat.md)
- [bar-list](https://hazeui.com/ui/bar-list.md)
- [uptime-tracker](https://hazeui.com/ui/uptime-tracker.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| label | `string` | "Request duration" | no | Metric caption. |
| samples | `{ label: string; value: number }[]` |  | no | Finite samples in time order, with a readable label for each. |
| unit | `string` | "ms" | no | Abbreviated unit, or an empty string. |
| detailsLabel | `string` | "View samples" | no | Label for the sample disclosure. |
| emptyLabel | `string` | "No samples yet." | no | Text displayed when there are no samples. |
| class | `string` |  | no | Additional classes on the figure. |

## Accessibility
- The SVG is decorative. The latest value and the full list of samples are available as text.
- Samples are spaced evenly. Aggregate at a regular interval when the horizontal spacing carries meaning.
- Key Enter or Space: Toggle the list of samples when its summary is focused.

## Agent prompts

### Install and use

Install the Haze UI component "Sparkline" with `npx shadcn@latest add @haze/sparkline`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Sparkline from "@/components/haze/ui/sparkline.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/sparkline.md

### Customize with real content

Adapt the Haze sparkline ui for our product. Replace the demo values of these props with our real content: label, samples, unit, detailsLabel, emptyLabel, class. Keep the layout, the heading level rules and these accessibility details: The SVG is decorative. The latest value and the full list of samples are available as text. Samples are spaced evenly. Aggregate at a regular interval when the horizontal spacing carries meaning. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using sparkline, check it fits: use it when a small stat card needs recent trend context; readers need exact numeric samples as well as the line. Avoid it when the values correspond to unrelated categories. use bar-list.; multiple series or axes need detailed comparison.. Alternatives: @haze/stat, @haze/bar-list, @haze/uptime-tracker.

## Example requests that should lead here
- Draw the last seven daily response times as a sparkline with a visible latest sample and textual data.

## Source

```astro
---
import { ChevronDown } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  label?: string;
  samples?: { label: string; value: number }[];
  unit?: string;
  detailsLabel?: string;
  emptyLabel?: string;
  class?: string;
}

const {
  label = "Request duration", unit = "ms", detailsLabel = "View samples", emptyLabel = "No samples yet.", class: className,
  samples = [
    { label: "Mon", value: 176 },
    { label: "Tue", value: 193 },
    { label: "Wed", value: 181 },
    { label: "Thu", value: 169 },
    { label: "Fri", value: 154 },
  ],
} = Astro.props;
const valid = samples.filter(({ value }) => Number.isFinite(value));
const minimum = valid.reduce((lowest, point) => Math.min(lowest, point.value), Infinity);
const maximum = valid.reduce((highest, point) => Math.max(highest, point.value), -Infinity);
const scale = Number.isFinite(maximum - minimum) ? 1 : 2;
const span = maximum / scale - minimum / scale;
const format = (value: number) => unit ? `${value} ${unit}` : String(value);
const points = valid.map(({ value }, index) => ({
  x: valid.length === 1 ? 110 : 4 + index * 212 / (valid.length - 1),
  y: span === 0 ? 26 : 4 + 44 * ((maximum / scale - value / scale) / span),
}));
const last = valid.at(-1);
const end = points.at(-1);
---

<figure class={cn("grid min-w-0 gap-3 rounded-xl border border-border bg-card p-4.5 text-card-foreground", className)}>
  <figcaption class="grid gap-1">
    <span class="text-[0.8125rem]/5 font-medium text-muted-foreground">{label}</span>
    {last && <span class="wrap-anywhere text-xl/7 font-medium tracking-[-0.01em] tabular-nums">{format(last.value)} <span class="text-[0.8125rem]/5 font-normal text-muted-foreground">{last.label}</span></span>}
  </figcaption>
  {end ? (
    <>
      <svg aria-hidden="true" viewBox="0 0 220 52" preserveAspectRatio="none" class="h-13 w-full">
        <path d="M0 4H220M0 26H220M0 48H220" class="fill-none stroke-border" vector-effect="non-scaling-stroke" />
        {points.length > 1 && <polyline points={points.map(({ x, y }) => `${x},${y}`).join(" ")}
          class="fill-none stroke-brand stroke-2" stroke-linecap="round" stroke-linejoin="round" vector-effect="non-scaling-stroke" />}
        <circle cx={end.x} cy={end.y} r="3" class="fill-brand" />
      </svg>
      <details class="group border-t border-border pt-2">
        <summary class="flex min-h-7 cursor-pointer list-none items-center justify-between gap-2 rounded-sm text-[0.8125rem]/5 font-medium text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">
          {detailsLabel}
          <ChevronDown aria-hidden="true" class="size-3.5 transition-[rotate] duration-200 ease-snappy group-open:rotate-180" />
        </summary>
        <dl class="grid gap-1 pt-2 text-[0.8125rem]/5">
          {valid.map(({ label: sample, value }) => <div class="flex min-w-0 flex-wrap items-start justify-between gap-x-3">
            <dt class="wrap-anywhere text-muted-foreground">{sample}</dt>
            <dd class="wrap-anywhere font-medium tabular-nums">{format(value)}</dd>
          </div>)}
        </dl>
      </details>
    </>
  ) : <p class="text-[0.8125rem]/5 text-muted-foreground">{emptyLabel}</p>}
</figure>
```
