KPI row
A responsive row of KPI tiles that wraps cleanly.
A sortable table with inline bars, coloured deltas and sparklines.
<TableChart />Table chart is free under the MIT license.
$ npx shadcn@latest add https://chartslibraryui.com/r/table-chart.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 { TableChart } from "@/components/charts/table-chart";
import { productTable } from "./sample-data";
export function Example() {
return (
<TableChart
title="Connectors are the fastest-growing product"
subtitle="Revenue, last twelve months"
data={productTable}
sortBy={{ key: "revenue", dir: "desc" }}
columns={[
{ key: "product", label: "Product" },
{ key: "revenue", label: "Revenue", type: "bar", format: "currency" },
{ key: "growth", label: "Growth", type: "delta", format: { kind: "percent", decimals: 1 } },
{ key: "margin", label: "Margin", type: "number", format: { kind: "percent", decimals: 0 } },
{ key: "trend", label: "Last 7 quarters", type: "sparkline" },
]}
/>
);
}
Rows plus column definitions (text, number, bar, delta, sparkline).
[{ product: "Core", revenue: 6.2e6, growth: 0.14, trend: [4.8, 5.0, 5.3] }, ...]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 TableChartProps {
data: Row[];
columns: TableChartColumn[];
title?: ReactNode;
subtitle?: ReactNode;
source?: ReactNode;
/** Initial sort. */
sortBy?: { key: string; dir: "asc" | "desc" };
/** Optional totals row (pass the row object). */
totals?: Row;
className?: string;
style?: CSSProperties;
/** Show a CSV download button. */
csv?: boolean;
}Installs table-chart.tsx and the shared core/ (11 files).
"use client";
import { useMemo, useState, type CSSProperties, type ReactNode } from "react";
import { arrowGlyph, toneOf, toneText, toneWord, type Favorable } from "./core/color";
import { downloadCSV } from "./core/frame";
import type { Row } from "./core/data";
import { formatValue, type FormatInput } from "./core/format";
import { Sparkline } from "./sparkline";
export type TableColumnType = "text" | "number" | "bar" | "delta" | "sparkline";
export interface TableChartColumn {
key: string;
label: string;
type?: TableColumnType;
format?: FormatInput;
/**
* For "delta" columns (values that are already a change, e.g. growth 0.14 = +14%): is up good?
* Use format "pp" when the value is a difference between two percentages.
*/
favorable?: Favorable;
/** Allow sorting by this column. Default true except sparklines. */
sortable?: boolean;
width?: number | string;
}
export interface TableChartProps {
data: Row[];
columns: TableChartColumn[];
title?: ReactNode;
subtitle?: ReactNode;
source?: ReactNode;
/** Initial sort. */
sortBy?: { key: string; dir: "asc" | "desc" };
/** Optional totals row (pass the row object). */
totals?: Row;
className?: string;
style?: CSSProperties;
/** Show a CSV download button. */
csv?: boolean;
}
/**
* A table that is also a chart: inline bars on a shared scale, signed deltas coloured by `favorable`, and trend
* sparklines. A real HTML table, so it's accessible and copy-pasteable by default.
*/
export function TableChart({ data, columns, title, subtitle, source, sortBy, totals, className, style, csv = true }: TableChartProps) {
const [sort, setSort] = useState(sortBy ?? null);
const rows = useMemo(() => {
const out = Array.isArray(data) ? [...data] : [];
if (sort) {
out.sort((a, b) => {
const av = a?.[sort.key];
const bv = b?.[sort.key];
const cmp = typeof av === "number" && typeof bv === "number" ? av - bv : String(av ?? "").localeCompare(String(bv ?? ""));
return sort.dir === "asc" ? cmp : -cmp;
});
}
return out;
}, [data, sort]);
const maxes = useMemo(() => {
const m: Record<string, number> = {};
for (const c of columns)
if (c.type === "bar") m[c.key] = Math.max(0, ...rows.map((r) => (typeof r?.[c.key] === "number" ? Math.abs(r[c.key] as number) : 0)));
return m;
}, [columns, rows]);
if (!Array.isArray(data) || !data.length)
return (
<figure className={["clui", className].filter(Boolean).join(" ")} style={style}>
{title && <h3 className="clui-title">{title}</h3>}
<div className="clui-message" style={{ minHeight: 120 }} role="alert">
<div className="clui-message-title">No data to display</div>
</div>
</figure>
);
const header = (c: TableChartColumn) => {
const sortable = c.sortable ?? c.type !== "sparkline";
const dir = sort?.key === c.key ? sort.dir : null;
const align = c.type && c.type !== "text" ? "right" : "left";
return (
<th key={c.key} scope="col" data-align={align} style={{ width: c.width }} aria-sort={dir === "asc" ? "ascending" : dir === "desc" ? "descending" : undefined}>
{sortable ? (
<button
type="button"
onClick={() => setSort((s) => (s?.key === c.key ? { key: c.key, dir: s.dir === "desc" ? "asc" : "desc" } : { key: c.key, dir: "desc" }))}
style={{ font: "inherit", color: "inherit", background: "none", border: 0, padding: 0, cursor: "pointer" }}
>
{c.label}
<span aria-hidden style={{ opacity: dir ? 1 : 0.3, marginLeft: 4 }}>
{dir === "asc" ? "↑" : dir === "desc" ? "↓" : "↕"}
</span>
</button>
) : (
c.label
)}
</th>
);
};
const cell = (c: TableChartColumn, r: Row, isTotal = false): ReactNode => {
const v = r?.[c.key];
switch (c.type) {
case "number":
return formatValue(v as number, c.format, { context: "table" });
case "bar": {
const num = typeof v === "number" ? v : null;
const share = num !== null && maxes[c.key] ? Math.abs(num) / maxes[c.key] : 0;
return (
<span style={{ display: "inline-flex", alignItems: "center", gap: 8, justifyContent: "flex-end", width: "100%" }}>
<span>{formatValue(num, c.format, { context: "table" })}</span>
{!isTotal && (
<span aria-hidden style={{ width: 80, height: 8, background: "var(--clui-grid)", borderRadius: 2, flexShrink: 0, overflow: "hidden" }}>
<span style={{ display: "block", height: "100%", width: `${share * 100}%`, background: num !== null && num < 0 ? "var(--clui-context)" : "var(--clui-s1)", borderRadius: 2 }} />
</span>
)}
</span>
);
}
case "delta": {
const num = typeof v === "number" ? v : null;
const tone = toneOf(num, c.favorable ?? "up");
return (
<span style={{ color: toneText(tone) }}>
<span aria-hidden style={{ fontSize: 8, marginRight: 3 }}>
{arrowGlyph(num)}
</span>
{formatValue(num, c.format, { context: "table", signed: true })}
<span className="clui-sr"> ({toneWord(tone)})</span>
</span>
);
}
case "sparkline":
return Array.isArray(v) ? (
<span style={{ display: "inline-block", width: 90, verticalAlign: "middle" }}>
<Sparkline data={v as number[]} height={22} label={c.label} format={c.format} />
</span>
) : (
"–"
);
default:
return v === null || v === undefined ? "–" : String(v);
}
};
const exportTable = {
columns: columns.filter((c) => c.type !== "sparkline").map((c) => ({ key: c.key, label: c.label })),
rows,
};
return (
<figure className={["clui", className].filter(Boolean).join(" ")} style={style}>
{(title || subtitle || csv) && (
<div className="clui-head">
<div className="clui-head-text">
{title && <h3 className="clui-title">{title}</h3>}
{subtitle && <p className="clui-subtitle">{subtitle}</p>}
</div>
{csv && (
<div className="clui-toolbar">
<button type="button" className="clui-tool" onClick={() => downloadCSV(exportTable, "table.csv")} aria-label="Download data as CSV">
CSV
</button>
</div>
)}
</div>
)}
<div className="clui-table-wrap" style={{ border: 0 }}>
<table className="clui-table" style={{ fontSize: 13 }}>
<thead>
<tr>{columns.map(header)}</tr>
</thead>
<tbody>
{rows.map((r, i) => (
<tr key={i}>
{columns.map((c, j) =>
j === 0 ? (
<th key={c.key} scope="row" data-align="left">
{cell(c, r)}
</th>
) : (
<td key={c.key} data-align={c.type && c.type !== "text" ? "right" : "left"}>
{cell(c, r)}
</td>
),
)}
</tr>
))}
</tbody>
{totals && (
<tfoot>
<tr style={{ fontWeight: 600 }}>
{columns.map((c, j) => (
<td key={c.key} data-align={j === 0 || !c.type || c.type === "text" ? "left" : "right"} style={{ borderTop: "1px solid var(--clui-axis)" }}>
{c.type === "sparkline" ? "" : cell(c, totals, true)}
</td>
))}
</tr>
</tfoot>
)}
</table>
</div>
{source && (
<figcaption className="clui-foot">
<span className="clui-source">Source: {source}</span>
</figcaption>
)}
</figure>
);
}
A responsive row of KPI tiles that wraps cleanly.
A word-sized trend for tiles, tables and inline text.
Actual vs plan per line item: levels and variance side by side, coloured by business direction.