Pivot Table

View as Markdown

Records summed two ways, such as revenue by region and quarter, with totals on both edges and cells tinted by size.

Closed deals: Revenue by region and quarter
Region / Quarter
North America$160,800$193,800$162,600$171,600$688,800
Europe$171,600$188,400$173,400$166,200$699,600
Asia Pacific$173,400$166,200$199,200$168,000$706,800
Latin America$199,200$168,000$160,800$193,800$721,800
Total$705,000$716,400$696,000$699,600$2,817,000

When to use

Records summed two ways, such as revenue by region and quarter, with totals on both edges and cells tinted by size. Use Data Grid instead when people read and edit the records themselves, not their sums.

Usage

import { PivotTable, type PivotRecord } from "./ui/uicontext/components/PivotTable.ts"
export const Revenue = <M>(h: Html.H<M>, deals: ReadonlyArray<PivotRecord>) =>	PivotTable(h, {		props: {			label: "Revenue",			records: deals,			fields: [				{ key: "region", label: "Region" },				{ key: "quarter", label: "Quarter" },				{ key: "amount", label: "Amount", measure: true, format: "currency" },			],			rows: "region",			columns: "quarter",			value: "amount",		},	})

Examples

Counting records

With no value chosen, each cell counts its records. Pick Age in days and Average to see how long tickets wait.

Open tickets: Count by team and priority
Team / Priority
Billing945422
Growth495422
Mobile458522
Platform444921
Total2122222287

API reference

Props

PivotTable props
NameTypeDefaultDescription
labelrequiredstring—What the table is, for its caption: "Revenue by region".
recordsrequiredPivotRecord[]——
fieldsrequiredPivotField[]—The fields records have: dimensions to group by, and measures to add up.
rowsrequiredstring—The field down the side to start with.
columnsstring | null—The field across the top to start with; none for a single column.
valuestring | null—The measure to add up to start with; without one, records are counted.
aggregateAggregate"sum"How the value is added up to start with; "sum" by default.
currencystring"USD"The currency for format: "currency"; "USD" by default.
fixedboolean—Hide the pickers: the table shows one fixed view.
idstring"pivot"Starts the ids inside it, unique on the page; "pivot" by default.
localestring—For the numbers; the browser's by default.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/pivot-table

Source