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.jsonPopulation by age
PopulationPyramidPopulationPyramidTabledefaultPopulationPyramidLabels- +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
import { ChartCard } from "@/components/sheenui/charts/chart-parts/chart-card";
import { PopulationPyramid } from "@/components/sheenui/charts/population-pyramid";
import { populationPyramidLegend, type PyramidGroup } from "@/components/sheenui/charts/population-pyramid/model";
import { PopulationPyramidTable } from "@/components/sheenui/charts/population-pyramid/table";
// Package build (after the first npm release, or a packed tarball): import { ChartCard, PopulationPyramid, PopulationPyramidTable, populationPyramidLegend, type PyramidGroup } from "@sheenui/theme";
const ages: PyramidGroup[] = [
{ label: "0–19", start: 13.3, end: 12.5 },
{ label: "20–39", start: 16.8, end: 16.3 },
{ label: "40–59", start: 17.4, end: 18.1 },
{ label: "60–79", start: 12.2, end: 14.4 },
{ label: "80+", start: 2.0, end: 3.7 },
];
export function Example() {
return (
<ChartCard
title="Population by age"
legend={populationPyramidLegend("Male", "Female")}
table={<PopulationPyramidTable data={ages} caption="Population by age group, in millions" />}
>
<PopulationPyramid data={ages} patterns aria-label="Population by age group and sex, in millions" />
</ChartCard>
);
}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
import { ChartCard } from "@/components/sheenui/charts/chart-parts/chart-card";
import { PopulationPyramid } from "@/components/sheenui/charts/population-pyramid";
import { populationPyramidLegend, type PyramidGroup } from "@/components/sheenui/charts/population-pyramid/model";
import { PopulationPyramidTable } from "@/components/sheenui/charts/population-pyramid/table";
// Package build (after the first npm release, or a packed tarball): import { ChartCard, PopulationPyramid, PopulationPyramidTable, populationPyramidLegend, type PyramidGroup } from "@sheenui/theme";
const tenure: PyramidGroup[] = [
{ label: "< 1 year", start: 5200, end: 3900 },
{ label: "1–2 years", start: 4100, end: 3800 },
{ label: "2–3 years", start: 3300, end: 3100 },
{ label: "3–5 years", start: 2400, end: 2600 },
];
const labels = { start: "Pro", end: "Team", group: "Tenure" };
export function Example() {
return (
<ChartCard
title="Customers by tenure"
legend={populationPyramidLegend("Pro", "Team", [3, 4])}
table={<PopulationPyramidTable data={tenure} labels={labels} caption="Customers by tenure and plan" />}
>
<PopulationPyramid data={tenure} firstAt="top" colors={[3, 4]} labels={labels} aria-label="Customers by tenure and plan" />
</ChartCard>
);
}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.tsxcomponents/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.tslib/{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
PopulationPyramidLabels keys and English defaults (8)
PopulationPyramidTable props
Keyboard
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
Related pages
48 charts pages in all: browse them on Charts.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .