Demos

A bar chart with a tooltip.

Usage

Use a chart to show how a value changes over time or compares across groups. The chart components do not draw anything themselves. They add theme colours, a tooltip, and a legend to Recharts, which you use for the chart.

Install recharts in the app that renders the chart.

Import the wrappers from @workspace/ui/components/chart.

Anatomy

  • ChartContainer — Sizes the chart, applies theme styles, and holds the config. Takes one Recharts chart as its child.
  • ChartTooltip and ChartTooltipContent — The Recharts tooltip and a themed body for it.
  • ChartLegend and ChartLegendContent — The Recharts legend and a themed body for it.
  • ChartStyle — Writes the colour variables. ChartContainer already renders it.

Config

config maps each data key to a label and a colour. Each colour becomes a CSS variable named after the key, so steps is var(--color-steps). Use these variables for fill and stroke.

Use theme instead of color to set a different colour for light and dark mode.

Notes

  • Give ChartContainer a height. It is aspect-video by default.
  • --chart-5 matches the primary colour. --chart-1 and --chart-2 are almost white in light mode, so do not use them for marks.
  • Add accessibilityLayer to the Recharts chart so the reader can move through the data with the arrow keys.
  • Put units in the label, such as LDL-C (mg/dL). The tooltip shows labels from config.

Examples

Line

A trend for one biomarker. The tooltip uses the line indicator.

Legend

A stacked area chart with two series and a legend.

Props

ChartContainer

PropTypeDefaultDescription
configChartConfig—Labels and colours for each data key. Required.
idstringgeneratedScopes the colour variables.
initialDimension{ width, height }320 × 200Size used before the container is measured.
classNamestring—Classes merged onto the wrapper. Set the height here.

ChartTooltipContent

PropTypeDefaultDescription
indicator"dot", "line", or "dashed"dotShape of the colour mark beside each value.
hideLabelbooleanfalseHides the heading.
hideIndicatorbooleanfalseHides the colour marks.
nameKeystring—Data field to use for each row's name.
labelKeystring—Data field to use for the heading.
labelFormatter(label, payload) => ReactNode—Formats the heading.
formatter(value, name, item, index, payload) => ReactNode—Replaces each row.

ChartLegendContent

PropTypeDefaultDescription
hideIconbooleanfalseShows a colour square even when the config has an icon.
nameKeystring—Data field to use for each item's label.
verticalAlign"top" or "bottom"bottomWhich side the spacing goes on.

ChartConfig

FieldTypeDescription
labelReactNodeShown in the tooltip and legend.
colorstringAny CSS colour.
theme{ light, dark }Colours per theme. Use instead of color.
iconComponentTypeShown instead of the colour mark.