Gallery / Stages, flows and plans /
Funnel
Conversion through the stages of a process.
72.7 KB of code · core 29.5 + 11 blocks 43.2
Checkout funnel
Of 48,200 visits, 2,270 ended in a purchase (4.7%); the steepest drop is from viewing a product to adding it to the cart, where seven in ten leave.
How it is made: transform.funnel centers each stage on zero (users0 = -users/2, users1 = +users/2), so the bars narrow like a funnel, and adds the share of the first stage (rate) and of the one before (stepRate). A text mark writes the rate to the right of each bar; the tooltip shows all three.
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, 72.7 KB of code, minified, which is 34% of the 215.9 KB that the core and every block weigh together. The report file is 79.1 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.linear Numbers to positions, with round ticks. | Numbers to positions, with round ticks. | 1.6 | |
transform.funnel Stage widths and conversion rates of a funnel. | Stage widths and conversion rates of a funnel. | 1.4 | |
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 11 blocks 43.2 | 72.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/funnel.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
funnel.json
{
"title": "Funnel",
"data": {
"funnel": [
{ "stage": "Visits", "users": 48200 },
{ "stage": "Product views", "users": 21500 },
{ "stage": "Add to cart", "users": 6400 },
{ "stage": "Checkout", "users": 3100 },
{ "stage": "Purchase", "users": 2270 }
]
},
"charts": [
{
"title": "Checkout funnel",
"note": "Of 48,200 visits, 2,270 ended in a purchase (4.7%); the steepest drop is from viewing a product to adding it to the cart, where seven in ten leave.",
"how": "transform.funnel centers each stage on zero (users0 = -users/2, users1 = +users/2), so the bars narrow like a funnel, and adds the share of the first stage (rate) and of the one before (stepRate). A text mark writes the rate to the right of each bar; the tooltip shows all three.",
"spec": {
"data": "funnel",
"size": [720, 320],
"padding": { "right": 64 },
"transforms": [{ "type": "funnel", "field": "users" }],
"scales": { "y": { "type": "band", "paddingInner": 0.22 }, "x": { "type": "linear" } },
"guides": [{ "type": "axis", "scale": "y" }],
"marks": [
{ "type": "rect", "x": "users0", "x2": "users1", "y": "stage", "radius": 4 },
{ "type": "text", "x": "users1", "y": "stage", "text": "rate", "format": { "style": "percent", "maximumFractionDigits": 1 }, "anchor": "start", "dx": 8, "weight": 600 }
],
"interaction": [
{
"type": "tooltip",
"fields": ["users", "rate", "stepRate"],
"formats": {
"users": "integer",
"rate": { "style": "percent", "maximumFractionDigits": 1 },
"stepRate": { "style": "percent", "maximumFractionDigits": 1 }
},
"labels": { "users": "Users" }
}
]
}
}
]
}
The report the composer makes from it is one standalone file (79.1 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.linear, transform.funnel, 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">
{"funnel":[{"stage":"Visits","users":48200},{"stage":"Product views","users":21500},{"stage":"Add to cart","users":6400},{"stage":"Checkout","users":3100},{"stage":"Purchase","users":2270}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "funnel",
"size": [
720,
320
],
"padding": {
"right": 64
},
"transforms": [
{
"type": "funnel",
"field": "users"
}
],
"scales": {
"y": {
"type": "band",
"paddingInner": 0.22
},
"x": {
"type": "linear"
}
},
"guides": [
{
"type": "axis",
"scale": "y"
}
],
"marks": [
{
"type": "rect",
"x": "users0",
"x2": "users1",
"y": "stage",
"radius": 4
},
{
"type": "text",
"x": "users1",
"y": "stage",
"text": "rate",
"format": {
"style": "percent",
"maximumFractionDigits": 1
},
"anchor": "start",
"dx": 8,
"weight": 600
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"users",
"rate",
"stepRate"
],
"formats": {
"users": "integer",
"rate": {
"style": "percent",
"maximumFractionDigits": 1
},
"stepRate": {
"style": "percent",
"maximumFractionDigits": 1
}
},
"labels": {
"users": "Users"
}
}
],
"alt": "Checkout funnel"
}
</script>