Sparkline
Show the trend of one or several measures in a small chart without axes.
Visitors
12,840+18.2%
A sparkline shows at a glance how a measure has moved. It has no axes, labels or tooltip, so it shows the shape of the data and nothing more. Place it beside the figure it belongs to, in a table row, a card or a line of text, and name it with label.
Installation
pnpm dlx shadcn@4.18.0 add @siteplane/sparklineComplete the one-time setup first. Registry JSON lists source files and dependencies.
For this page’s examples, also install:
pnpm dlx shadcn@4.18.0 add @siteplane/toggle-groupUsage
import { Sparkline } from "@/siteplane/components/sparkline";
<Sparkline label="Sign-ups per day" data={[12, 18, 14, 25, 31, 22, 38]} />;Variants
variant sets how the data is drawn. line, the default, is the stroke alone. area adds a fill under the line that fades out toward the baseline, and fill keeps that fill without the line.
line
area
fill
Several series
Pass a list of series instead of one list of numbers. The series share one scale and one color, so they stand apart by their shape: here desktop visitors drop at the weekend while mobile visitors rise. With area each series keeps its line; fill reads best when they overlap.
area
fill
Scale
By default the scale runs from 0 to the highest value, so the height of the line shows the size of the values, and a measure that moves by a few percent draws a nearly flat line. Set includeZero={false} to span the scale from the lowest value to the highest. A small change then shows its shape, but the line no longer shows its size, so write the figure beside it, as a metric card does.
From 0
Lowest to highest
Color
Without color, the sparkline is drawn in the text color around it. Give the parent of a rise text-success-foreground and the parent of a fall text-destructive-foreground, and the line takes the same color as the change beside it. Set color to a CSS color or token to give the sparkline a color of its own, such as var(--chart-brand), the brand color the other charts start from, in the other examples.
- Sign-ups+83.1%
- Orders−41.2%
Changing data
When data changes, the curve morphs into the new one, whatever its length, so a switch from 30 to 90 days glides instead of jumping. A series of one point or none, or a change in the number of series, shows at once.
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
Sparkline. | readonly number[] | readonly (readonly number[])[] | required | One series of numbers, or a list of series that share one scale. A value that is not a finite number is left out and its neighbors are joined. |
Sparkline. | string | required | Accessible name of the sparkline; it is also its native title. |
Sparkline. | line | area | fill | line | line draws the stroke alone; area adds a fill under it that fades out toward the baseline; fill draws that fill without the stroke. |
Sparkline. | string | currentColor | A CSS color or token for the marks. Without it they take the text color around the sparkline, so a parent sets it. |
Sparkline. | number | 32 | Height in pixels. The width follows the room the sparkline is given. |
Sparkline. | boolean | true | Takes 0 into the scale, so the height of the line shows the size of the values. false spans the scale from the lowest value to the highest, so a small change still shows its shape; a series that is the same all along then sits in the middle. |
Sparkline. | string | unset | Additional classes. A width class such as w-24 replaces the full width. |
SparklineProps and SparklineVariant export these contracts. The sparkline
renders one svg element with role="img". Other SVG attributes, such as an
id or data-* attributes, pass through to it; it accepts no children or ref.
Usage notes
Use a sparkline to show how a measure has moved beside its value. It shows the shape of the data only, so when readers need exact values, dates or several measures on a shared axis, use a Line Chart or a Table instead.
The sparkline fills the width it is given and is 32 pixels high by default. In a table cell or beside text, give it a width such as w-24, and set height in pixels. The stroke stays 2 pixels wide at any size.
All series share one scale. It includes 0 unless includeZero is false, so by default a series that varies little around a large value looks flat. A value that is not a finite number is left out and its neighbors are joined, so no gap is drawn. The line is a smooth curve through every point, and a series with only one value is drawn as a dot. A fill reaches from the line to the 0 line, so a series below 0 hangs down from it and one that crosses it fills toward it from both sides. When 0 lies outside the scale, as with includeZero={false}, the fill reaches the edge of the box instead.
The sparkline is an image named by label. It is not a focus stop and shows no values on hover, focus or tap. Screen readers announce the label and not the values, so say in the label what the sparkline shows and over which period, and write the figure beside it as text.
The sparkline renders its complete SVG on the server. It is drawn in its own units and stretched to its box, so it never measures its container. New data and a change of color animate with the Motion tokens (--motion-duration-value and --motion-ease-standard), and instantly under reduced motion: the system setting, html with data-motion-reduce="true" or SiteplaneMotionProvider preset="reduced".