Gallery / Comparing categories /
Population pyramid
Two groups back to back per category (a population pyramid by age and sex).
71.2 KB of code · core 29.5 + 11 blocks 41.7
Population by age, millions: men (left) and women (right)
People in their thirties are the largest group; from 50 on women outnumber men, by nearly two to one over 80.
How it is made: Two horizontal bars per band: calculate makes the men negative so they grow to the left of 0. The axis writes both sides without a sign (signDisplay "never"); the tooltip shows the original fields. Bands read from the top in data order, so the oldest come first. Give x a symmetric domain so 0 sits in the middle.
What the report is made of
The composer reads the JSON below and puts into the page the core and only the blocks its chart uses: 11 of the 59, 71.2 KB of code, minified, which is 33% of the 215.9 KB that the core and every block weigh together. The report file is 77.8 KB: that code, the data, the styles and the page around them.
| Block | What it does | Share of the code | KB |
|---|---|---|---|
core.composed-plain A report with plain data: no checks, no decoder. Most reports. | A report with plain data: no checks, no decoder. Most reports. | 29.5 | |
lib.pointer Where the pointer is on the chart, and following a mouse or a finger once a frame. Shared by tooltip, crosshair, zoom and brush. | Where the pointer is on the chart, and following a mouse or a finger once a frame. Shared by tooltip, crosshair, zoom and brush. | 1.7 | |
lib.ticks Round steps and ticks (1, 2 or 5 × 10ⁿ). Shared by the number scales and the histogram bins. | Round steps and ticks (1, 2 or 5 × 10ⁿ). Shared by the number scales and the histogram bins. | 0.7 | |
scale.band One band per category: the axis of bar charts. | One band per category: the axis of bar charts. | 1.2 | |
scale.linear Numbers to positions, with round ticks. | Numbers to positions, with round ticks. | 1.6 | |
transform.calculate A new column from two numbers: a margin, a gap to plan. | A new column from two numbers: a margin, a gap to plan. | 1.4 | |
mark.rect Rectangles: bars, grouped bars, ranges, heatmap cells; the hexagons of a hexbin. | Rectangles: bars, grouped bars, ranges, heatmap cells; the hexagons of a hexbin. | 4.5 | |
mark.rule Target and threshold lines; the target tick of a bullet chart. | Target and threshold lines; the target tick of a bullet chart. | 3.8 | |
guide.axis Axes: formatted labels, a title, long categories wrapped. | Axes: formatted labels, a title, long categories wrapped. | 11.7 | |
guide.grid Grid lines at the ticks. | Grid lines at the ticks. | 0.6 | |
interaction.tooltip The values under the pointer. | The values under the pointer. | 10.7 | |
renderer.svg Draws into SVG: sharp, selectable, prints well. The default. | Draws into SVG: sharp, selectable, prints well. The default. | 3.9 | |
| Total | The core 29.5 and 11 blocks 41.7 | 71.2 |
The JSON
This is the whole input. data holds the datasets, each chart is a spec that names one.
In a checkout of the repository: node scripts/compose.js skills/blockcharts/recipes/pyramid.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
pyramid.json
{
"title": "Population pyramid",
"data": {
"people": [
{ "age": "80+", "men": 1.2, "women": 2.1 },
{ "age": "70-79", "men": 3.1, "women": 4.0 },
{ "age": "60-69", "men": 4.9, "women": 5.6 },
{ "age": "50-59", "men": 6.2, "women": 6.5 },
{ "age": "40-49", "men": 6.8, "women": 6.7 },
{ "age": "30-39", "men": 7.4, "women": 7.1 },
{ "age": "20-29", "men": 6.9, "women": 6.5 },
{ "age": "10-19", "men": 6.1, "women": 5.8 },
{ "age": "0-9", "men": 5.4, "women": 5.1 }
]
},
"charts": [
{
"title": "Population by age, millions: men (left) and women (right)",
"note": "People in their thirties are the largest group; from 50 on women outnumber men, by nearly two to one over 80.",
"how": "Two horizontal bars per band: calculate makes the men negative so they grow to the left of 0. The axis writes both sides without a sign (signDisplay \"never\"); the tooltip shows the original fields. Bands read from the top in data order, so the oldest come first. Give x a symmetric domain so 0 sits in the middle.",
"spec": {
"data": "people",
"size": [640, 340],
"transforms": [{ "type": "calculate", "a": "men", "op": "*", "value": -1, "as": "menLeft" }],
"scales": {
"y": { "type": "band", "paddingInner": 0.15 },
"x": { "type": "linear", "domain": [-8, 8] }
},
"guides": [
{ "type": "grid", "scale": "x" },
{ "type": "axis", "scale": "y" },
{ "type": "axis", "scale": "x", "format": { "signDisplay": "never" } }
],
"marks": [
{ "type": "rect", "x": "menLeft", "y": "age", "fill": "var(--bc-c0, #3a6fe4)" },
{ "type": "rect", "x": "women", "y": "age", "fill": "var(--bc-c1, #14a39a)" },
{ "type": "rule", "x": { "value": 0 }, "stroke": "var(--bc-axis, #cbd2dc)", "strokeWidth": 1 }
],
"interaction": [{ "type": "tooltip", "fields": ["men", "women"] }]
}
}
]
}
The report the composer makes from it is one standalone file (77.8 KB): open it from disk, mail it, print it.
In a page of your own
The charts above use the composition kit (54.9 KB gzipped), the smallest one that has the blocks this example needs:
lib.pointer, lib.ticks, scale.band, scale.linear, transform.calculate, mark.rect, mark.rule, guide.axis, guide.grid, interaction.tooltip, renderer.svg.
The HTML below loads it from the jsDelivr CDN, pinned to version 0.0.5; to serve it from your own site, put its file there
(the files of a release) and change the address.
HTML
<script src="https://cdn.jsdelivr.net/npm/blockcharts@0.0.5/release/kits/composition.js"></script>
<script type="application/json" id="bc-data">
{"people":[{"age":"80+","men":1.2,"women":2.1},{"age":"70-79","men":3.1,"women":4},{"age":"60-69","men":4.9,"women":5.6},{"age":"50-59","men":6.2,"women":6.5},{"age":"40-49","men":6.8,"women":6.7},{"age":"30-39","men":7.4,"women":7.1},{"age":"20-29","men":6.9,"women":6.5},{"age":"10-19","men":6.1,"women":5.8},{"age":"0-9","men":5.4,"women":5.1}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "people",
"size": [
640,
340
],
"transforms": [
{
"type": "calculate",
"a": "men",
"op": "*",
"value": -1,
"as": "menLeft"
}
],
"scales": {
"y": {
"type": "band",
"paddingInner": 0.15
},
"x": {
"type": "linear",
"domain": [
-8,
8
]
}
},
"guides": [
{
"type": "grid",
"scale": "x"
},
{
"type": "axis",
"scale": "y"
},
{
"type": "axis",
"scale": "x",
"format": {
"signDisplay": "never"
}
}
],
"marks": [
{
"type": "rect",
"x": "menLeft",
"y": "age",
"fill": "var(--bc-c0, #3a6fe4)"
},
{
"type": "rect",
"x": "women",
"y": "age",
"fill": "var(--bc-c1, #14a39a)"
},
{
"type": "rule",
"x": {
"value": 0
},
"stroke": "var(--bc-axis, #cbd2dc)",
"strokeWidth": 1
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"men",
"women"
]
}
],
"alt": "Population by age, millions: men (left) and women (right)"
}
</script>