Share over time
Shares over time.
80.5 KB of code · core 29.5 + 14 blocks 51.0
Traffic mix (100%)
Direct grew from 24% of visits in January to 32% in May, at the cost of search and social.
How it is made: normalize: true scales every stack to 0..1, so the areas show shares rather than totals. follows: the stack is rebuilt when the legend hides a series, so the rest add up to 100% again.
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, 80.5 KB of code, minified, which is 37% of the 215.9 KB that the core and every block weigh together. The report file is 87.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.stack Stacks series on top of each other, to 100%, or around a center line (a streamgraph). | Stacks series on top of each other, to 100%, or around a center line (a streamgraph). | 2.2 | |
mark.area Filled areas, stacked or not. | Filled areas, stacked or not. | 2.9 | |
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.legend-filter Click a legend item to hide or show its series. | Click a legend item to hide or show its series. | 3.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 14 blocks 51.0 | 80.5 |
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/stacked-area-100.json report.html.
Saved as page.json, it composes the same with the skill's own runtime/compose.js.
stacked-area-100.json
{
"title": "Share over time",
"data": {
"sources": {
"columns": {
"month": ["2024-01-01", "2024-02-01", "2024-03-01", "2024-04-01", "2024-05-01",
"2024-01-01", "2024-02-01", "2024-03-01", "2024-04-01", "2024-05-01",
"2024-01-01", "2024-02-01", "2024-03-01", "2024-04-01", "2024-05-01"],
"source": ["Search", "Search", "Search", "Search", "Search",
"Social", "Social", "Social", "Social", "Social",
"Direct", "Direct", "Direct", "Direct", "Direct"],
"visits": [400, 420, 380, 450, 470,
250, 300, 320, 310, 280,
200, 210, 260, 300, 350]
}
}
},
"charts": [
{
"title": "Traffic mix (100%)",
"note": "Direct grew from 24% of visits in January to 32% in May, at the cost of search and social.",
"how": "normalize: true scales every stack to 0..1, so the areas show shares rather than totals. follows: the stack is rebuilt when the legend hides a series, so the rest add up to 100% again.",
"spec": {
"data": "sources",
"transforms": [{ "type": "stack", "field": "visits", "by": "month", "group": "source", "normalize": true, "follows": "color" }],
"scales": {
"x": { "type": "time" },
"y": { "type": "linear", "domain": [0, 1] },
"color": { "type": "color" }
},
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x", "count": 12, "anchor": "end" },
{ "type": "axis", "scale": "y", "format": "percent" },
{ "type": "legend", "scale": "color", "position": "top", "title": "Source" }
],
"marks": [{ "type": "area", "x": "month", "y": "visits1", "y2": "visits0", "color": "source" }],
"interaction": [{ "type": "tooltip", "fields": ["month", "source", "visits"], "labels": { "source": "Source", "visits": "Visits" }, "formats": { "month": "month" } }, { "type": "legend-filter" }]
}
}
]
}
The report the composer makes from it is one standalone file (87.2 KB): open it from disk, mail it, print it.
In a page of your own
The charts above use the timeseries kit (52.1 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.stack, mark.area, guide.axis, guide.grid, guide.legend, interaction.legend-filter, 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/timeseries.js"></script>
<script type="application/json" id="bc-data">
{"sources":{"columns":{"month":["2024-01-01","2024-02-01","2024-03-01","2024-04-01","2024-05-01","2024-01-01","2024-02-01","2024-03-01","2024-04-01","2024-05-01","2024-01-01","2024-02-01","2024-03-01","2024-04-01","2024-05-01"],"source":["Search","Search","Search","Search","Search","Social","Social","Social","Social","Social","Direct","Direct","Direct","Direct","Direct"],"visits":[400,420,380,450,470,250,300,320,310,280,200,210,260,300,350]}}}
</script>
<script type="application/json" data-bc-chart>
{
"data": "sources",
"transforms": [
{
"type": "stack",
"field": "visits",
"by": "month",
"group": "source",
"normalize": true,
"follows": "color"
}
],
"scales": {
"x": {
"type": "time"
},
"y": {
"type": "linear",
"domain": [
0,
1
]
},
"color": {
"type": "color"
}
},
"guides": [
{
"type": "grid",
"scale": "y"
},
{
"type": "axis",
"scale": "x",
"count": 12,
"anchor": "end"
},
{
"type": "axis",
"scale": "y",
"format": "percent"
},
{
"type": "legend",
"scale": "color",
"position": "top",
"title": "Source"
}
],
"marks": [
{
"type": "area",
"x": "month",
"y": "visits1",
"y2": "visits0",
"color": "source"
}
],
"interaction": [
{
"type": "tooltip",
"fields": [
"month",
"source",
"visits"
],
"labels": {
"source": "Source",
"visits": "Visits"
},
"formats": {
"month": "month"
}
},
{
"type": "legend-filter"
}
],
"alt": "Traffic mix (100%)"
}
</script>