Skip to content
IndexRequest a UIAboutContact

Stacked Bars with Series Toggle

Stacked columns where legend buttons switch series on and off and the chart rescales smoothly.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Build a stacked column chart, 300x170px, with five quarters (Q1 to Q5) and three series (Product, Services, Support) using three shades (#171717, #8c8c87, #d4d4d4). Compute each column as a flex column of segments whose heights are the series value divided by the largest visible column total, so hiding a series rescales the whole chart; animate segment heights over 400ms cubic-bezier(.2,.7,.2,1). Below the chart place three legend toggle buttons (swatch plus name) with aria-pressed; pressing one hides or shows that series, and the last visible series cannot be turned off (its button gets aria-disabled and a short note). Each column has an aria-label listing the visible values, e.g. "Q3: Product 40, Services 22". Hovering a segment shows its value in a small readout line under the chart. Under prefers-reduced-motion remove transitions.

About this pattern

Definition

Columns divided into stacked segments, one per series. Legend buttons show or hide a series, and the remaining segments animate to the new proportions.

Purpose & UX principles

  • Lets people isolate one series to compare it across columns.
  • Toggle buttons use aria-pressed and always keep at least one series on, so the chart is never empty.
  • Segment values are in the accessible name of each column, so the data is available without the visual.

Interaction states

  • All series on, some off (button looks inactive), only one series left (its button is locked).

Motion specification

  • Heights and offsets animate 400ms cubic-bezier(.2,.7,.2,1) when a series toggles or the scale changes.
  • Reduced motion: instant update.

Responsive behaviour

  • Columns share the width; the legend wraps under the chart on narrow widths.

Code

HTML
<div class="box">
  <div class="ch" id="ch" role="group" aria-label="Revenue by quarter"></div>
  <p class="read" id="read" aria-live="polite">Hover a segment</p>
  <div class="lg" id="lg"></div>
</div>

Related patterns

Interactive Bar Chart

↗
Interactive Bar ChartData Visualization

Donut Chart with Linked Legend

↗
Donut Chart with Linked LegendData Visualization

Sortable Ranking Bars

↗
Sortable Ranking BarsData Visualization