Bar Chart
A bar chart recipe - Recharts drawn with Appica UI tokens, for comparing a measure across categories.
A bar chart compares 1 measure across categories or periods. Like every chart here it is a recipe: Recharts draws it and the chart layer themes it. Each example below is a complete file to paste and adapt.
Installation
Recharts is the only extra dependency, with react-is at the same version as react:
pnpm add recharts react-isThe examples import the chart layer from @/components/chart. If your project doesn't have it yet, copy it from the Charts page first.
Examples
Default
Bars with a 4 px outer radius from chartMarks.barRadius, and a maxBarSize so a chart with few bars doesn't grow fat ones. On mount they grow from the baseline one after another.
Stacked
BarStack stacks the bars inside it and rounds the stack as 1 shape, where a radius on each Bar would round every segment. A stroke in the surface color separates the segments.
Grouped
Without a BarStack, bars that share a category sit side by side. barGap sets the space between them.
Horizontal
A ranking reads best as horizontal bars, sorted largest first, with room for long labels. Recharts calls this layout="vertical": the category axis is the YAxis, and its width="auto" fits the longest label.
Negative
For values on both sides of zero, color carries the sign: each row has its own fill, the success color of chartColors above the line and the error color below. A ReferenceLine marks zero.
Accessibility
- Pass
aria-labelto the chart with what it shows ("Orders by month"). It becomes the accessible name of the SVG. - The chart takes focus, and the arrow keys move the tooltip from bar to bar.
- A chart with 2 or more series has a
Legend, and the tooltip names every series, so color is never the only cue. In the diverging chart the sign is in the value too. - Nothing animates under reduced motion.