Skip to Content

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-ui

Usage

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.

API requests
  • On pace: even use reaches 500 by Sunday.
  • Used so far: 320 by Thursday.
  • Projected: reaches 500 on Saturday.
  • Now: Thursday.
  • Limit: 500.
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

PropTypeDefaultDescription
configChartConfigrequiredMaps each series key to a label, an optional icon, and a color or per-theme theme. Defines the --color-<key> variable.
childrenrecharts chartrequiredOne recharts chart, such as BarChart.
idstringgeneratedStable id for the generated style block.
classNamestring-Merged last. Default is aspect-video.

A series with neither color nor theme takes the palette entry at its position, wrapping after six.

PaceChart

PropTypeDefaultDescription
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).
limitnumber-The cap. Without it, only the used line and Now show.
nowstringlast period with usedThe label of the Now period.
titleReactNode-Card title.
slotReactNode-Any node, right of the title; below it on phones.
yAxisTitlestring-Title of the Y axis.
format(n: number) => stringtoLocaleStringFormats axis ticks, the limit label and the descriptions.
stateready | loading | errorreadyLoading shows a skeleton; the header and slot stay. Empty data shows an empty state.
onRetry() => void-Retry button of the error state.
labelsPartial<PaceLabels>EnglishText of each part (pace, used, projected, now, limit) and of the states. The limit label reads <value> <labels.limit>.
colorsPartial<Record<part, string>>theme tokensA CSS colour per part.
hidePartial<Record<part, boolean>>-Hide a part.
descriptionsPartial<Record<part, string>>-Replace the screen-reader sentence of a part.
classNamesPartial<Record<part | "header" | "chart", string>>-className per part, the header and the chart area.
classNamestring-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.

PropTypeDefaultDescription
indicatordot | line | dasheddotShape of the series marker.
hideLabelbooleanfalseHide the heading.
hideIndicatorbooleanfalseHide the marker.
nameKeystring-Payload key used to look up the series label.
labelKeystring-Payload key used to look up the heading.
labelFormatter(value, payload) => ReactNode-Format the heading.
formatterrecharts formatter-Replace each row.
colorstring-Override the marker colour.
classNamestring-Merged last.

ChartLegendContent

Pass as content of ChartLegend, which is recharts' Legend.

PropTypeDefaultDescription
hideIconbooleanfalseUse a swatch instead of the config icon.
nameKeystring-Payload key used to look up the series label.
verticalAligntop | bottombottomSpacing side. Set by recharts.
classNamestring-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.