KPI row
A responsive row of KPI tiles that wraps cleanly.
A headline number with its comparison, direction-aware colour and an optional sparkline.
<KpiTile />KPI tile is free under the MIT license.
$ npx shadcn@latest add https://chartslibraryui.com/r/kpi-tile.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 { KpiTile } from "@/components/charts/kpi-tile";
import { kpis } from "./sample-data";
export function Example() {
return (
<div style={{ display: "grid", gap: 12, gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))" }}>
<KpiTile label="Annual recurring revenue" value={kpis.arr.value} compare={kpis.arr.prior} compareLabel="vs last year" format="currency" trend={kpis.arr.spark} />
<KpiTile label="Logo churn, monthly" value={kpis.churn.value} compare={kpis.churn.prior} compareLabel="vs last quarter" format={{ kind: "percent", decimals: 2 }} favorable="down" />
<KpiTile label="Customer acquisition cost" value={kpis.cac.value} compare={kpis.cac.prior} compareLabel="vs last quarter" format="currency" favorable="down" />
</div>
);
}
A value, optionally a comparison value, a format and a trend array.
{ label: "ARR", value: 18420000, compare: 15960000, format: "currency" }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 KpiTileProps {
/** Sentence case, no trailing colon: "Annual recurring revenue". */
label: string;
value: number | null | undefined;
format?: FormatInput;
/** Comparison value (prior period, plan, target). */
compare?: number | null;
/** Names the comparison: "vs last year", "vs plan". */
compareLabel?: string;
/** Is up good? "down" for costs, churn, latency. Default "up". */
favorable?: Favorable;
/** How to express the change. Default: "both" for currency/number, "absolute" (pp) for percentages. */
delta?: "absolute" | "relative" | "both" | "none";
/** Optional trend, oldest first. */
trend?: (number | null)[];
/** Extra line under the delta (e.g. "Target $20M"). */
footnote?: ReactNode;
className?: string;
/** Mark the value as a forecast or partial period. */
status?: "forecast" | "partial";
}Installs kpi-tile.tsx and the shared core/ (11 files).
"use client";
import type { ReactNode } from "react";
import { arrowGlyph, toneOf, toneText, toneWord, type Favorable } from "./core/color";
import { formatChange, formatDelta, formatValue, type FormatInput } from "./core/format";
import { Sparkline } from "./sparkline";
export interface KpiTileProps {
/** Sentence case, no trailing colon: "Annual recurring revenue". */
label: string;
value: number | null | undefined;
format?: FormatInput;
/** Comparison value (prior period, plan, target). */
compare?: number | null;
/** Names the comparison: "vs last year", "vs plan". */
compareLabel?: string;
/** Is up good? "down" for costs, churn, latency. Default "up". */
favorable?: Favorable;
/** How to express the change. Default: "both" for currency/number, "absolute" (pp) for percentages. */
delta?: "absolute" | "relative" | "both" | "none";
/** Optional trend, oldest first. */
trend?: (number | null)[];
/** Extra line under the delta (e.g. "Target $20M"). */
footnote?: ReactNode;
className?: string;
/** Mark the value as a forecast or partial period. */
status?: "forecast" | "partial";
}
/**
* A headline number with its comparison. The arrow follows the sign; the colour follows `favorable`, and the
* accessible text says "favorable" or "unfavorable" so meaning never depends on colour alone.
*/
export function KpiTile({
label,
value,
format,
compare,
compareLabel = "vs prior period",
favorable = "up",
delta: deltaMode,
trend,
footnote,
className,
status,
}: KpiTileProps) {
const isPercent = typeof format === "object" ? format.kind === "percent" : format === "percent";
const mode = deltaMode ?? (isPercent ? "absolute" : "both");
const hasCompare = typeof compare === "number" && Number.isFinite(compare) && typeof value === "number";
const diff = hasCompare ? (value as number) - (compare as number) : null;
const tone = toneOf(diff, favorable);
const abs = formatDelta(diff, format);
const rel = hasCompare ? formatChange(value, compare) : "–";
const deltaText =
mode === "absolute" ? abs : mode === "relative" ? rel : mode === "both" ? (rel === "–" ? abs : `${abs} (${rel})`) : "";
const valueText = formatValue(value, format, { context: "label" });
const sr = `${label}: ${valueText}${status ? ` (${status})` : ""}.${
hasCompare && mode !== "none" ? ` ${deltaText} ${compareLabel}, ${toneWord(tone)}.` : ""
}`;
return (
<div className={["clui clui-kpi", className].filter(Boolean).join(" ")} role="group" aria-label={sr}>
<div className="clui-kpi-label" title={label}>
{label}
{status && <span style={{ marginLeft: 6, color: "var(--clui-ink-3)" }}>· {status === "forecast" ? "Forecast" : "Partial"}</span>}
</div>
<div className="clui-kpi-foot">
<div style={{ minWidth: 0 }}>
<div className="clui-kpi-value" aria-hidden>
{valueText}
</div>
{hasCompare && mode !== "none" && (
<div className="clui-kpi-delta" aria-hidden>
<span style={{ color: toneText(tone), fontWeight: 500 }}>
{arrowGlyph(diff) && <span style={{ fontSize: 9, marginRight: 3 }}>{arrowGlyph(diff)}</span>}
{deltaText}
</span>
<span className="clui-kpi-delta-muted">{compareLabel}</span>
</div>
)}
</div>
{trend && trend.length > 1 && (
<div className="clui-kpi-spark" style={{ width: 84, flexShrink: 0 }} aria-hidden>
<Sparkline data={trend} height={30} format={format} label={label} />
</div>
)}
</div>
{footnote && <div className="clui-kpi-delta clui-kpi-delta-muted">{footnote}</div>}
</div>
);
}
A responsive row of KPI tiles that wraps cleanly.
A word-sized trend for tiles, tables and inline text.
KPIs against targets and qualitative ranges: the compact replacement for gauges.