# Stacked bars

> Use stacked bars to compare totals across items and see what each total is made of, with a switch to compare the mix instead.

- Data type: Shares of a total (`parts`)
- Free (MIT): install with `npx chartreuse add stacked-bars`, or registry item `/r/stacked-bars.json`
- Example: **The US and China tied on gold at Paris 2024, but the US won 35 more medals** (Paris 2024 Olympic medals, top 12 nations by golds, count of medals)
- How to read it: Each bar is one nation's medals stacked gold, silver, bronze; switch to % to compare the mix rather than the haul.
- Interactive: filter
- Phone: Adapts to mobile (Draws its own phone layout below 520px) · Video formats: 16:9 (best 16:9)

## Use it when
- a few items (5 to 15) each split into the same 2 to 5 parts
- both the totals and the mix matter
- readers can turn parts off to compare just one

## Avoid it when
- more than five parts (the middle segments are hard to compare)
- only the mix matters (use a 100% bar or a marimekko)
- the parts don't add to a meaningful total
- values below zero (bars stack from zero)

## Fields (data.json)
- `code` (id): a short unique code for the row (IOC country code)
- `country` (category): the bar's name (the nation)
- `gold` (number, unit "medals", format count): one stacked part per number key, in the data's order; 2 to 5 parts (more fold into Other), any names. Here: gold medals
- `silver` (number, unit "medals", format count): silver medals
- `bronze` (number, unit "medals", format count): bronze medals

Units are the example's: with your own data, set each number field's `unit` and `format` (percent, count, decimal, currency, compact) in the meta you pass to mount, or `chartreuse render --unit field=text --format field=count`. Scales and labels follow your data.

## Interactions, motion, sound
- hover or tap a segment to read the medal count and its share of the nation's haul
- a button per part (Gold, Silver, Bronze) switches it on or off and restacks the bars
- the % button rescales every bar to 100%
- Motion: Gold segments grow first, then silver, then bronze; toggles and the % switch restack the bars smoothly.
- Sound: A chime on toggles; a whoosh on the % switch; a pop on hover. Quiet while it draws and between steps; mute it from its card. (sound is off until the viewer turns it on)

## Example spec
```json
{
  "chart": "stacked-bars",
  "title": "The US and China tied on gold at Paris 2024, but the US won 35 more medals",
  "subtitle": "Paris 2024 Olympic medals, top 12 nations by golds, count of medals",
  "data": "/charts/stacked-bars.data.json"
}
```

## Source
2024 Summer Olympics medal table, Wikipedia (CC BY-SA 4.0), retrieved 2026-12-01: https://en.wikipedia.org/wiki/2024_Summer_Olympics_medal_table

Note: Ordered as the official table: by golds, then silvers. Twelve of the 92 teams (NOCs) that won at least one medal.

Live: /charts/stacked-bars · Manifest: /charts/stacked-bars.json · Tags: stacked bar, part to whole, olympics, sport, medals, legend toggle, percent
