Components
Spark Stat
A figure with its change against the previous period and a sparkline. The pill and the line take their colour from the trend.
Installation
bunx @happlyui/cli@latest add spark-stat
Dependencies
npm packages
recharts@^3.8.1
Registry dependencies
These are automatically installed when you add this component.
happly-ui-utilswidget-card
Usage
import * as SparkStat from "@/components/ui/spark-stat"
<SparkStat.Root
title="Metric title"
value={65}
delta={23}
points={[2, 3, 5, 8, 12]}
/>
Examples
Default
A positive change turns the pill and line green. The trend follows the sign of delta unless you pass one.
Metric title
65+23%
<SparkStat.Root title="Metric title" value={65} delta={23} points={points} />
Down
A negative change turns the pill and line red.
Metric title
1,284-8%
<SparkStat.Root title="Metric title" value={1284} delta={-8} points={points} />
Flat
No change keeps the pill and line grey.
Metric title
20%
<SparkStat.Root title="Metric title" value={2} delta={0} points={points} />
No delta
Leave delta out, or pass null, when there is no previous period to compare against. The pill is hidden and the line turns grey unless you pass a trend.
Metric title
8,241
<SparkStat.Root title="Metric title" value={8241} delta={null} points={points} />
Narrow range
The sparkline scales to its data, so a small change between large values remains visible. A change that rounds to 0% gets a neutral pill and line.
Recent activity
8,2410%
<SparkStat.Root title="Recent activity" value={8241} delta={0.3} points={[8100, 8138, 8110, 8205, 8241]} />
API Reference
SparkStat.Root
The whole card, 96px tall. Size its width with className.
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | - | The metric's name |
value | number | - | The figure, formatted with toLocaleString |
delta | number | null | - | Change against the previous period, in percent. Leave out, or pass null, for no pill |
trend | 'up' | 'down' | 'flat' | sign of rounded delta | Colours the pill and line. Pass your data source's own direction when it has one; a displayed 0% stays neutral |
points | number[] | - | The sparkline's values, oldest first |