Design System
Data display

Chart

The Recharts wrapper carrying HMS chart tokens, so every series colour comes from the palette rather than from a component.

Purpose

ChartContainer is the bridge between recharts and the design system. You still compose the chart from recharts marks — BarChart, AreaChart, XAxis — but the container supplies what recharts does not: a responsive frame, themed grid lines and axis text, and a config that turns each series into a CSS variable so colours come from the palette (--chart-1 … --chart-5) instead of being hardcoded per component.

For the common dashboard tile — a titled card with a headline value, trend pill and area chart — use the Chart card widget rather than composing it by hand. For a headline figure without a chart, that is Stat card.

Example

config maps each series key to its label and colour; the container exposes each colour as --color-<key>, which the marks read back.

Claims by month
import {
  ChartContainer,
  ChartTooltip,
  ChartTooltipContent,
  type ChartConfig,
} from "@healthmetrics/ui/chart";
import { Bar, BarChart, CartesianGrid, Legend, XAxis } from "recharts";

const config = {
  approved: { label: "Approved", color: "var(--chart-1)" },
  pending: { label: "Pending", color: "var(--chart-2)" },
} satisfies ChartConfig;

<ChartContainer config={config} label="Approved and pending claims by month, bar chart">
  <BarChart data={monthly}>
    <CartesianGrid vertical={false} />
    <XAxis dataKey="month" tickLine={false} axisLine={false} tickMargin={8} fontSize={11} />
    <ChartTooltip content={<ChartTooltipContent />} />
    <Legend />
    <Bar dataKey="approved" fill="var(--color-approved)" radius={4} />
    <Bar dataKey="pending" fill="var(--color-pending)" radius={4} />
  </BarChart>
</ChartContainer>;

The same container drives any recharts chart type. An area trend, with a gradient fill built on the series variable:

Area trend
<ChartContainer
  config={{ claims: { label: "Claims", color: "var(--chart-1)" } }}
  label="Claims submitted per month, area chart"
>
  <AreaChart data={trend}>
    <defs>
      <linearGradient id="fill-claims" x1="0" y1="0" x2="0" y2="1">
        <stop offset="0%" stopColor="var(--color-claims)" stopOpacity={0.3} />
        <stop offset="100%" stopColor="var(--color-claims)" stopOpacity={0.02} />
      </linearGradient>
    </defs>
    <CartesianGrid vertical={false} />
    <XAxis dataKey="month" tickLine={false} axisLine={false} tickMargin={8} fontSize={11} />
    <ChartTooltip content={<ChartTooltipContent />} />
    <Area dataKey="claims" type="monotone" stroke="var(--color-claims)" fill="url(#fill-claims)" strokeWidth={2} />
  </AreaChart>
</ChartContainer>;

ChartTooltip and ChartLegend are recharts' Tooltip and Legend re-exported unchanged; pair the tooltip with ChartTooltipContent to get the design-system tooltip, which reads its labels from your config. The whole composition is client-side — put charts in a "use client" module.

Structure

The tokens behind the frame.

PropertyValue
Frame16:9 (aspect-video) responsive; override with className
Palette--chart-1 … --chart-5, retuned for contrast in dark mode
Series varseach config colour exposed as --color-<key> on the container
Grid lines--border at 60%
Axis ticks12px, --muted-foreground
Tooltip--popover surface, 1px --border, --shadow-8, tabular-num values

Best practices

Do

  • Take every series colour from the palette — var(--chart-1) … var(--chart-5) via config, never a hex value in a component.
  • Keep it to five series or fewer; the palette has five steps because more than that stops being readable.
  • Use ChartTooltipContent so tooltips look the same on every dashboard.
  • Format values before charting — a tooltip showing 85 where users expect MYR 85.00 is a data bug, not a styling one.

Don't

  • Rebuild the titled dashboard tile — Chart card is that composition.
  • Pass more than one chart element as children — the container renders exactly one, sized to the frame.
  • Distinguish series by colour alone — the legend and tooltip labels must carry the names.
  • Reach for a chart when a single number answers the question — that is a Stat card.

Accessibility & keyboard

  • Name every chart with the label prop: recharts renders an unnamed SVG, and label turns the container into one named role="img" — "Claims by month, bar chart". Without it a screen reader finds an anonymous graphic.
  • Tooltips are hover-driven and invisible to assistive tech. When the chart is the only source of the numbers, provide them in accessible form too — a Table or Description list nearby.
  • Series colours meet contrast in both themes, but colour is still the weakest channel — keep the legend on multi-series charts.
  • Keyboard: none by default. Recharts' own accessibilityLayer prop on the chart element (e.g. <BarChart accessibilityLayer>) opts into keyboard focus and arrow-key navigation between data points; the wrapper neither adds nor blocks it.

Props

PropTypeDefaultDescription
configrequiredChartConfig—Maps each series key to its label, colour and optional icon; colours are exposed as `--color-<key>`.
labelstring | undefined—Accessible name for the chart. Recharts renders an unnamed `<svg>`, so without this a screen reader finds an anonymous graphic. When set, the container becomes one named `role="img"` ("Claims by month, bar chart") — pair the chart with a table or figure text for the underlying numbers.
childrenrequiredReact.ReactElement<unknown, string | React.JSXElementConstructor<any>>—A single recharts chart element rendered inside the responsive container.

ChartConfig is Record<string, { label?, color?, icon? }> — one entry per series key. ChartTooltip and ChartLegend accept the standard recharts Tooltip/Legend props; ChartTooltipContent takes the recharts tooltip render props and needs no configuration of its own. useChart() reads the active config inside custom chart children.

Chart in Storybook

On this page