How to make a population pyramid in JavaScript
A population pyramid draws a country's ages as back-to-back bars. This post builds Japan's from UN data, then animates it from 1950 to 2100.
Made with OpenChart. How to build this bar chart
Japan had 126 million people in 2020, and more of them were 45 to 49 years old than any other five-year age group, according to the UN’s World Population Prospects. A population pyramid draws that structure as horizontal bars for each age group, men extending left and women right from a shared center, youngest at the bottom. In OpenChart a pyramid is a horizontal bar chart with one mark option, mirror: true, and adding encoding.time makes it play through the years.
The data: one row per age group and sex
A pyramid reads long data, one row for each age group and sex:
const rows = [
{ year: 2020, age: "0-4", sex: "Male", pop: 2375000 },
{ year: 2020, age: "0-4", sex: "Female", pop: 2254000 },
{ year: 2020, age: "45-49", sex: "Male", pop: 5054000 },
{ year: 2020, age: "45-49", sex: "Female", pop: 4953000 },
// ...21 age groups times 2 sexes: 42 rows for one year
];The UN publishes the figures in thousands of people, and these rows multiply them by 1,000 so the axis reads “6M” for six million people; left in thousands, it would read “6k”. Both sexes are positive numbers; the chart decides which way each one points.
A static pyramid with mirror
mark: { type: "bar", mirror: true } turns a horizontal bar chart into a pyramid. The first value of the color field extends left from the center and the second extends right, both measured on one scale, with the age labels in a gutter between them. The two values label the halves above the plot in place of a legend.
Made with OpenChart. How to build this bar chart
Three encoding choices make it read as a pyramid:
y.sortlists the age groups oldest first, which puts 100+ at the top of the axis and 0-4 at the bottom.color.sort: ["Male", "Female"]puts men on the left, the convention in demography. Without asort, the first value in the data goes left.x.format: ".2~s"prints the axis in millions, and both sides read as positive numbers going out from the center.
The 2020 pyramid has two bulges. People aged 70 to 74 were born from 1946 to 1950, Japan’s postwar baby boom, and the 45-49 group are largely their children. The base is narrow, since under-15s were 12% of the population in 2020, against 35% in 1950. At the top, the women’s bar for 100 and older is more than six times the men’s, 97,000 women to 15,000 men.
Why mirror and not negative values
The usual workaround, used in the Plotly and Highcharts pyramid examples, stores men’s counts as negative numbers and formats the axis to hide the minus sign. Tooltips, CSV exports and the data table a screen reader gets still list -5,054,000 men. The age labels also stay at the left edge of the plot, where they collide with the men’s bars, instead of sitting between the halves.
Vega-Lite’s pyramid example builds three views side by side with hconcat, one of them a reversed x scale, and keeps their scales matched by hand. OpenChart has no hconcat; with mirror, both halves share one scale, so equal counts draw equal bar lengths on both sides.
On a mirrored chart the color field must have exactly two values, the bars must be horizontal (quantitative x, categories on y), and encoding.x.stack can’t be set, since mirrored bars never stack. A color field with three values fails with a message that names the field and its values.
Animating the pyramid with encoding.time
Add a time channel and pass every year’s rows, and the pyramid plays. This one runs every fifth year from 1950 to 2100 in 15 seconds:
const spec = {
mark: { type: "bar", mirror: true },
data: rows, // 31 years x 21 age groups x 2 sexes: 1,302 rows
encoding: {
y: { field: "age", type: "ordinal", sort: [...ageGroups].reverse() }, // ageGroups lists youngest to oldest
x: { field: "pop", type: "quantitative", format: ".2~s" },
color: { field: "sex", type: "nominal", sort: ["Male", "Female"] },
time: { field: "year", type: "temporal" },
},
playback: { duration: 15000 },
};Made with OpenChart. How to build this bar chart
In 1950 the largest group is the youngest, 0-4. The bulge climbs the pyramid as the years play, and by 2100 the largest group is 85-89 in a population of 77 million, down from 126 million in 2020. Figures from 2025 onward are the UN’s medium-variant projection, and the subtitle says so, since a reader watching the bars move has no other cue that the estimates have turned into projections.
The bars need no key. Each bar is identified by its age group and sex, so “Female, 45-49” in 2020 grows or shrinks into “Female, 45-49” in 2025, with lengths interpolated between the two years. The value axis is fixed for the whole run, sized to the largest bar in any year, so a bar’s length means the same number of people in every frame. The year appears as a large faint label in the upper right.
Keep the y sort a fixed list. A pyramid sorted by value would rank its rows once over all years and never re-rank them, and OpenChart warns about that sort and suggests type: "race" for bars that should re-rank each period.
Controls, playback and static output
createChart returns a chart whose playback property has play(), pause(), seek(t), setSpeed(speed), getState() and on("period", callback); it’s null on a chart without a time channel. seek takes a zero-based period index, so chart.playback?.seek(14) shows 2020. The control strip under the chart plays, pauses and scrubs, and with focus on it the arrow keys step five years at a time. The playback block takes the same options as the bar chart race’s race block: duration or step, holdEnd, loop, autoplay, controls and period.
Readers who have asked their system for reduced motion get the pyramid paused on its last frame, and play steps through the years without a tween. A screen reader hears the year when the reader pauses or steps, not during playback.
For a static image, renderStaticSVG(spec, { t: 14 }) from @opendata-ai/openchart-vanilla/static draws 2020 as an SVG string without a browser. Without t it draws the last year, here the 2100 projection, so pass t when the final frame isn’t the one you want in a preview.
The population pyramid docs page covers the gutter, value labels and every mirror rule.