Gallery / Change and ranking /
Waterfall
How a start value becomes an end value through gains and losses (profit bridge).
74.0 KB of code · core 29.5 + 12 blocks 44.5
From revenue to operating profit
Of $892K of revenue, $248K is left as operating profit; after the cost of sales, salaries take the most.
How it is made: transform.waterfall keeps a running total in row order: each bar starts where the last one ended. Rows marked by the total field are subtotals, drawn from 0. The step column (increase, decrease, total) is colored by a color scale with a fixed domain and range. Each figure (amount) stands where its bar ends (value1): above a gain or a total, under a loss (negative: "flip" with sign: "amount"), so no two figures stand side by side. With a figure on every bar the chart needs no value axis.
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: 12 of the 59, 74.0 KB of code, minified, which is 34% of the 215.9 KB that the core and every block weigh together. The report file is 80.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.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.band One band per category: the axis of bar charts. | One band per category: the axis of bar charts. | 1.2 | |
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.waterfall Increases, decreases and subtotals of a bridge chart. | Increases, decreases and subtotals of a bridge chart. | 1.8 | |
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 | |
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 12 blocks 44.5 | 74.0 |
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/waterfall.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
waterfall.json
{
"title": "Waterfall",
"data": {
"bridge": [
{ "item": "Revenue", "value": 891600, "subtotal": false },
{ "item": "Cost of sales", "value": -348200, "subtotal": false },
{ "item": "Gross profit", "value": null, "subtotal": true },
{ "item": "Marketing", "value": -121500, "subtotal": false },
{ "item": "Salaries", "value": -207800, "subtotal": false },
{ "item": "Other income", "value": 33900, "subtotal": false },
{ "item": "Operating profit", "value": null, "subtotal": true }
]
},
"charts": [
{
"title": "From revenue to operating profit",
"note": "Of $892K of revenue, $248K is left as operating profit; after the cost of sales, salaries take the most.",
"how": "transform.waterfall keeps a running total in row order: each bar starts where the last one ended. Rows marked by the `total` field are subtotals, drawn from 0. The `step` column (increase, decrease, total) is colored by a color scale with a fixed domain and range. Each figure (`amount`) stands where its bar ends (`value1`): above a gain or a total, under a loss (`negative: \"flip\"` with `sign: \"amount\"`), so no two figures stand side by side. With a figure on every bar the chart needs no value axis.",
"spec": {
"data": "bridge",
"size": [720, 340],
"padding": { "top": 28 },
"transforms": [{ "type": "waterfall", "field": "value", "total": "subtotal" }],
"scales": {
"x": { "type": "band", "paddingInner": 0.28 },
"y": { "type": "linear", "zero": true },
"color": { "type": "color", "domain": ["increase", "decrease", "total"], "range": ["var(--bc-c6, #2e9e62)", "var(--bc-c3, #e0527a)", "var(--bc-c0, #3a6fe4)"] }
},
"guides": [{ "type": "axis", "scale": "x" }],
"marks": [
{ "type": "rect", "x": "item", "y": "value1", "y2": "value0", "color": "step", "radius": 3 },
{ "type": "text", "x": "item", "y": "value1", "text": "amount", "sign": "amount", "negative": "flip", "format": { "style": "currency", "currency": "USD", "notation": "compact", "maximumFractionDigits": 0 }, "baseline": "auto", "dy": -6, "weight": 600 }
],
"interaction": [
{
"type": "tooltip",
"fields": ["amount"],
"formats": { "amount": { "style": "currency", "currency": "USD", "maximumFractionDigits": 0 } }
}
]
}
}
]
}
The report the composer makes from it is one standalone file (80.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.columns, lib.pointer, lib.ticks, scale.band, scale.color, scale.linear, transform.waterfall, mark.rect, mark.text, guide.axis, 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">
{"bridge":[{"item":"Revenue","value":891600,"subtotal":false},{"item":"Cost of sales","value":-348200,"subtotal":false},{"item":"Gross profit","value":null,"subtotal":true},{"item":"Marketing","value":-121500,"subtotal":false},{"item":"Salaries","value":-207800,"subtotal":false},{"item":"Other income","value":33900,"subtotal":false},{"item":"Operating profit","value":null,"subtotal":true}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "bridge",
"size": [
720,
340
],
"padding": {
"top": 28
},
"transforms": [
{
"type": "waterfall",
"field": "value",
"total": "subtotal"
}
],
"scales": {
"x": {
"type": "band",
"paddingInner": 0.28
},
"y": {
"type": "linear",
"zero": true
},
"color": {
"type": "color",
"domain": [
"increase",
"decrease",
"total"
],
"range": [
"var(--bc-c6, #2e9e62)",
"var(--bc-c3, #e0527a)",
"var(--bc-c0, #3a6fe4)"
]
}
},
"guides": [
{
"type": "axis",
"scale": "x"
}
],
"marks": [
{
"type": "rect",
"x": "item",
"y": "value1",
"y2": "value0",
"color": "step",
"radius": 3
},
{
"type": "text",
"x": "item",
"y": "value1",
"text": "amount",
"sign": "amount",
"negative": "flip",
"format": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"baseline": "auto",
"dy": -6,
"weight": 600
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"amount"
],
"formats": {
"amount": {
"style": "currency",
"currency": "USD",
"maximumFractionDigits": 0
}
}
}
],
"alt": "From revenue to operating profit"
}
</script>