KPI Sparklines
A grid of six product-health KPI tiles, each with a delta and sparkline, and a 7/30/90-day period selector.
"use client"
import { useState } from "react"
import { ArrowDownIcon, ArrowUpIcon } from "@phosphor-icons/react"
import { cn } from "cn"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
const COPY = {
title: "Product health",
description: "Key metrics across all workspaces",
against: "vs previous period",
}
const RANGES = [
{ key: "7d", label: "7 days" },
{ key: "30d", label: "30 days" },
{ key: "90d", label: "90 days" },
] as const
type Range = (typeof RANGES)[number]["key"]
const METRICS = [
{
label: "Active workspaces",
value: "4,812",
delta: 8.2,
good: "up",
series: {
"7d": [40, 42, 41, 45, 47, 46, 50],
"30d": [30, 33, 31, 36, 40, 38, 44, 47, 46, 50],
"90d": [20, 24, 28, 27, 33, 38, 42, 45, 50, 52],
},
},
{
label: "Weekly sessions",
value: "126k",
delta: 3.4,
good: "up",
series: {
"7d": [60, 58, 63, 62, 66, 64, 68],
"30d": [50, 55, 52, 58, 57, 61, 60, 64, 66, 68],
"90d": [38, 42, 41, 49, 52, 55, 58, 62, 65, 68],
},
},
{
label: "Error rate",
value: "0.42%",
delta: -11.6,
good: "down",
series: {
"7d": [30, 28, 29, 25, 26, 22, 21],
"30d": [40, 38, 41, 36, 33, 34, 29, 27, 24, 21],
"90d": [52, 49, 50, 44, 40, 36, 33, 28, 24, 21],
},
},
{
label: "Median load time",
value: "1.18s",
delta: 2.1,
good: "down",
series: {
"7d": [20, 22, 21, 23, 22, 24, 25],
"30d": [22, 21, 23, 22, 24, 23, 25, 24, 26, 25],
"90d": [18, 20, 19, 22, 21, 23, 24, 24, 26, 25],
},
},
{
label: "Trial conversion",
value: "18.9%",
delta: 1.6,
good: "up",
series: {
"7d": [30, 32, 31, 33, 34, 33, 36],
"30d": [26, 28, 27, 30, 29, 32, 31, 34, 35, 36],
"90d": [20, 22, 25, 24, 27, 30, 31, 33, 35, 36],
},
},
{
label: "Support backlog",
value: "63",
delta: -9.0,
good: "down",
series: {
"7d": [50, 48, 49, 44, 42, 40, 38],
"30d": [60, 56, 58, 52, 50, 47, 45, 42, 40, 38],
"90d": [70, 66, 64, 60, 55, 50, 47, 44, 41, 38],
},
},
] as const
/** A grid of KPI tiles, each with a sparkline and a period selector. */
export default function MetricsKpiSparklines01() {
const [range, setRange] = useState<Range>("30d")
return (
<Card className="mx-auto w-full max-w-4xl">
<CardHeader className="sm:flex sm:items-start sm:justify-between">
<div>
<CardTitle>
<h2>{COPY.title}</h2>
</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
</div>
<div
role="group"
aria-label="Period"
className="mt-3 inline-flex rounded-full bg-muted p-1 sm:mt-0"
>
{RANGES.map((r) => (
<button
key={r.key}
type="button"
aria-pressed={range === r.key}
onClick={() => setRange(r.key)}
className={cn(
"rounded-full px-3 py-1 text-xs font-medium text-muted-foreground transition-colors focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-none",
range === r.key && "bg-background text-foreground shadow-sm"
)}
>
{r.label}
</button>
))}
</div>
</CardHeader>
<CardContent>
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
{METRICS.map((m) => {
const up = m.delta > 0
const healthy = (m.good === "up") === up
const Arrow = up ? ArrowUpIcon : ArrowDownIcon
return (
<div
key={m.label}
className="flex flex-col gap-3 rounded-2xl border p-4"
>
<p className="text-xs text-muted-foreground">{m.label}</p>
<div className="flex items-end justify-between gap-3">
<div>
<p className="text-2xl font-medium tabular-nums">
{m.value}
</p>
<p
className={cn(
"mt-1 flex items-center gap-1 text-xs",
healthy ? "text-primary" : "text-destructive"
)}
>
<Arrow aria-hidden="true" className="size-3" />
{Math.abs(m.delta)}%
<span className="sr-only"> {COPY.against}</span>
</p>
</div>
<Sparkline values={m.series[range]} healthy={healthy} />
</div>
</div>
)
})}
</div>
</CardContent>
</Card>
)
}
function Sparkline({
values,
healthy,
}: {
values: readonly number[]
healthy: boolean
}) {
const w = 96
const h = 36
const min = Math.min(...values)
const max = Math.max(...values)
const pts = values.map((v, i) => [
(i / (values.length - 1)) * w,
h - 3 - ((v - min) / (max - min || 1)) * (h - 6),
])
const line = pts
.map((p, i) => `${i ? "L" : "M"}${p[0].toFixed(1)} ${p[1].toFixed(1)}`)
.join(" ")
const last = pts[pts.length - 1]
return (
<svg
aria-hidden="true"
viewBox={`0 0 ${w} ${h}`}
className={cn(
"h-9 w-24 shrink-0 overflow-visible",
healthy ? "text-primary" : "text-destructive"
)}
>
<path
d={`${line} L${w} ${h} L0 ${h} Z`}
fill="currentColor"
opacity="0.1"
/>
<path
d={line}
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
<circle cx={last[0]} cy={last[1]} r="2.5" fill="currentColor" />
</svg>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/kpi-sparklines-01npx shadcn@latest add @01orbital/kpi-sparklines-01yarn dlx shadcn@latest add @01orbital/kpi-sparklines-01bunx --bun shadcn@latest add @01orbital/kpi-sparklines-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/metrics/kpi-sparklines-01/index.tsx: change them there, not in the markup. RANGESandMETRICS, also at the top of the file, hold its lists and settings.
Built from
npm: @phosphor-icons/react, cn. The CLI installs them 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