Chart
Beautiful charts built on recharts, coloured by the active theme.
import {
type ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@dapplelight/signet-ui/components/chart"
import { Bar, BarChart, CartesianGrid, XAxis } from "recharts"
const data = [
{ month: "Jun", revenue: 31200, expenses: 24100 },
{ month: "Jul", revenue: 35800, expenses: 25300 },
{ month: "Aug", revenue: 33900, expenses: 26800 },
{ month: "Sep", revenue: 40100, expenses: 27200 },
{ month: "Oct", revenue: 43000, expenses: 28900 },
{ month: "Nov", revenue: 48200, expenses: 29400 },
]
const config = {
revenue: { label: "Revenue" },
expenses: { label: "Expenses" },
} satisfies ChartConfig
export function Demo() {
return (
<ChartContainer config={config} className="aspect-[2/1] w-full">
<BarChart data={data}>
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} />
<ChartTooltip content={<ChartTooltipContent />} />
<Bar dataKey="revenue" fill="var(--color-revenue)" radius={4} />
<Bar dataKey="expenses" fill="var(--color-expenses)" radius={4} />
</BarChart>
</ChartContainer>
)
}Installation
npm i @dapplelight/signet-uiUsage
import {
type ChartConfig,
ChartContainer,
ChartLegend,
ChartLegendContent,
ChartTooltip,
ChartTooltipContent,
} from "@dapplelight/signet-ui/components/chart"
import { Bar, BarChart, XAxis } from "recharts"
import { config, data } from "./revenue"
<ChartContainer config={config} className="aspect-[2/1] w-full">
<BarChart data={data}>
<XAxis dataKey="month" />
<ChartTooltip content={<ChartTooltipContent />} />
<Bar dataKey="revenue" fill="var(--color-revenue)" />
</BarChart>
</ChartContainer>Examples
Line
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@dapplelight/signet-ui/components/chart"
import { CartesianGrid, Line, LineChart, XAxis } from "recharts"
import { config, data } from "./revenue"
<ChartContainer config={config} className="aspect-[2/1] w-full">
<LineChart data={data}>
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} />
<ChartTooltip content={<ChartTooltipContent indicator="line" />} />
<Line dataKey="revenue" stroke="var(--color-revenue)" strokeWidth={2} dot={false} />
<Line dataKey="expenses" stroke="var(--color-expenses)" strokeWidth={2} dot={false} />
</LineChart>
</ChartContainer>Area
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@dapplelight/signet-ui/components/chart"
import { Area, AreaChart, CartesianGrid, XAxis } from "recharts"
import { config, data } from "./revenue"
<ChartContainer config={config} className="aspect-[2/1] w-full">
<AreaChart data={data}>
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} />
<ChartTooltip content={<ChartTooltipContent hideLabel />} />
<Area
dataKey="revenue"
stroke="var(--color-revenue)"
fill="var(--color-revenue)"
fillOpacity={0.2}
/>
</AreaChart>
</ChartContainer>Legend
import { ChartLegend, ChartLegendContent } from "@dapplelight/signet-ui/components/chart"
<ChartLegend content={<ChartLegendContent />} />Pace
PaceChart shows usage against an even pace and a limit: the even-pace line (dashed, grey), what is used so far (solid), a projection from Now to where use would hit the limit (dashed, amber), a Now marker and the limit point. Segments are straight, gridlines are horizontal, and the card header takes any node in slot, here Day, Week and Month tabs.
import { PaceChart } from "@dapplelight/signet-ui/components/chart"
import { Tabs, TabsList, TabsTrigger } from "@dapplelight/signet-ui/components/tabs"
const week = [
{ label: "Mon", name: "Monday", used: 60 },
{ label: "Tue", name: "Tuesday", used: 130 },
{ label: "Wed", name: "Wednesday", used: 220 },
{ label: "Thu", name: "Thursday", used: 320 },
{ label: "Fri", name: "Friday" },
{ label: "Sat", name: "Saturday" },
{ label: "Sun", name: "Sunday" },
]
<PaceChart
title="API requests"
yAxisTitle="Requests"
data={week}
limit={500}
slot={
<Tabs defaultValue="week">
<TabsList>
<TabsTrigger value="day">Day</TabsTrigger>
<TabsTrigger value="week">Week</TabsTrigger>
<TabsTrigger value="month">Month</TabsTrigger>
</TabsList>
</Tabs>
}
/>Use is assumed to continue at its average rate so far, and even pace is limit divided evenly over the periods. Below the chart a visually hidden list gives one sentence per part (for example "Projected: reaches 500 on Saturday."), and the chart itself carries them as its aria-label.
Without a limit there is no pace line, projection or limit point. Once use is over the limit there is no projection and the limit point stays at the end. With nothing used there is no Now marker.
API Reference
Import from @dapplelight/signet-ui/components/chart only. It is not in the barrel because recharts (version 3.8 up to 4) is an optional peer dependency. Colours come from the theme's optional --chart-1 to --chart-6, each falling back to a core token.
ChartContainer
| Prop | Type | Default | Description |
|---|---|---|---|
config | ChartConfig | required | Maps each series key to a label, an optional icon, and a color or per-theme theme. Defines the --color-<key> variable. |
children | recharts chart | required | One recharts chart, such as BarChart. |
id | string | generated | Stable id for the generated style block. |
className | string | - | Merged last. Default is aspect-video. |
A series with neither color nor theme takes the palette entry at its position, wrapping after six.
PaceChart
| Prop | Type | Default | Description |
|---|---|---|---|
data | { label: string; name?: string; used?: number | null }[] | [] | One point per period, with the running total in used up to Now. label is the axis tick, name is what a screen reader says (default label). |
limit | number | - | The cap. Without it, only the used line and Now show. |
now | string | last period with used | The label of the Now period. |
title | ReactNode | - | Card title. |
slot | ReactNode | - | Any node, right of the title; below it on phones. |
yAxisTitle | string | - | Title of the Y axis. |
format | (n: number) => string | toLocaleString | Formats axis ticks, the limit label and the descriptions. |
state | ready | loading | error | ready | Loading shows a skeleton; the header and slot stay. Empty data shows an empty state. |
onRetry | () => void | - | Retry button of the error state. |
labels | Partial<PaceLabels> | English | Text of each part (pace, used, projected, now, limit) and of the states. The limit label reads <value> <labels.limit>. |
colors | Partial<Record<part, string>> | theme tokens | A CSS colour per part. |
hide | Partial<Record<part, boolean>> | - | Hide a part. |
descriptions | Partial<Record<part, string>> | - | Replace the screen-reader sentence of a part. |
classNames | Partial<Record<part | "header" | "chart", string>> | - | className per part, the header and the chart area. |
className | string | - | On the card. |
part is one of pace, used, projected, now, limit. paceModel(data, limit, now) returns the numbers behind the chart.
ChartConfig
Record<string, { label?: ReactNode; icon?: ComponentType } & ({ color?: string } \| { theme: { light: string; dark: string } })>
ChartTooltipContent
Pass as content of ChartTooltip, which is recharts' Tooltip.
| Prop | Type | Default | Description |
|---|---|---|---|
indicator | dot | line | dashed | dot | Shape of the series marker. |
hideLabel | boolean | false | Hide the heading. |
hideIndicator | boolean | false | Hide the marker. |
nameKey | string | - | Payload key used to look up the series label. |
labelKey | string | - | Payload key used to look up the heading. |
labelFormatter | (value, payload) => ReactNode | - | Format the heading. |
formatter | recharts formatter | - | Replace each row. |
color | string | - | Override the marker colour. |
className | string | - | Merged last. |
ChartLegendContent
Pass as content of ChartLegend, which is recharts' Legend.
| Prop | Type | Default | Description |
|---|---|---|---|
hideIcon | boolean | false | Use a swatch instead of the config icon. |
nameKey | string | - | Payload key used to look up the series label. |
verticalAlign | top | bottom | bottom | Spacing side. Set by recharts. |
className | string | - | Merged last. |
Other exports
ChartTooltip and ChartLegend re-export recharts' Tooltip and Legend. CHART_COLORS is the default palette and ChartStyle writes the colour variables.