Gallery / Comparing categories /
Calculated fields
A number computed from the columns (profit from revenue and cost, a margin).
82.2 KB of code · core 29.5 + 14 blocks 52.7
Profit per quarter
Profit more than tripled in two years, from 58,000 in Q1 2023 to 187,000 in Q4 2024; it fell only in Q1 2024.
How it is made: The data has revenue and cost only. transform.calculate subtracts one from the other into a new column, profit, which the bars and their labels use like any other field.
On a page of its own this chart would need 11 blocks, 72.7 KB with the core:
Margin per quarter
The margin rose from 14% to 25% of revenue: profit grew faster than sales.
How it is made: Calculations chain: profit first, then margin = profit / revenue. A margin is a fraction (0.15), so the axis and the labels use the percent format.
On a page of its own this chart would need 13 blocks, 77.7 KB with the core:
What the report is made of
The composer reads the JSON below and puts into the page the core and only the blocks its charts use: 14 of the 59, 82.2 KB of code, minified, which is 38% of the 215.9 KB that the core and every block weigh together. A block that two charts use is carried once. The report file is 90.4 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.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.calculate A new column from two numbers: a margin, a gap to plan. | A new column from two numbers: a margin, a gap to plan. | 1.4 | |
mark.line A line per series, with optional end labels. | A line per series, with optional end labels. | 4.4 | |
mark.point Dots, bubbles and beeswarms. | Dots, bubbles and beeswarms. | 3.9 | |
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 14 blocks 52.7 | 82.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/calculated-field.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
calculated-field.json
{
"title": "Calculated fields",
"data": {
"pnl": [
{
"quarter": "Q1 2023",
"revenue": 410000,
"cost": 352000
},
{
"quarter": "Q2 2023",
"revenue": 452000,
"cost": 380000
},
{
"quarter": "Q3 2023",
"revenue": 488000,
"cost": 401000
},
{
"quarter": "Q4 2023",
"revenue": 560000,
"cost": 452000
},
{
"quarter": "Q1 2024",
"revenue": 530000,
"cost": 431000
},
{
"quarter": "Q2 2024",
"revenue": 590000,
"cost": 463000
},
{
"quarter": "Q3 2024",
"revenue": 640000,
"cost": 489000
},
{
"quarter": "Q4 2024",
"revenue": 735000,
"cost": 548000
}
]
},
"charts": [
{
"title": "Profit per quarter",
"note": "Profit more than tripled in two years, from 58,000 in Q1 2023 to 187,000 in Q4 2024; it fell only in Q1 2024.",
"how": "The data has revenue and cost only. transform.calculate subtracts one from the other into a new column, profit, which the bars and their labels use like any other field.",
"spec": {
"data": "pnl",
"size": [
680,
300
],
"padding": {
"top": 24
},
"transforms": [
{
"type": "calculate",
"a": "revenue",
"op": "-",
"b": "cost",
"as": "profit"
}
],
"scales": {
"x": {
"type": "band"
},
"y": {
"type": "linear",
"zero": true,
"nice": true
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x"
},
{
"type": "axis",
"scale": "y",
"format": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
}
}
],
"marks": [
{
"type": "rect",
"x": "quarter",
"y": "profit",
"radius": 2
},
{
"type": "text",
"x": "quarter",
"y": "profit",
"text": "profit",
"format": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"baseline": "auto",
"dy": -5
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"quarter",
"revenue",
"cost",
"profit"
],
"formats": {
"revenue": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"cost": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"profit": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
}
}
}
]
}
},
{
"title": "Margin per quarter",
"note": "The margin rose from 14% to 25% of revenue: profit grew faster than sales.",
"how": "Calculations chain: profit first, then margin = profit / revenue. A margin is a fraction (0.15), so the axis and the labels use the percent format.",
"spec": {
"data": "pnl",
"size": [
680,
280
],
"padding": {
"top": 24
},
"transforms": [
{
"type": "calculate",
"a": "revenue",
"op": "-",
"b": "cost",
"as": "profit"
},
{
"type": "calculate",
"a": "profit",
"op": "/",
"b": "revenue",
"as": "margin"
}
],
"scales": {
"x": {
"type": "band"
},
"y": {
"type": "linear",
"zero": true,
"nice": true
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x"
},
{
"type": "axis",
"scale": "y",
"format": "percent"
}
],
"marks": [
{
"type": "line",
"x": "quarter",
"y": "margin",
"strokeWidth": 2.5,
"stroke": "var(--bc-c0, #3a6fe4)"
},
{
"type": "point",
"x": "quarter",
"y": "margin",
"r": 4,
"fill": "var(--bc-c0, #3a6fe4)"
},
{
"type": "text",
"x": "quarter",
"y": "margin",
"text": "margin",
"format": {
"style": "percent",
"maximumFractionDigits": 1
},
"baseline": "auto",
"dy": -10
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"quarter",
"profit",
"margin"
],
"formats": {
"profit": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"margin": {
"style": "percent",
"maximumFractionDigits": 1
}
}
}
]
}
}
]
}
The report the composer makes from it is one standalone file (90.4 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.curve, lib.pointer, lib.ticks, scale.band, scale.linear, transform.calculate, mark.line, mark.point, 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">
{"pnl":[{"quarter":"Q1 2023","revenue":410000,"cost":352000},{"quarter":"Q2 2023","revenue":452000,"cost":380000},{"quarter":"Q3 2023","revenue":488000,"cost":401000},{"quarter":"Q4 2023","revenue":560000,"cost":452000},{"quarter":"Q1 2024","revenue":530000,"cost":431000},{"quarter":"Q2 2024","revenue":590000,"cost":463000},{"quarter":"Q3 2024","revenue":640000,"cost":489000},{"quarter":"Q4 2024","revenue":735000,"cost":548000}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "pnl",
"size": [
680,
300
],
"padding": {
"top": 24
},
"transforms": [
{
"type": "calculate",
"a": "revenue",
"op": "-",
"b": "cost",
"as": "profit"
}
],
"scales": {
"x": {
"type": "band"
},
"y": {
"type": "linear",
"zero": true,
"nice": true
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x"
},
{
"type": "axis",
"scale": "y",
"format": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
}
}
],
"marks": [
{
"type": "rect",
"x": "quarter",
"y": "profit",
"radius": 2
},
{
"type": "text",
"x": "quarter",
"y": "profit",
"text": "profit",
"format": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"baseline": "auto",
"dy": -5
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"quarter",
"revenue",
"cost",
"profit"
],
"formats": {
"revenue": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"cost": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"profit": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
}
}
}
],
"alt": "Profit per quarter"
}
</script>
<script type="application/json" data-bc-chart>
{
"data": "pnl",
"size": [
680,
280
],
"padding": {
"top": 24
},
"transforms": [
{
"type": "calculate",
"a": "revenue",
"op": "-",
"b": "cost",
"as": "profit"
},
{
"type": "calculate",
"a": "profit",
"op": "/",
"b": "revenue",
"as": "margin"
}
],
"scales": {
"x": {
"type": "band"
},
"y": {
"type": "linear",
"zero": true,
"nice": true
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x"
},
{
"type": "axis",
"scale": "y",
"format": "percent"
}
],
"marks": [
{
"type": "line",
"x": "quarter",
"y": "margin",
"strokeWidth": 2.5,
"stroke": "var(--bc-c0, #3a6fe4)"
},
{
"type": "point",
"x": "quarter",
"y": "margin",
"r": 4,
"fill": "var(--bc-c0, #3a6fe4)"
},
{
"type": "text",
"x": "quarter",
"y": "margin",
"text": "margin",
"format": {
"style": "percent",
"maximumFractionDigits": 1
},
"baseline": "auto",
"dy": -10
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"quarter",
"profit",
"margin"
],
"formats": {
"profit": {
"style": "currency",
"currency": "USD",
"notation": "compact",
"maximumFractionDigits": 0
},
"margin": {
"style": "percent",
"maximumFractionDigits": 1
}
}
}
],
"alt": "Margin per quarter"
}
</script>