Gallery / Dashboards and reports /
Small multiples
The same chart for each region, product or country, to compare on one scale.
81.2 KB of code · core 29.5 + 14 blocks 51.7
Units by product, one panel per region
Coffee sells best in North and West, tea in South; in East coffee overtook tea in April.
North
South
East
West
How it is made: facet draws the chart once for each value of a field, on the same axes, so the panels can be compared at a glance. The legend is drawn once, above them.
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: 14 of the 59, 81.2 KB of code, minified, which is 38% of the 215.9 KB that the core and every block weigh together. The report file is 91.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.curve The path of a line: straight segments, a smooth curve that never overshoots, or steps. Shared by line and area. | The path of a line: straight segments, a smooth curve that never overshoots, or steps. Shared by line and area. | 1.2 | |
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.color A color per category, from the theme palette. | A color per category, from the theme palette. | 0.8 | |
scale.linear Numbers to positions, with round ticks. | Numbers to positions, with round ticks. | 1.6 | |
scale.time Dates to positions, ticks in days, months or years (UTC). | Dates to positions, ticks in days, months or years (UTC). | 4.1 | |
transform.filter Keeps the rows that pass a test: numbers, dates, text, lists. | Keeps the rows that pass a test: numbers, dates, text, lists. | 2.3 | |
mark.line A line per series, with optional end labels. | A line per series, with optional end labels. | 4.4 | |
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 | |
guide.legend Legends: categories, a color bar, bubble sizes. | Legends: categories, a color bar, bubble sizes. | 6.0 | |
interaction.crosshair A line that follows the pointer across all series. | A line that follows the pointer across all series. | 2.1 | |
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 14 blocks 51.7 | 81.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/small-multiples.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
small-multiples.json
{
"title": "Small multiples",
"data": {
"sales": { "csv": "month,region,product,units\n2024-01-01,North,Coffee,120\n2024-01-01,North,Tea,80\n2024-02-01,North,Coffee,132\n2024-02-01,North,Tea,84\n2024-03-01,North,Coffee,141\n2024-03-01,North,Tea,79\n2024-04-01,North,Coffee,150\n2024-04-01,North,Tea,88\n2024-01-01,South,Coffee,64\n2024-01-01,South,Tea,110\n2024-02-01,South,Coffee,70\n2024-02-01,South,Tea,118\n2024-03-01,South,Coffee,73\n2024-03-01,South,Tea,126\n2024-04-01,South,Coffee,81\n2024-04-01,South,Tea,133\n2024-01-01,East,Coffee,35\n2024-01-01,East,Tea,42\n2024-02-01,East,Coffee,39\n2024-02-01,East,Tea,41\n2024-03-01,East,Coffee,44\n2024-03-01,East,Tea,46\n2024-04-01,East,Coffee,52\n2024-04-01,East,Tea,47\n2024-01-01,West,Coffee,91\n2024-01-01,West,Tea,30\n2024-02-01,West,Coffee,88\n2024-02-01,West,Tea,33\n2024-03-01,West,Coffee,95\n2024-03-01,West,Tea,31\n2024-04-01,West,Coffee,102\n2024-04-01,West,Tea,36\n" }
},
"charts": [
{
"title": "Units by product, one panel per region",
"note": "Coffee sells best in North and West, tea in South; in East coffee overtook tea in April.",
"how": "`facet` draws the chart once for each value of a field, on the same axes, so the panels can be compared at a glance. The legend is drawn once, above them.",
"facet": { "field": "region", "columns": 2 },
"spec": {
"data": "sales",
"size": [300, 180],
"padding": { "top": 8, "right": 8, "bottom": 8, "left": 8 },
"scales": {
"x": { "type": "time" },
"y": { "type": "linear", "zero": true },
"color": { "type": "color" }
},
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x", "format": { "month": "short" } },
{ "type": "axis", "scale": "y" },
{ "type": "legend", "scale": "color", "position": "top" }
],
"marks": [{ "type": "line", "x": "month", "y": "units", "color": "product", "curve": "smooth" }],
"interaction": [
{ "type": "tooltip", "fields": ["month", "product", "units"], "formats": { "month": "month" } },
{ "type": "crosshair" }
]
}
}
]
}
The report the composer makes from it is one standalone file (91.2 KB): open it from disk, mail it, print it.
In a page of your own
The charts above use the full kit (81.8 KB gzipped), the smallest one that has the blocks this example needs:
lib.curve, lib.pointer, lib.ticks, scale.color, scale.linear, scale.time, transform.filter, mark.line, guide.axis, guide.grid, guide.legend, interaction.crosshair, 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/full.js"></script>
<script type="application/json" id="bc-data">
{"sales":{"columns":{"month":["2024-01-01","2024-01-01","2024-02-01","2024-02-01","2024-03-01","2024-03-01","2024-04-01","2024-04-01","2024-01-01","2024-01-01","2024-02-01","2024-02-01","2024-03-01","2024-03-01","2024-04-01","2024-04-01","2024-01-01","2024-01-01","2024-02-01","2024-02-01","2024-03-01","2024-03-01","2024-04-01","2024-04-01","2024-01-01","2024-01-01","2024-02-01","2024-02-01","2024-03-01","2024-03-01","2024-04-01","2024-04-01"],"region":["North","North","North","North","North","North","North","North","South","South","South","South","South","South","South","South","East","East","East","East","East","East","East","East","West","West","West","West","West","West","West","West"],"product":["Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea","Coffee","Tea"],"units":[120,80,132,84,141,79,150,88,64,110,70,118,73,126,81,133,35,42,39,41,44,46,52,47,91,30,88,33,95,31,102,36]}}}
</script>
<h3>North</h3>
<script type="application/json" data-bc-chart>
{
"data": "sales",
"size": [
300,
180
],
"padding": {
"top": 8,
"right": 8,
"bottom": 8,
"left": 8
},
"scales": {
"x": {
"type": "time",
"domain": [
1704067200000,
1711929600000
],
"nice": false
},
"y": {
"type": "linear",
"zero": true,
"domain": [
0,
160
],
"nice": false
},
"color": {
"type": "color",
"domain": [
"Coffee",
"Tea"
]
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x",
"format": {
"month": "short"
}
},
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "line",
"x": "month",
"y": "units",
"color": "product",
"curve": "smooth"
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"month",
"product",
"units"
],
"formats": {
"month": "month"
}
},
{
"type": "crosshair"
}
],
"alt": "Units by product, one panel per region: North",
"transforms": [
{
"type": "filter",
"field": "region",
"value": "North"
}
]
}
</script>
<h3>South</h3>
<script type="application/json" data-bc-chart>
{
"data": "sales",
"size": [
300,
180
],
"padding": {
"top": 8,
"right": 8,
"bottom": 8,
"left": 8
},
"scales": {
"x": {
"type": "time",
"domain": [
1704067200000,
1711929600000
],
"nice": false
},
"y": {
"type": "linear",
"zero": true,
"domain": [
0,
160
],
"nice": false
},
"color": {
"type": "color",
"domain": [
"Coffee",
"Tea"
]
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x",
"format": {
"month": "short"
}
},
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "line",
"x": "month",
"y": "units",
"color": "product",
"curve": "smooth"
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"month",
"product",
"units"
],
"formats": {
"month": "month"
}
},
{
"type": "crosshair"
}
],
"alt": "Units by product, one panel per region: South",
"transforms": [
{
"type": "filter",
"field": "region",
"value": "South"
}
]
}
</script>
<h3>East</h3>
<script type="application/json" data-bc-chart>
{
"data": "sales",
"size": [
300,
180
],
"padding": {
"top": 8,
"right": 8,
"bottom": 8,
"left": 8
},
"scales": {
"x": {
"type": "time",
"domain": [
1704067200000,
1711929600000
],
"nice": false
},
"y": {
"type": "linear",
"zero": true,
"domain": [
0,
160
],
"nice": false
},
"color": {
"type": "color",
"domain": [
"Coffee",
"Tea"
]
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x",
"format": {
"month": "short"
}
},
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "line",
"x": "month",
"y": "units",
"color": "product",
"curve": "smooth"
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"month",
"product",
"units"
],
"formats": {
"month": "month"
}
},
{
"type": "crosshair"
}
],
"alt": "Units by product, one panel per region: East",
"transforms": [
{
"type": "filter",
"field": "region",
"value": "East"
}
]
}
</script>
<h3>West</h3>
<script type="application/json" data-bc-chart>
{
"data": "sales",
"size": [
300,
180
],
"padding": {
"top": 8,
"right": 8,
"bottom": 8,
"left": 8
},
"scales": {
"x": {
"type": "time",
"domain": [
1704067200000,
1711929600000
],
"nice": false
},
"y": {
"type": "linear",
"zero": true,
"domain": [
0,
160
],
"nice": false
},
"color": {
"type": "color",
"domain": [
"Coffee",
"Tea"
]
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x",
"format": {
"month": "short"
}
},
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "line",
"x": "month",
"y": "units",
"color": "product",
"curve": "smooth"
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"month",
"product",
"units"
],
"formats": {
"month": "month"
}
},
{
"type": "crosshair"
}
],
"alt": "Units by product, one panel per region: West",
"transforms": [
{
"type": "filter",
"field": "region",
"value": "West"
}
]
}
</script>