Skip to content

Stacked Bar

A stacked bar chart answers a different question than a plain bar. A plain bar compares totals: which category is biggest. A stacked bar keeps that comparison but splits each bar into the parts it is made of — so you read the total and its composition in the same place. Revenue by quarter, split by product line. Headcount by department, split by team. Energy by year, split by source. Composition over a category axis is the most common analytics figure after the plain line.

It is a mode of the Bar chart, not a separate class. Hand Bar a set of series instead of a flat list and it stacks them. It draws vertically (the default) or horizontally — set orientation: 'horizontal' for a ranked, composed list; axes and grid behave as they do for an ordinary bar, and on first render the segments fade in (they do not grow, and stagger does not apply).

This is composition within a category — several series sharing one axis. That is a different shape from the hierarchical bar, which breaks a single total down a tree of parents and children. Same instinct — “what is this made of?” — different structure: stacking adds a series dimension, a hierarchy adds depth.

Data format

Pass an array of series, each with a name and its values:

[
    { name: 'Cloud',    values: [{ label: 'Q1', value: 42 }, { label: 'Q2', value: 48 }] },
    { name: 'Licenses', values: [{ label: 'Q1', value: 30 }, { label: 'Q2', value: 29 }] },
    { name: 'Services', values: [{ label: 'Q1', value: 18 }, { label: 'Q2', value: 20 }] },
]

Bar switches into stacked mode automatically when it sees this series-major shape (a values array on the first item), so there is no separate class to import and no flag to remember for the common case. The categories are the ordered union of every series’ labels — a series that skips a category simply contributes nothing there, and the stack closes up. Segments stack in the order the series are given, bottom to top (or left to right when horizontal).

Values are expected to be non-negative — a stacked bar encodes cumulative length, and a negative slice has no coherent place in the pile. For signed comparisons (before/after, gains/losses around a baseline) use a diverging bar instead. A negative, missing or non-numeric value counts as zero and draws no segment.

Percent (100%) mode

When the story is the shifting share rather than the absolute total, normalize every bar to fill the axis: pass stacked: 'percent'. Each category then reads as parts of 100%, which makes a changing mix legible even when the totals move underneath it.

new RareCharts.Bar('#chart', {
    stacked: 'percent',
}).setData(series);

Orientation and legend

Both orientations work. vertical (default) reads as composition over time; horizontal reads as a ranked, composed list and handles long category labels better. Set it the usual way:

new RareCharts.Bar('#chart', { orientation: 'horizontal' }).setData(series);

A legend is built automatically from the series names, colored to match the segments, and each item hides and shows its series: a click toggles it, a double click keeps it alone, Show all brings all back. A hidden series leaves the stacks — the segments above close up — and its category keeps its slot. The value axis runs to the tallest stack shown, and stacked: 'percent' bars are shares of what is shown; legendRescale: false keeps the axis on every series and the percents of the whole bar. Pass your own legend array to override the items — see Interactive legend for the format and the gestures.

Colors and tooltips

Each series takes the next color from the theme palette, in order. Override a single series with a color on it:

{ name: 'Cloud', color: '#00c97a', values: [ ... ] }

Colors belong to series here: a color on a single value is ignored. Per-bar colors are for the single-series modes.

Hovering a segment shows its category, series name, value, and share of that category’s total. Customize it with tooltipFormat: (seg) => html, where seg is { i, name, cat, v0, v1, value, total }: the series index and name, the category, where the segment starts and ends on the stack, its value, and the category total. It also carries the role fields every formatter gets: label (the category), series (the series name), color and percent (the share of the category total).

Stacked options

Stacked bars share the Bar chart’s options — grid, axis visibility, yTickFormat / xTickFormat, yPrefix / ySuffix (vertical, not in 'percent' mode), animate / duration / ease, labelMaxLength, and margins all apply; barColor, showValues and stagger do not. The date-only options — timeframes, navigator, annotations — do not apply: stacked bars have no date axis, so the chart hides the timeframe buttons and the navigator strip (annotations are skipped with one console warning). The options specific to stacking:

Option Type Default Description
stacked true | 'percent' auto Stacking is enabled automatically by the series-major data shape. Set 'percent' to normalize each bar to 100%.
orientation 'vertical' | 'horizontal' 'vertical' Stack upward (vertical) or rightward (horizontal).
barWidth number — Width of the whole column in px (its thickness when horizontal); every segment shares it. Capped at the space per category, so neighbouring columns touch but never overlap.
legend array auto Auto-built from series names. Pass your own to override.
yTickFormat / xTickFormat function by tick step ('percent': .0%) (value) => string for the value axis (Y in vertical, X in horizontal). Defaults to labels by the tick step (0 25 50 75 100; SI from 1,000, exact on the divided axis: 0.375k) — with yPrefix / ySuffix on a vertical axis — or a percent format in 'percent' mode.
yTicks / xTicks number 4 Value-axis interval count — the niced domain is divided into exactly this many intervals. yTickValues / xTickValues override with explicit positions.
valueFormat function comma number (seg) => string — formats the value inside the default segment tooltip; seg as for tooltipFormat.
tooltipFormat function — (seg) => html where seg is { i, name, cat, v0, v1, value, total } plus the role fields label (= cat), series (= name), color and percent (value / total) — formatter arguments. Default shows the category, the series and value, and the share of the category total.