Sparkline Table
Metrics table with a small trend line in every row beside the latest value and its change; rising metrics draw in the chart color and falling ones in muted gray.
"use client"
import { Line, LineChart, YAxis } from "recharts"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { ChartContainer, type ChartConfig } from "@/components/ui/chart"
// The copy: change text here, not in the markup.
const COPY = {
title: "This week",
last: "Last",
readings: "readings per metric",
metric: "Metric",
trend: "Trend",
latest: "Latest",
change: "Change",
} as const
const MONEY = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0,
})
const PERCENT = new Intl.NumberFormat("en-US", {
style: "percent",
maximumFractionDigits: 2,
signDisplay: "always",
})
const series = (base: number, swing: number, drift: number, phase: number) =>
Array.from({ length: 20 }, (_, index) => ({
value: Math.round(
base +
Math.sin(index / 2.4 + phase) * swing +
Math.sin(index * 1.3 + phase) * swing * 0.3 +
index * drift
),
}))
const METRICS: ReadonlyArray<{
name: string
series: ReadonlyArray<{ value: number }>
unit?: string
money?: boolean
}> = [
{ name: "Visitors", series: series(1_200, 120, 18, 0) },
{ name: "Signups", series: series(84, 10, 1.2, 1.4) },
{ name: "Conversion", series: series(62, 4, -0.6, 2.2), unit: "‰" },
{ name: "Churned", series: series(22, 3, -0.4, 0.6) },
{ name: "Revenue", series: series(3_400, 260, 42, 3.1), money: true },
]
const CHART_CONFIG = {
value: { label: "Value", color: "var(--chart-3)" },
} satisfies ChartConfig
/** A metrics table where each row carries its own trend line, so the table
* answers both how much and which way. A rising metric draws in the chart
* color and a falling one in muted gray, so the chart never implies whether a
* fall is good or bad. */
export default function MetricsSparklineTable() {
return (
<Card className="mx-auto w-full max-w-md">
<CardHeader>
<CardTitle>
<h2>{COPY.title}</h2>
</CardTitle>
<CardDescription>
{COPY.last} {METRICS[0].series.length} {COPY.readings}
</CardDescription>
</CardHeader>
<CardContent>
<table className="w-full text-sm/5">
{/* Screen readers get the column names; sighted readers get the layout. */}
<thead className="sr-only">
<tr>
<th scope="col">{COPY.metric}</th>
<th scope="col">{COPY.trend}</th>
<th scope="col">{COPY.latest}</th>
<th scope="col">{COPY.change}</th>
</tr>
</thead>
<tbody className="divide-y divide-border">
{METRICS.map((metric) => {
const first = metric.series[0].value
const last = metric.series[metric.series.length - 1].value
const change = (last - first) / first
const latest = metric.money
? MONEY.format(last)
: `${last.toLocaleString("en-US")}${metric.unit ?? ""}`
return (
<tr key={metric.name}>
<th
scope="row"
className="py-2.5 text-left font-normal text-muted-foreground"
>
{metric.name}
</th>
<td className="py-2.5">
<ChartContainer
config={CHART_CONFIG}
className="ms-auto aspect-auto h-7 w-24"
>
<LineChart
data={[...metric.series]}
margin={{ top: 4, right: 4, bottom: 4, left: 4 }}
>
<YAxis hide domain={["dataMin", "dataMax"]} />
<Line
dataKey="value"
type="monotone"
stroke={
change < 0
? "var(--muted-foreground)"
: "var(--color-value)"
}
strokeWidth={1.5}
dot={false}
isAnimationActive={false}
/>
</LineChart>
</ChartContainer>
</td>
<td className="py-2.5 ps-4 text-right font-medium tabular-nums">
{latest}
</td>
<td
className={
change < 0
? "w-16 py-2.5 ps-3 text-right text-muted-foreground tabular-nums"
: "w-16 py-2.5 ps-3 text-right text-chart-3 tabular-nums"
}
>
{PERCENT.format(change)}
</td>
</tr>
)
})}
</tbody>
</table>
</CardContent>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/metrics-sparkline-tableMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/metrics-sparkline-table/index.tsx: change them there, not in the markup. MONEY,PERCENTandCHART_CONFIG, also at the top of the file, hold its lists and settings.
Built from
npm: recharts@3.8.0. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on