Scales, ticks, number and date formats, the palette, legend, tooltip, table view and states every chart shares.
Couldn't load this chart.
The analytics service didn't answer in time.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Chart } from "./ui/uicontext/components/charts/Chart.ts"import { ChartStatus } from "./ui/uicontext/components/charts/Data.ts"import { SegmentedControl } from "./ui/uicontext/components/SegmentedControl.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"import { signupsByPlan } from "./metrics.ts"const styles = stylex.create({ root: { display: "grid", gap: space.lg, inlineSize: "100%" } })const Shown = Schema.Literals(["ready", "loading", "refreshing", "error", "empty"])const labels = { ready: "Data", loading: "Loading", refreshing: "Refreshing", error: "Failed", empty: "Empty" } as constconst Message = UiSchema.TaggedEnum({ PickedChartState: { state: Shown }, RequestedRetrySignups: {},})// Every chart shares these states, in the plot's own height so nothing shifts: a quiet placeholder// on first load, the last data dimmed while it refreshes, a failure with Retry, and an empty period.export const ChartStates = Html.component({ name: "ChartStates", model: Schema.Struct({ state: Shown }), message: Message,}).make({ init: () => ({ state: "error" as const }), update: { PickedChartState: ($, { state }) => $.evolve({ state }), RequestedRetrySignups: ($) => $.evolve({ state: "ready" }), }, view: (h, model) => { const status: ChartStatus = model.state === "empty" ? "ready" : model.state return h.div.attrs(sx(styles.root)).children( SegmentedControl(h, { label: "State", value: model.state, options: Shown.literals.map((value) => ({ value, label: labels[value] })), onPick: (state) => Message.PickedChartState({ state: Schema.is(Shown)(state) ? state : "ready" }), }), Chart(h, { props: { type: "bar", stacked: "stacked", label: "Weekly signups by plan", series: model.state === "empty" ? signupsByPlan.map((s) => ({ ...s, points: [] })) : signupsByPlan, x: { kind: "time", unit: "week" }, status, error: "The analytics service didn't answer in time.", empty: "No signups in this period. Try a longer range.", }, onEmit: { RequestedRetry: () => Message.RequestedRetrySignups(), Highlighted: () => undefined, Selected: () => undefined, Toggled: () => undefined, }, }), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/chart-foundationUsage
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.
| Measure | In full | Short | Change | Axis |
|---|---|---|---|---|
| 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 |
| Seats | 1,284 | 1,284 | +1.7% | 0 · 500 · 1,000 · 1,500 |
| Requests | 48.2B | 48.2B | +6.4% | 0 · 20B · 40B · 60B |
| Churn | 2.1% | 2.1% | −18.7% | 0% · 1% · 2% · 3% |
| p95 latency | 413 ms | 413 ms | −7.5% | 0 · 200 · 400 · 600 ms |
| Net change | -$18,400 | -$18K | −182% | -$20K · -$10K · $0 · $10K · $20K |
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import type { ChartFormat } from "./ui/uicontext/components/charts/Data.ts"import { formatDelta, formatShort, formatValue, tickFormatter } from "./ui/uicontext/components/charts/Format.ts"import { niceTicks } from "./ui/uicontext/components/charts/Scale.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ table: { inlineSize: "min(100%, 40rem)", borderCollapse: "collapse", fontSize: font.sm }, th: { textAlign: "start", fontWeight: 500, color: color.muted, paddingBlock: space.sm, paddingInline: space.md, borderBottomWidth: 1, borderBottomStyle: "solid", borderBottomColor: color.line }, rowHead: { textAlign: "start", fontWeight: 500 }, td: { paddingBlock: space.sm, paddingInline: space.md, borderBottomWidth: 1, borderBottomStyle: "solid", borderBottomColor: color.line, fontVariantNumeric: "tabular-nums", overflowWrap: "anywhere" },})const rows: ReadonlyArray<{ readonly name: string; readonly value: number; readonly format: ChartFormat; readonly max: number }> = [ { name: "Revenue", value: 248_120.5, format: { style: "currency", currency: "USD" }, max: 260_000 }, { name: "Revenue in euros", value: 1_204_880, format: { style: "currency", currency: "EUR", digits: 0 }, max: 1_300_000 }, { name: "Seats", value: 1_284, format: { style: "number" }, max: 1_400 }, { name: "Requests", value: 48_213_904_117, format: { style: "compact" }, max: 50_000_000_000 }, { name: "Churn", value: 0.0214, format: { style: "percent" }, max: 0.03 }, { name: "p95 latency", value: 412.6, format: { style: "unit", unit: "millisecond", digits: 0 }, max: 500 }, { name: "Net change", value: -18_400, format: { style: "currency", currency: "USD", digits: 0 }, max: 20_000 },]// The same value in full (tooltips and tables), short (stats and labels), as a change, and the axis// it would sit on.export const Formats = <M>(h: Html.H<M>) => h.table.attrs(sx(styles.table)).children( h.thead.children(h.tr.children(...["Measure", "In full", "Short", "Change", "Axis"].map((c) => h.th.key(c).attrs({ scope: "col", ...sx(styles.th) }).children(c)))), h.tbody.children(...rows.map((row) => { const ticks = niceTicks(Math.min(0, row.value), row.max, 4) return h.tr.key(row.name).children( h.th.attrs({ scope: "row", ...sx(styles.td, styles.rowHead) }).children(row.name), h.td.attrs(sx(styles.td)).children(formatValue(row.value, row.format)), h.td.attrs(sx(styles.td)).children(formatShort(row.value, row.format)), h.td.attrs(sx(styles.td)).children(formatDelta(row.value / row.max - 0.9)), h.td.attrs(sx(styles.td)).children(ticks.values.map(tickFormatter(ticks.values, row.format)).join(" · ")), ) })), )API reference
LegendView function
Options
| Name | Type | Description |
|---|---|---|
labelrequired | string | — |
entriesrequired | LegendEntry[] | — |
toggle | ((id: string) => Message) | — |
style | Style | — |
TooltipView function
Options
| Name | Type | Description |
|---|---|---|
xrequired | number | — |
yrequired | number | — |
widthrequired | number | — |
titlerequired | string | — |
rowsrequired | TipRow[] | — |
total | string | — |
DataTableView function
Options
| Name | Type | Description |
|---|---|---|
captionrequired | string | — |
columnsrequired | string[] | The first column's header, then one per value column |
rowsrequired | ReadonlyArray<{ readonly key: string; readonly head: string; readonly cells: string[] }> | — |
ChartStateView function
Options
| Name | Type | Description |
|---|---|---|
statusrequired | ChartStatus | — |
heightrequired | number | — |
emptyrequired | boolean | — |
emptyTextrequired | string | What nothing-to-show means here: "No deploys in this period." |
error | string | — |
retry | Message | — |
allHidden | Message | Every series is hidden: offer them back |
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.