Skip to content
OpenChart

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.

6M4M2M002M4M6M2.4M2.3M2.7M2.5M2.9M2.7M3M2.9M3.1M3M3.1M3M3.3M3.2M3.7M3.6M4.3M4.2M5.1M5M4.4M4.3M4M3.9M3.8M3.7M4.1M4.2M4.5M5M3.2M3.9M2.3M3.2M1.3M2.4M490k1.2M120k470k15k97kJapan's largest age group in 2020 was 45-49Population by five-year age group and sex, 20206M4M2M002M4M6M2.4M2.3M2.7M2.5M2.9M2.7M3M2.9M3.1M3M3.1M3M3.3M3.2M3.7M3.6M4.3M4.2M5.1M5M4.4M4.3M4M3.9M3.8M3.7M4.1M4.2M4.5M5M3.2M3.9M2.3M3.2M1.3M2.4M490k1.2M120k470k15k97kJapan's largest age group in 2020 was 45-49Population by five-year age group and sex, 2020
Japan's population by age and sex, 2020. Source: UN World Population Prospects 2024 (medium variant).

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.

6M4M2M002M4M6M2.4M2.3M2.7M2.5M2.9M2.7M3M2.9M3.1M3M3.1M3M3.3M3.2M3.7M3.6M4.3M4.2M5.1M5M4.4M4.3M4M3.9M3.8M3.7M4.1M4.2M4.5M5M3.2M3.9M2.3M3.2M1.3M2.4M490k1.2M120k470k15k97kJapan's largest age group in 2020 was 45-49Population by five-year age group and sex, 20206M4M2M002M4M6M2.4M2.3M2.7M2.5M2.9M2.7M3M2.9M3.1M3M3.1M3M3.3M3.2M3.7M3.6M4.3M4.2M5.1M5M4.4M4.3M4M3.9M3.8M3.7M4.1M4.2M4.5M5M3.2M3.9M2.3M3.2M1.3M2.4M490k1.2M120k470k15k97kJapan's largest age group in 2020 was 45-49Population by five-year age group and sex, 2020
Live example. Source: UN World Population Prospects 2024 (medium variant).

Made with OpenChart. How to build this bar chart

{
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    }
  },
  "chrome": {
    "title": "Japan's largest age group in 2020 was 45-49",
    "subtitle": "Population by five-year age group and sex, 2020"
  },
  "data": [
    {
      "year": 2020,
      "age": "0-4",
      "sex": "Male",
      "pop": 2375000
    },
    {
      "year": 2020,
      "age": "0-4",
      "sex": "Female",
      "pop": 2254000
    },
    {
      "year": 2020,
      "age": "5-9",
      "sex": "Male",
      "pop": 2697000
    },
    {
      "year": 2020,
      "age": "5-9",
      "sex": "Female",
      "pop": 2548000
    },
    {
      "year": 2020,
      "age": "10-14",
      "sex": "Male",
      "pop": 2861000
    },
    {
      "year": 2020,
      "age": "10-14",
      "sex": "Female",
      "pop": 2700000
    },
    // … 36 more rows; Copy includes them
  ]
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    }
  },
  "chrome": {
    "title": "Japan's largest age group in 2020 was 45-49",
    "subtitle": "Population by five-year age group and sex, 2020"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    }
  },
  "chrome": {
    "title": "Japan's largest age group in 2020 was 45-49",
    "subtitle": "Population by five-year age group and sex, 2020"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    }
  },
  "chrome": {
    "title": "Japan's largest age group in 2020 was 45-49",
    "subtitle": "Population by five-year age group and sex, 2020"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    }
  },
  "chrome": {
    "title": "Japan's largest age group in 2020 was 45-49",
    "subtitle": "Population by five-year age group and sex, 2020"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Three encoding choices make it read as a pyramid:

  • y.sort lists 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 a sort, 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 },
};
8M6M4M2M002M4M6M8M1.4M1.5M630k1.6M1.9M2M1.9M1.9M1.9M2M1.4M2.1M2.2M2.2M2.2M2.1M2.2M2.5M2.6M2.3M1.5M1.4M1.6M180k1.7M1.9M2M1.9M1.9M1.9M2M1.5M2.1M2.2M2.2M2.1M2M2M2.2M2.1M1.5M710kJapan's largest age group moves from 0-4 in 1950 to 85-89 in 2100Population by age and sex, 1950-2100; 2025 onward is the UN's projection8M6M4M2M002M4M6M8M1.4M1.5M630k1.6M1.9M2M1.9M1.9M1.9M2M1.4M2.1M2.2M2.2M2.2M2.1M2.2M2.5M2.6M2.3M1.5M1.4M1.6M180k1.7M1.9M2M1.9M1.9M1.9M2M1.5M2.1M2.2M2.2M2.1M2M2M2.2M2.1M1.5M710kJapan's largest age group moves from 0-4 in 1950 to 85-89 in 2100Population by age and sex, 1950-2100; 2025 onward is the UN's projection
Japan's population by age and sex, 1950-2100, playing. Source: UN World Population Prospects 2024 (medium variant).

Made with OpenChart. How to build this bar chart

{
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "data": [
    {
      "year": 1950,
      "age": "0-4",
      "sex": "Male",
      "pop": 6002000
    },
    {
      "year": 1950,
      "age": "0-4",
      "sex": "Female",
      "pop": 5944000
    },
    {
      "year": 1955,
      "age": "0-4",
      "sex": "Male",
      "pop": 4852000
    },
    {
      "year": 1955,
      "age": "0-4",
      "sex": "Female",
      "pop": 4687000
    },
    {
      "year": 1960,
      "age": "0-4",
      "sex": "Male",
      "pop": 4085000
    },
    {
      "year": 1960,
      "age": "0-4",
      "sex": "Female",
      "pop": 3949000
    },
    // … 1,296 more rows; Copy includes them
  ],
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 15000
  },
  "chrome": {
    "title": "Japan's largest age group moves from 0-4 in 1950 to 85-89 in 2100",
    "subtitle": "Population by age and sex, 1950-2100; 2025 onward is the UN's projection"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,302 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 15000
  },
  "chrome": {
    "title": "Japan's largest age group moves from 0-4 in 1950 to 85-89 in 2100",
    "subtitle": "Population by age and sex, 1950-2100; 2025 onward is the UN's projection"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,302 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 15000
  },
  "chrome": {
    "title": "Japan's largest age group moves from 0-4 in 1950 to 85-89 in 2100",
    "subtitle": "Population by age and sex, 1950-2100; 2025 onward is the UN's projection"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,302 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 15000
  },
  "chrome": {
    "title": "Japan's largest age group moves from 0-4 in 1950 to 85-89 in 2100",
    "subtitle": "Population by age and sex, 1950-2100; 2025 onward is the UN's projection"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,302 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "mirror": true
  },
  "encoding": {
    "y": {
      "field": "age",
      "type": "ordinal",
      "sort": [
        "100+",
        "95-99",
        "90-94",
        "85-89",
        "80-84",
        "75-79",
        "70-74",
        "65-69",
        "60-64",
        "55-59",
        "50-54",
        "45-49",
        "40-44",
        "35-39",
        "30-34",
        "25-29",
        "20-24",
        "15-19",
        "10-14",
        "5-9",
        "0-4"
      ]
    },
    "x": {
      "field": "pop",
      "type": "quantitative",
      "format": ".2~s"
    },
    "color": {
      "field": "sex",
      "type": "nominal",
      "sort": [
        "Male",
        "Female"
      ]
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 15000
  },
  "chrome": {
    "title": "Japan's largest age group moves from 0-4 in 1950 to 85-89 in 2100",
    "subtitle": "Population by age and sex, 1950-2100; 2025 onward is the UN's projection"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

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.