Charts and graphs · @jengacore/chart-figure
ChartFigure
The accessible frame for any chart: titled figure, a summary sentence wired with aria-describedby, the numbers as a visually hidden table, and colour-blind-safe series colours.
Alerts raised by hourAlerts peak at 10:00 with 31 and fall to 9 by 16:00.
| Hour | Alerts |
|---|---|
| 08:00 | 12 |
| 10:00 | 31 |
| 12:00 | 24 |
| 14:00 | 18 |
| 16:00 | 9 |
Install
terminal
npx shadcn@latest add @jengacore/chart-figureWithout the namespace configured: npx shadcn@latest add https://ui.jengacore.com/r/<new-york-v4|base-nova>/chart-figure.json
Registry dependencies
Source
components/jc/chart-figure.tsx
import * as React from "react"
import { cn } from "@/lib/utils"
/**
* Series colours in the order that keeps neighbouring series apart under deuteranopia, protanopia and tritanopia,
* in Light and Dark: blue, amber, green, magenta, grey. Two series: use the first two. Five series also need direct
* labels, a dash pattern or a gap between segments, because no five hues stay apart for every reader.
*/
export const SERIES_COLORS = ["var(--chart-2)", "var(--chart-3)", "var(--chart-1)", "var(--chart-5)", "var(--chart-4)"] as const
/** Line dash patterns that pair with SERIES_COLORS, so a line chart never relies on hue alone. */
export const SERIES_DASHES = [undefined, "6 3", "2 3", "8 3 2 3", "1 4"] as const
export const seriesColor = (index: number) => SERIES_COLORS[index % SERIES_COLORS.length] ?? "var(--chart-4)"
export const seriesDash = (index: number) => SERIES_DASHES[index % SERIES_DASHES.length]
export type ChartColumn<Row> = {
key: keyof Row & string
label: string
/** Formats the cell for the data table; defaults to toLocaleString("en") for numbers. */
format?: (value: Row[keyof Row], row: Row) => string
}
function cell<Row>(column: ChartColumn<Row>, row: Row) {
const value = row[column.key]
if (column.format) return column.format(value, row)
if (typeof value === "number") return value.toLocaleString("en")
return String(value ?? "")
}
/**
* The accessible frame every chart sits in: a figure named by its title, described by a one-sentence summary of what
* the chart shows (aria-describedby), and the same numbers as a data table. The table is visually hidden by default,
* so screen readers get every value; `showTable` puts it on screen too, for readers who prefer numbers.
*/
export function ChartFigure<Row extends Record<string, unknown>>({
title,
summary,
data,
columns,
showTable = false,
actions,
className,
children,
}: {
title: string
/** One sentence a reader could act on: the trend, the outlier, the total. Read with the chart. */
summary: string
data: readonly Row[]
/** The first column labels each row (the x axis or the category); the others are the series. */
columns: ChartColumn<Row>[]
showTable?: boolean
actions?: React.ReactNode
className?: string
children: React.ReactNode
}) {
const id = React.useId()
const titleId = `${id}-title`
const summaryId = `${id}-summary`
const [first, ...rest] = columns
return (
<figure
data-slot="chart-figure"
aria-labelledby={titleId}
aria-describedby={summaryId}
className={cn(
"grid min-w-0 gap-3 rounded-lg border border-border bg-card p-4 shadow-card contrast:border-input",
// Recharts makes the chart focusable (arrow keys move the tooltip); chart.tsx hides its outline, so restore one.
"[&_.recharts-surface:focus-visible]:outline-2! [&_.recharts-surface:focus-visible]:outline-offset-2! [&_.recharts-surface:focus-visible]:outline-ring! [&_.recharts-surface:focus-visible]:outline-solid!",
className
)}
>
<figcaption className="flex items-start gap-3">
<div className="grid min-w-0 gap-0.5">
<span id={titleId} className="text-[15px] leading-5 font-semibold">
{title}
</span>
<span id={summaryId} className="text-[13px] leading-[18px] text-muted-foreground">
{summary}
</span>
</div>
{actions ? <div className="ml-auto flex shrink-0 items-center gap-2">{actions}</div> : null}
</figcaption>
{children}
{first ? (
<div className={showTable ? "overflow-x-auto" : "sr-only"}>
<table className="w-full text-[12.5px] tabular-nums">
<caption className="sr-only">{`${title}: data table`}</caption>
<thead>
<tr className="border-b border-border text-left text-muted-foreground">
{columns.map((c) => (
<th key={c.key} scope="col" className="px-2 py-1 font-medium first:pl-0">
{c.label}
</th>
))}
</tr>
</thead>
<tbody>
{data.map((row, i) => (
<tr key={`${cell(first, row)}-${i}`} className="border-b border-border last:border-0">
<th scope="row" className="px-2 py-1 pl-0 text-left font-normal">
{cell(first, row)}
</th>
{rest.map((c) => (
<td key={c.key} className="px-2 py-1">
{cell(c, row)}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
) : null}
</figure>
)
}
/**
* A chart block's title and summary describe its numbers, so a block given its own `data` must be given its own
* `title` and `summary` too; the defaults describe the sample data only.
*/
export type ChartCopy<Data> =
| { data?: undefined; title?: string; summary?: string }
| { data: Data; title: string; summary: string }
export type ChartSeries = {
/** Key of the value in each data row. Letters, digits, - and _ only: it becomes a CSS variable name. */
key: string
label: string
/** Defaults to SERIES_COLORS in order. */
color?: string
}
/** A ChartConfig for <ChartContainer> from a series list, coloured in the colour-blind-safe order. */
export function seriesConfig(series: readonly ChartSeries[]) {
return Object.fromEntries(series.map((s, i) => [s.key, { label: s.label, color: s.color ?? seriesColor(i) }])) as Record<
string,
{ label: string; color: string }
>
}
/** ("Mobile money", 2) -> "mobile-money-2": a unique ChartConfig key and CSS variable suffix for a category. */
export const slug = (name: string, index: number) => `${name.toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-|-$/g, "") || "item"}-${index}`