KPI tile
A headline number with its comparison, direction-aware colour and an optional sparkline.
A word-sized trend for tiles, tables and inline text.
<Sparkline />Sparkline is free under the MIT license.
$ npx shadcn@latest add https://chartslibraryui.com/r/sparkline.jsonInstalls the chart, its dependencies and the shared core into your components folder.
Then import the theme once: import "@/components/charts/core/charts.css" Installation guide
import { Sparkline } from "@/components/charts/sparkline";
export function Example() {
return (
<div className="clui" style={{ display: "grid", gap: 14, maxWidth: 420 }}>
{rows.map((r) => (
<div key={r.label} style={{ display: "grid", gridTemplateColumns: "120px 1fr 70px", alignItems: "center", gap: 12, fontSize: 13 }}>
<span style={{ color: "var(--clui-ink-2)" }}>{r.label}</span>
<Sparkline data={r.data} label={r.label} height={26} />
<span style={{ textAlign: "right", fontWeight: 600, fontVariantNumeric: "tabular-nums" }}>{r.value}</span>
</div>
))}
</div>
);
}
An array of numbers (null for gaps), oldest first.
[412, 430, 428, null, 455, 471]Missing values are null. Invalid values make the chart say what’s wrong instead of drawing a misleading picture. How charts handle bad data
Every chart also accepts the shared frame props: title, subtitle, source, height, mode, toolbar. Common API
export interface SparklineProps {
/** Values in order. null makes a gap. */
data: (number | null)[];
/** Height in px. Width fills the container unless `width` is set. */
height?: number;
width?: number;
/** Line colour. Default: the context grey with the last point in the accent. */
color?: string;
/** Light wash under the line. */
area?: boolean;
/** Mark the last point. Default true. */
endDot?: boolean;
/** Include zero in the range. Default false (a sparkline shows shape, not magnitude). */
zero?: boolean;
/** Used in the accessible label. */
label?: string;
format?: FormatInput;
className?: string;
}Installs sparkline.tsx and the shared core/ (11 files).
"use client";
import { useWidth } from "./core/frame";
import { linePath, areaPath, type MaybePoint } from "./core/path";
import { linearScale } from "./core/scale";
import { formatValue, type FormatInput } from "./core/format";
export interface SparklineProps {
/** Values in order. null makes a gap. */
data: (number | null)[];
/** Height in px. Width fills the container unless `width` is set. */
height?: number;
width?: number;
/** Line colour. Default: the context grey with the last point in the accent. */
color?: string;
/** Light wash under the line. */
area?: boolean;
/** Mark the last point. Default true. */
endDot?: boolean;
/** Include zero in the range. Default false (a sparkline shows shape, not magnitude). */
zero?: boolean;
/** Used in the accessible label. */
label?: string;
format?: FormatInput;
className?: string;
}
/**
* A word-sized trend with no axes, for tiles, tables and inline text. It shows shape, not magnitude, so pair it
* with the actual number.
*/
export function Sparkline({
data,
height = 28,
width: fixed,
color = "var(--clui-ink-3)",
area = false,
endDot = true,
zero = false,
label = "Trend",
format,
className,
}: SparklineProps) {
const [ref, measured] = useWidth<HTMLSpanElement>(fixed, 120);
const width = fixed ?? measured;
const values = Array.isArray(data) ? data.map((v) => (typeof v === "number" && Number.isFinite(v) ? v : null)) : [];
const finite = values.filter((v): v is number => v !== null);
const first = finite[0];
const last = finite[finite.length - 1];
const aria = finite.length
? `${label}: ${finite.length} points, from ${formatValue(first, format)} to ${formatValue(last, format)}, low ${formatValue(
Math.min(...finite),
format,
)}, high ${formatValue(Math.max(...finite), format)}.`
: `${label}: no data.`;
let content = null;
if (width > 0 && finite.length > 0) {
let lo = Math.min(...finite);
let hi = Math.max(...finite);
if (zero) {
lo = Math.min(0, lo);
hi = Math.max(0, hi);
}
if (lo === hi) {
lo -= 1;
hi += 1;
}
const pad = 3;
const x = linearScale([0, Math.max(1, values.length - 1)], [pad, width - pad]);
const y = linearScale([lo, hi], [height - pad, pad]);
const pts: MaybePoint[] = values.map((v, i) => (v === null ? null : [x(i), y(v)]));
let lastI = -1;
for (let i = values.length - 1; i >= 0; i--) if (values[i] !== null) { lastI = i; break; }
content = (
<svg width={width} height={height} aria-hidden style={{ display: "block", overflow: "visible" }}>
{area && <path d={areaPath(pts, y(Math.max(lo, Math.min(0, hi))), "linear")} style={{ fill: color, opacity: 0.12 }} />}
<path d={linePath(pts)} fill="none" style={{ stroke: color }} strokeWidth={1.5} strokeLinejoin="round" strokeLinecap="round" />
{endDot && lastI >= 0 && (
<circle cx={x(lastI)} cy={y(values[lastI]!)} r={3} style={{ fill: "var(--clui-s1)", stroke: "var(--clui-surface)" }} strokeWidth={1.5} />
)}
</svg>
);
}
return (
<span
ref={ref}
className={["clui", className].filter(Boolean).join(" ")}
role="img"
aria-label={aria}
style={{ display: "block", width: fixed ?? "100%", height, minWidth: 40 }}
>
{content}
</span>
);
}
A headline number with its comparison, direction-aware colour and an optional sparkline.
A sortable table with inline bars, coloured deltas and sparklines.