Gallery / Comparing categories /
Bar chart
Compare categories (with the value written on each bar).
71.3 KB of code · core 29.5 + 10 blocks 41.8
Sales by region
West sells the most, 66, more than twice the 25 of Central.
How it is made: The text mark writes the value above each bar: it reads its x and y like the bars and takes the label from the text field.
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: 10 of the 59, 71.3 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.2 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 | |
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.text Value labels that shrink, or step aside, when they do not fit. | Value labels that shrink, or step aside, when they do not fit. | 5.3 | |
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 10 blocks 41.8 | 71.3 |
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/bar.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
bar.json
{
"title": "Bar chart",
"data": {
"sales": [
{ "region": "North", "total": 42 },
{ "region": "South", "total": 58 },
{ "region": "East", "total": 31 },
{ "region": "West", "total": 66 },
{ "region": "Central", "total": 25 }
]
},
"charts": [
{
"title": "Sales by region",
"note": "West sells the most, 66, more than twice the 25 of Central.",
"how": "The text mark writes the value above each bar: it reads its x and y like the bars and takes the label from the text field.",
"spec": {
"data": "sales",
"size": [640, 300],
"scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x" },
{ "type": "axis", "scale": "y" }
],
"marks": [
{ "type": "rect", "x": "region", "y": "total", "radius": 2 },
{ "type": "text", "x": "region", "y": "total", "text": "total", "baseline": "auto", "dy": -5 }
],
"interaction": [{ "type": "tooltip" }]
}
}
]
}
The report the composer makes from it is one standalone file (77.2 KB): open it from disk, mail it, print it.
In a page of your own
The charts above use the basic kit (42.9 KB gzipped), the smallest one that has the blocks this example needs:
lib.pointer, lib.ticks, scale.band, scale.linear, mark.rect, mark.text, 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/basic.js"></script>
<script type="application/json" id="bc-data">
{"sales":[{"region":"North","total":42},{"region":"South","total":58},{"region":"East","total":31},{"region":"West","total":66},{"region":"Central","total":25}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "sales",
"size": [
640,
300
],
"scales": {
"x": {
"type": "band"
},
"y": {
"type": "linear",
"zero": true
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x"
},
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "rect",
"x": "region",
"y": "total",
"radius": 2
},
{
"type": "text",
"x": "region",
"y": "total",
"text": "total",
"baseline": "auto",
"dy": -5
}
],
"interaction": [
{
"type": "tooltip"
}
],
"alt": "Sales by region"
}
</script>