A word-sized trend line with its latest, lowest and highest points.
- Recurring revenue$296.8K
- Weekly signups859
- p95 latency, hourly256 ms
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { formatShort } from "./ui/uicontext/components/charts/Format.ts"import { Sparkline } from "./ui/uicontext/components/charts/Sparkline.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { latency, mrr, signupsByPlan } from "./metrics.ts"const styles = stylex.create({ list: { display: "grid", gap: 0, margin: 0, padding: 0, listStyle: "none", inlineSize: "min(100%, 30rem)", fontSize: font.sm }, row: { display: "grid", gridTemplateColumns: "minmax(0, 1fr) auto 5.5rem", alignItems: "center", gap: space.lg, paddingBlock: space.md, borderBottomWidth: 1, borderBottomStyle: "solid", borderBottomColor: color.line, }, name: { minWidth: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, value: { textAlign: "end", fontWeight: 600, fontVariantNumeric: "tabular-nums" },})const total = (i: number) => (mrr.reduce((sum, s) => sum + (s.points[i]?.y ?? 0), 0))const revenue = mrr[0]?.points.map((_, i) => total(i)) ?? []const signups = signupsByPlan[0]?.points.map((_, i) => signupsByPlan.reduce((sum, s) => sum + (s.points[i]?.y ?? 0), 0)) ?? []const p95 = (latency[1]?.points ?? []).filter((_, i) => i % 12 === 0).map((p) => p.y)// Each metric's last twelve readings beside its latest value. The dot marks the latest; the small// gray dots the lowest and highest.export const MetricRows = <M>(h: Html.H<M>) => h.ul.attrs(sx(styles.list)).children( ...[ { id: "mrr", name: "Recurring revenue", values: revenue, text: formatShort(revenue.at(-1), { style: "currency", currency: "USD" }) }, { id: "signups", name: "Weekly signups", values: signups, text: formatShort(signups.at(-1)) }, { id: "p95", name: "p95 latency, hourly", values: p95, text: `${p95.at(-1) ?? "—"} ms`, color: 2 as const }, ].map((metric) => h.li.key(metric.id).attrs(sx(styles.row)).children( h.span.attrs(sx(styles.name)).children(metric.name), Sparkline(h, { values: metric.values, label: metric.name, extremes: true, color: metric.color }), h.span.attrs(sx(styles.value)).children(metric.text), ), ), )Installation
pnpm dlx shadcn@latest add checkfu/uicontext/sparklineUsage
import { Sparkline } from "./ui/uicontext/components/charts/Sparkline.ts"export const SignupTrend = <M>(h: Html.H<M>, weekly: ReadonlyArray<number>) => Sparkline(h, { values: weekly, label: "Signups, last 12 weeks" })Examples
Fill the width
Stretches with its container and keeps a crisp line.
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Sparkline } from "./ui/uicontext/components/charts/Sparkline.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { latency } from "./metrics.ts"const styles = stylex.create({ frame: { inlineSize: "100%", resize: "horizontal", overflow: "hidden", minInlineSize: 120, maxInlineSize: "100%" } })const p50 = (latency[0]?.points ?? []).map((p) => p.y)// Stretched to its container with a soft fill. Drag the corner: the line stays sharp at any width.export const WideSparkline = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.frame)).children( Sparkline(h, { values: p50, label: "p50 latency, last 24 hours", format: { style: "unit", unit: "millisecond" }, width: "fill", height: 48, area: true }), )API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
valuesrequired | ReadonlyArray<number | null> | — | — |
labelrequired | string | — | What it shows, for its accessible name: "Signups, last 12 weeks" |
format | ChartFormat | — | — |
width | number | "fill" | 96 | Pixels, or "fill" to take its container's width. 96 by default |
height | number | 24 | Pixels; 24 by default |
area | boolean | — | A soft fill under the line |
extremes | boolean | — | Dots on the lowest and highest values |
color | ChartColor | — | — |
locale | string | — | — |
style | Style | — | — |
Accessibility
- A sparkline is one image whose name says where the trend went: from, to, low and high.