Skip to content
EspañolEnglish
All components (283)

Stacked bars

Data · StackedBarChart

What it is and when to use it

Stacked bars: one stack per category with a segment per series, to see at once the total and what it is made of ("sales per month and per channel"). It shares the data, the scale, the legend and the table with LineChart and AreaChart.

The drawing is decoration and the table is the data: below it there is a real table (one row per category, one column per series), hidden from view unless tableVisible is set. Hovering a stack shows the value of each series and the total.

When to use it

  • A few categories (months, shops, weeks) where the total of each one and the split between series matter.
  • The order of series is the order of the stack: the first one goes at the bottom. Values are positive: a negative or a null draws no segment and does not add to the total.
  • The axis always starts at zero; min and max fix the scale by hand, to compare two charts.
  • format writes the values (euros, units) on the axis, on hover, in the summary and in the table; categoryName is the header of the first column of the table.
  • percent makes each stack fill the height and show the share of each series over the total of its category (“at 100 %”): the axis, the tooltip and the summary speak in percentages, and the table and the tooltip total stay in the values. renderTooltip paints the tooltip your way with the category, one ready-written row per series and the total. animated (the default) grows the stacks from the base and slides the segments when the data change; with reduced motion nothing is animated.

When not to

  • A single series, or comparing categories without a split: BarChart.
  • The evolution over many points: AreaChart with stacked, or LineChart.
  • Parts of a total at a single moment: DonutChart.

Keyboard: Tab moves to the drawing (a single tab stop) and the arrow keys move from point to point; Home and End jump to the first and last, and Escape leaves. A spoken announcement gives each point with its position (“Feb, Web: 20. 2 of 3”). Up and down change series. With toggleSeries the legend is a row of buttons (Tab and Enter or Space hide or restore a series) and with onPointPress every value in a category tooltip is a button reached with Tab. The complete data is in the table.

Accessibility: the drawing is an image (role="img") with a written summary of each series (where it goes from and to and where it peaks) and the table is always there for the screen reader. Colour is never the only clue: the legend (by default, only with more than one series) and the summary carry the name of each series, and each segment is separated from the one above by a line in the background colour.

Fine-tuning: CSS variables --amset-series-*: -axis, -bar (the widest a stack can be), -cap, -footer, -height and -label. The size moves the height (10, 14 or 18 rem) and the maximum width of the stack; height wins if passed. The colour of each series comes from its tone.

What it looks like

Chart type
Use the arrow keys to move through the values, Home and End to jump to the first and last, and Escape to leave.
  • Store
  • Online
  • Wholesale
Sales by month and channel
MonthStoreOnlineWholesale
Jul6,9005,1001,900
Aug5,2005,6002,400
Sep7,8006,3002,800

<StackedBarChart
  label="Sales by month and channel"
  categories={['Jul', 'Aug', 'Sep']}
  series={canales}
  categoryName="Month"
  tableVisible
/>

Props

Import it with import { StackedBarChart } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
categoriesrequiredreadonly string[]What goes on the bottom axis: the months, the days. One per value of each series.
labelrequiredstringWhat is being measured. Required: a chart without a title does not inform.
seriesrequiredreadonly ChartSeries[]—
animatedbooleanAnimates the drawing: lines are drawn from one side to the other, stacks grow from the base and, when the data change, segments and points slide to their new place instead of jumping. With reduced motion it is never animated. `true` by default.
axesbooleanBackground lines with their number.
categoryNamestringWhat the first column of the table is called. By default, "Period".
classNamestringMerged with the library classes; it does not replace them.
format(value: number) => string—
groupedbooleanThe bars of each category sit side by side, each from zero, instead of stacked. `false` by default.
heightnumberHeight of the drawing in pixels. By default, that of the size.
hiddenSeriesreadonly string[]The keys of the hidden series, when the chart's user keeps them. Without it, the chart keeps them itself.
legendbooleanThe legend with the name of each series. By default, only if there is more than one.
maxnumber—
minnumberMinimum and maximum of the axis set by hand, to compare two charts on the same scale.
onHiddenSeriesChange(hidden: readonly string[]) => voidCalled with the hidden keys after a legend entry is pressed.
onPointPress(point: ChartPoint) => voidMakes every value pressable (in the tooltip of its category, also by keyboard and touch) and hands over the pressed point.
percentbooleanEach stack fills the height and shows the share of each series over the total of its category (“at 100 %”). The axis, the tooltip and the summary speak in percentages; the table and the tooltip total stay in the values. `false` by default.
referenceBandsreadonly ChartReferenceBand[]Horizontal reference bands: an acceptable range.
referenceLinesreadonly ChartReferenceLine[]Horizontal reference lines: a target, an average.
renderTooltip(info: ChartTooltipInfo) => ReactNodePaints the tooltip that appears when passing over a category (and with the keyboard, when the series are pressable) instead of the built-in one. It receives the category, one row per series with its value already written and the total; if the row has to be pressable, whoever paints it adds that. It is only the view: the screen reader still receives the summary and the table.
size"sm" | "md" | "lg"—
tableVisiblebooleanAlso shows the table. For the screen reader it is always there.
toggleSeriesbooleanThe legend stops being a label and each entry becomes a button that hides or restores its series. `false` by default.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: