Chart Foundations

View as Markdown

The scales, formats, legend and tooltip every chart shares.

When to use

The scales, formats, legend and tooltip every chart shares. Use Line Chart instead when you want a finished chart, not its parts.

Usage

import { formatShort, formatTime, formatValue, tickFormatter } from "./ui/uicontext/components/charts/Format.ts"import { niceTicks } from "./ui/uicontext/components/charts/Scale.ts"
const usd = { style: "currency", currency: "USD" } as constexport const revenueText = formatValue(248_120, usd) // "$248,120.00"export const revenueShort = formatShort(248_120, usd) // "$248.1K"export const month = formatTime(Date.UTC(2026, 10, 1), "month") // "November 2026"const ticks = niceTicks(0, 248_120, 5) // 0, 50,000 … 250,000export const axisLabels = ticks.values.map(tickFormatter(ticks.values, usd)) // "$0", "$50K" … "$250K"

Examples

Formats

Every value goes through Intl, so another locale gets its own digits and symbols.

MeasureIn fullShortChangeAxis
Revenue$248,120.50$248.1K+5.4%$0 · $100K · $200K · $300K
Revenue in euros€1,204,880€1M+2.7%€0 · €500K · €1M · €1.5M
Seats1,2841,284+1.7%0 · 500 · 1,000 · 1,500
Requests48.2B48.2B+6.4%0 · 20B · 40B · 60B
Churn2.1%2.1%−18.7%0% · 1% · 2% · 3%
p95 latency413 ms413 ms−7.5%0 · 200 · 400 · 600 ms
Net change-$18,400-$18K−182%-$20K · -$10K · $0 · $10K · $20K

API reference

LegendView function

Options

Legend options
NameTypeDescription
labelrequiredstring—
entriesrequiredLegendEntry[]—
toggle((id: string) => Message)—
styleStyle—

TooltipView function

Options

Tooltip options
NameTypeDescription
xrequirednumber—
yrequirednumber—
widthrequirednumber—
titlerequiredstring—
rowsrequiredTipRow[]—
totalstring—

DataTableView function

Options

DataTable options
NameTypeDescription
captionrequiredstring—
columnsrequiredstring[]The first column's header, then one per value column
rowsrequiredReadonlyArray<{ readonly key: string; readonly head: string; readonly cells: string[] }>—

ChartStateView function

Options

ChartState options
NameTypeDescription
statusrequiredChartStatus—
heightrequirednumber—
emptyrequiredboolean—
emptyTextrequiredstringWhat nothing-to-show means here: "No deploys in this period."
errorstring—
retryMessage—
allHiddenMessageEvery series is hidden: offer them back

Keyboard and accessibility

  • Every chart is a named figure with a hidden table of its data, and View as table shows that table instead of the plot.
  • The legend's entries are toggle buttons (aria-pressed). A hidden series keeps its color when it comes back.
  • Loading is a status, a failure is an alert with Retry, and both keep the plot's height so nothing moves.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/chart-foundations

Source