Skip to content

Population Pyramid for React

A React population pyramid: two sets of horizontal bars mirrored around a centre line, one row per age group. Pass data as readonly PyramidGroup[]: one item per group, { label, start, end }.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/population-pyramid.json

Population by age

Use the up and down arrow keys to read each group, Home and End for the first and last.

  • PopulationPyramid
  • PopulationPyramidTable
  • defaultPopulationPyramidLabels
  • +1 more

Counts or shares

A fictional country by age and sex, in millions. Both halves share one scale, so they compare honestly. Show each bar as a share of the whole population, and hatch one side so the two differ without colour.

Population by age

Millions of people

  • Male
  • Female

Beyond demographics

Any ordered bands with two sides work: here customers by tenure on two plans, with the labels renamed through the legend and labels prop. firstAt="top" puts the first band at the top.

Customers by tenure

Accounts per plan

  • Pro
  • Team

Files to copy

13 folders · 30 files · 4 packages
From theme/src/
  • components/charts/{population-pyramid.tsx, bar-flip.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.tsx, chart-fit.tsx, chart-icons.tsx, chart-parts.tsx, chart-patterns.tsx, chart-pointer.tsx, chart-tip.tsx, chart-utils.ts, heatmap.tsx}
  • components/charts/chart-parts/
  • components/charts/heatmap/
  • components/charts/population-pyramid/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/menu/
  • components/skeleton/
  • components/status-icons/
  • hooks/{use-controllable.ts, use-direction.ts, use-intersection.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, csv.ts, form.ts, glide-highlight.tsx, heading.ts, math.ts, popup-a11y.ts, popup-motion.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/population-pyramid.jsonRegistry item: population-pyramid. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".

Props

PropTypeDefaultDescription
data (required)readonly PyramidGroup[]NoneOne item per age group: { label, start, end }, from the first group to the last, e.g. 0–4 up to 85+.
sharebooleanfalseShow each bar as a share of the whole population (%) instead of a count.
firstAt"top" | "bottom""bottom"Which end of data sits at the bottom. "bottom" puts the first group at the bottom, so the youngest are at the base.
colorsreadonly [ChartColor, ChartColor][1, 2]Palette slots (1–8) of the start and the end side.
patternsbooleanfalseTell the two sides apart without colour (hatch fills on the end side). Off by default: only two sides, and they sit on opposite sides of the centre line.
showValuesbooleantruePrint each bar's value past its end where it fits.
format((n: number) => string)formatCompact in localeFormats a count for the labels, tooltips and data table: takes the number, returns the text shown.
formatPercent((ratio: number) => string)percentages in localeFormats a share (0.082 → "8.2%").
localestring"en"Locale of the default number formats.
emptyStateReactNodeNoneShown when there is nothing to plot.
labelsPartial<PopulationPyramidLabels>defaultPopulationPyramidLabelsEvery other string, for translation: labels={{ roleDescription: "…" }}; omitted keys keep the English of defaultPopulationPyramidLabels. All 8 keys of PopulationPyramidLabels are listed in the key table under the props table.
aria-label (required)stringNoneNames the chart for assistive tech (required).
classNamestringNoneAdds extra classes to the outer element.
PopulationPyramidLabels keys and English defaults (8)
Pass any key in labels; the rest keep the defaults of defaultPopulationPyramidLabels.
KeyEnglish default
roleDescriptionpopulation pyramid
instructionsUse the up and down arrow keys to read each group, Home and End for the first and last.
startMale
endFemale
totalTotal
groupAge group
shareOfTotalShare of total
noDataNo data

PopulationPyramidTable props

PropTypeDefaultDescription
data (required)readonly PyramidGroup[]NoneOne item per age group: { label, start, end }, from the first group to the last, e.g. 0–4 up to 85+.
format((n: number) => string)formatCompact in localeFormats a count for the labels, tooltips and data table: takes the number, returns the text shown.
labelsPartial<PopulationPyramidLabels>defaultPopulationPyramidLabelsEvery other string, for translation: labels={{ roleDescription: "…" }}; omitted keys keep the English of defaultPopulationPyramidLabels. All 8 keys of PopulationPyramidLabels are listed in the key table under the props table.
localestring"en"Locale of the default number formats.
formatPercent((ratio: number) => string)percentages in localeFormats a share (0.082 → "8.2%").
sharebooleanfalseShow each bar as a share of the whole population (%) instead of a count.
captionstringNoneHidden caption that names the table for screen readers.
classNamestringNoneAdds extra classes to the outer element.

Keyboard

KeysAction
TabFocus the chart. It is one tab stop; the arrow keys then read it.
↑/↓Move between groups, top to bottom.
Home/EndJump to the top / bottom group.
EscapeClear the reading.

Accessibility

  • The chart is a focusable figure named by its aria-label (the chart title), with the keyboard hint as its description and, inside a ChartCard with a table view, aria-details pointing at that table. Moving through it announces the group, both sides, the total and its share of the population.
  • The two sides sit on opposite sides of the centre line and patterns hatches one of them, so colour never separates them; every bar's value is printed where it fits and always in the tooltip and table.
  • In a ChartCard the legend toggles a side on and off; the scale follows the sides that remain.
  • Reduced motion: the bars appear at once and data changes jump instead of gliding. In right-to-left text the sides swap.

All exports on this page

  • PopulationPyramid — A population pyramid: two sets of horizontal bars mirrored around a centre line, one row per age group.
  • PopulationPyramidTable — Every group with the size of each side and the total, and (with share) its share of the population.
  • defaultPopulationPyramidLabels — The built-in English strings of PopulationPyramid; spread it into your own labels and change what you need.

When to use the population pyramid

  • Use it for demographics: a country, city or customer base by age group and sex, where the two halves sit back to back on one scale so their shapes compare honestly.
  • Use it for any two-sided split across ordered bands, such as customers by tenure on two plans, survey answers by age, or applicants against hires by seniority.
  • Set share to read each bar as a part of the whole population instead of a count, and firstAt to choose whether the first group sits at the base or at the top.
  • Skip it for more than two sides or for unordered categories, where a grouped Bar chart is clearer, and for a single series, where a Bar chart needs no mirror.
  • Pair it with ChartCard, populationPyramidLegend and PopulationPyramidTable for a legend that toggles a side, Chart / Table tabs and a text table.

Frequently asked questions

48 charts pages in all: browse them on Charts.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .