Gallery / Distributions /
Histogram
How values of one variable are spread.
68.1 KB of code · core 29.5 + 9 blocks 38.6
Distribution of heights
Half of the 80 people are between 166 and 175 cm tall; the shortest is 141.5 cm, the tallest 190.6 cm.
How it is made: transform.bin counts the values in round-edged bins; the rect draws each bin from its start (x) to its end (x2), as tall as its count.
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: 9 of the 59, 68.1 KB of code, minified, which is 32% of the 215.9 KB that the core and every block weigh together. The report file is 74.4 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.linear Numbers to positions, with round ticks. | Numbers to positions, with round ticks. | 1.6 | |
transform.bin Counts values in equal bins: histograms. | Counts values in equal bins: histograms. | 3.3 | |
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 | |
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 9 blocks 38.6 | 68.1 |
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/histogram.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
histogram.json
{
"title": "Histogram",
"data": {
"people": {
"columns": { "height": [159.5, 166.3, 163.2, 167.9, 165.5, 164.5, 178.2, 180, 171.4, 177.7, 181, 180.1, 158, 159.6, 185, 172.8, 174.3, 173.3, 170, 176, 155.5, 174.2, 167.5, 164.4, 172.6, 166.5, 172.2, 170, 169.1, 166.5, 175.7, 175, 174.8, 176.4, 167.8, 173.3, 175.3, 168.7, 171.2, 184.1, 168.8, 174.7, 165.6, 162.2, 172.3, 168.7, 160.7, 164.4, 182.4, 175.4, 175.4, 171.9, 173.1, 164.1, 177.1, 166.1, 165.7, 163.2, 166, 185.5, 141.5, 172.8, 185.4, 165.5, 181.2, 168.1, 175.9, 171.4, 164, 179.1, 171.4, 163.9, 172.4, 163, 171.5, 167.6, 190.6, 180.1, 157.1, 171.6] }
}
},
"charts": [
{
"title": "Distribution of heights",
"note": "Half of the 80 people are between 166 and 175 cm tall; the shortest is 141.5 cm, the tallest 190.6 cm.",
"how": "transform.bin counts the values in round-edged bins; the rect draws each bin from its start (x) to its end (x2), as tall as its count.",
"spec": {
"data": "people",
"size": [
640,
300
],
"transforms": [
{
"type": "bin",
"field": "height",
"bins": 10
}
],
"scales": {
"x": {
"type": "linear",
"nice": false
},
"y": {
"type": "linear",
"zero": true
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x"
},
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "rect",
"x": "bin0",
"x2": "bin1",
"y": "count"
}
],
"interaction": [
{ "type": "tooltip", "labels": { "count": "People" } }
]
}
}
]
}
The report the composer makes from it is one standalone file (74.4 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.linear, transform.bin, mark.rect, 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":{"columns":{"height":[159.5,166.3,163.2,167.9,165.5,164.5,178.2,180,171.4,177.7,181,180.1,158,159.6,185,172.8,174.3,173.3,170,176,155.5,174.2,167.5,164.4,172.6,166.5,172.2,170,169.1,166.5,175.7,175,174.8,176.4,167.8,173.3,175.3,168.7,171.2,184.1,168.8,174.7,165.6,162.2,172.3,168.7,160.7,164.4,182.4,175.4,175.4,171.9,173.1,164.1,177.1,166.1,165.7,163.2,166,185.5,141.5,172.8,185.4,165.5,181.2,168.1,175.9,171.4,164,179.1,171.4,163.9,172.4,163,171.5,167.6,190.6,180.1,157.1,171.6]}}}
</script>
<script type="application/json" data-bc-chart>
{
"data": "people",
"size": [
640,
300
],
"transforms": [
{
"type": "bin",
"field": "height",
"bins": 10
}
],
"scales": {
"x": {
"type": "linear",
"nice": false
},
"y": {
"type": "linear",
"zero": true
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x"
},
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "rect",
"x": "bin0",
"x2": "bin1",
"y": "count"
}
],
"interaction": [
{
"type": "tooltip",
"labels": {
"count": "People"
}
}
],
"alt": "Distribution of heights"
}
</script>