Treemap
Shares of a whole across two levels (region, then product).
68.7 KB of code · core 29.5 + 11 blocks 39.2
Revenue by region and product
North America brings the most revenue, 42% of it; Enterprise plans bring half.
How it is made: transform.treemap lays the rows out as near-square cells for the shape of the plot, one block per region (group), each cell's area its revenue. It writes the corners (x0, x1, y0, y1) in 0..1, drawn by a rect on two linear scales with the domain [0, 1] and no axes, and each cell's name as label: the text with x2 and y2 leaves out a name its cell has no room for (minSize 10: smaller would not be read), and on keeps it readable on the cell's color.
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, 68.7 KB of code, minified, which is 32% of the 215.9 KB that the core and every block weigh together. The report file is 75.7 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.columns Checks the columns a transform reads and writes. Shared by contour, funnel, hexbin, sankey, treemap and waterfall. | Checks the columns a transform reads and writes. Shared by contour, funnel, hexbin, sankey, treemap and waterfall. | 0.6 | |
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 | |
transform.treemap Lays out the rectangles of a treemap. | Lays out the rectangles of a treemap. | 3.5 | |
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.legend Legends: categories, a color bar, bubble sizes. | Legends: categories, a color bar, bubble sizes. | 6.0 | |
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 39.2 | 68.7 |
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/treemap.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
treemap.json
{
"title": "Treemap",
"data": {
"sales": [
{ "region": "North America", "product": "Enterprise", "revenue": 4200000 },
{ "region": "North America", "product": "Pro", "revenue": 2600000 },
{ "region": "North America", "product": "Starter", "revenue": 900000 },
{ "region": "Europe", "product": "Enterprise", "revenue": 3100000 },
{ "region": "Europe", "product": "Pro", "revenue": 2200000 },
{ "region": "Europe", "product": "Starter", "revenue": 700000 },
{ "region": "Asia Pacific", "product": "Enterprise", "revenue": 1900000 },
{ "region": "Asia Pacific", "product": "Pro", "revenue": 1500000 },
{ "region": "Asia Pacific", "product": "Starter", "revenue": 600000 },
{ "region": "Latin America", "product": "Pro", "revenue": 500000 },
{ "region": "Latin America", "product": "Starter", "revenue": 300000 }
]
},
"charts": [
{
"title": "Revenue by region and product",
"note": "North America brings the most revenue, 42% of it; Enterprise plans bring half.",
"how": "transform.treemap lays the rows out as near-square cells for the shape of the plot, one block per region (group), each cell's area its revenue. It writes the corners (x0, x1, y0, y1) in 0..1, drawn by a rect on two linear scales with the domain [0, 1] and no axes, and each cell's name as `label`: the text with x2 and y2 leaves out a name its cell has no room for (`minSize` 10: smaller would not be read), and `on` keeps it readable on the cell's color.",
"spec": {
"data": "sales",
"size": [720, 400],
"padding": { "top": 0, "right": 0, "bottom": 0, "left": 0 },
"transforms": [{ "type": "treemap", "field": "revenue", "group": "region", "name": "product", "padding": 0.006 }],
"scales": {
"x": { "type": "linear", "domain": [0, 1] },
"y": { "type": "linear", "domain": [0, 1] },
"color": { "type": "color" }
},
"guides": [{ "type": "legend", "scale": "color", "position": "bottom" }],
"marks": [
{ "type": "rect", "x": "x0", "x2": "x1", "y": "y1", "y2": "y0", "color": "region", "radius": 3 },
{ "type": "text", "x": "x0", "x2": "x1", "y": "y1", "y2": "y0", "text": "label", "on": "region", "anchor": "start", "baseline": "hanging", "dx": 8, "dy": 8, "size": 12, "minSize": 10, "weight": 600 }
],
"interaction": [{ "type": "tooltip", "fields": ["region", "product", "revenue"], "formats": { "revenue": { "style": "currency", "currency": "USD", "notation": "compact", "maximumFractionDigits": 1 } } }]
}
}
]
}
The report the composer makes from it is one standalone file (75.7 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.columns, lib.pointer, lib.ticks, scale.color, scale.linear, transform.treemap, mark.rect, mark.text, guide.legend, 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">
{"sales":[{"region":"North America","product":"Enterprise","revenue":4200000},{"region":"North America","product":"Pro","revenue":2600000},{"region":"North America","product":"Starter","revenue":900000},{"region":"Europe","product":"Enterprise","revenue":3100000},{"region":"Europe","product":"Pro","revenue":2200000},{"region":"Europe","product":"Starter","revenue":700000},{"region":"Asia Pacific","product":"Enterprise","revenue":1900000},{"region":"Asia Pacific","product":"Pro","revenue":1500000},{"region":"Asia Pacific","product":"Starter","revenue":600000},{"region":"Latin America","product":"Pro","revenue":500000},{"region":"Latin America","product":"Starter","revenue":300000}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "sales",
"size": [
720,
400
],
"padding": {
"top": 0,
"right": 0,
"bottom": 0,
"left": 0
},
"transforms": [
{
"type": "treemap",
"field": "revenue",
"group": "region",
"name": "product",
"padding": 0.006
}
],
"scales": {
"x": {
"type": "linear",
"domain": [
0,
1
]
},
"y": {
"type": "linear",
"domain": [
0,
1
]
},
"color": {
"type": "color"
}
},
"guides": [
{
"type": "legend",
"scale": "color",
"position": "bottom"
}
],
"marks": [
{
"type": "rect",
"x": "x0",
"x2": "x1",
"y": "y1",
"y2": "y0",
"color": "region",
"radius": 3
},
{
"type": "text",
"x": "x0",
"x2": "x1",
"y": "y1",
"y2": "y0",
"text": "label",
"on": "region",
"anchor": "start",
"baseline": "hanging",
"dx": 8,
"dy": 8,
"size": 12,
"minSize": 10,
"weight": 600
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"region",
"product",
"revenue"
],
"formats": {
"revenue": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 1
}
}
}
],
"alt": "Revenue by region and product"
}
</script>