blockcharts alpha

Gallery / Trends over time /

Stacked bars with a line, and shares of the stack

Stacked bars with a line of another measure (a margin) on a second axis; segments labelled with their share of the bar.

93.9 KB of code · core 29.5 + 17 blocks 64.4

Revenue by product line, $M (bars) and operating margin (line, right)

Revenue grew every quarter, from $60M to $94M; the margin dipped to 19% in Q3 and peaked at 27% in Q4.

How it is made: All marks of a chart draw from one table. After the stack there is a row per quarter and product, so the line's field must have the same value in every row of a quarter (margin here), or the line jumps up and down within it. The line goes on a second scale y2 with its axis on the right.

Each line's share of its quarter

Services grew from 17% of the quarter’s revenue in Q1 to 28% in Q4.

How it is made: transform.window with op "total" writes the sum of each quarter on every row of it, and op "share" each row as a part of that sum, without collapsing the rows as aggregate would. The share labels the segments, and the total stands over each stack: it is on every row of its quarter, and a label repeated on one spot is written once.

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: 17 of the 59, 93.9 KB of code, minified, which is 43% 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 102.7 KB: that code, the data, the styles and the page around them.

BlockWhat it doesShare of the codeKB
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.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.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
transform.window
Moving averages, running totals, change from the previous row; totals, shares and ranks in a group.
Moving averages, running totals, change from the previous row; totals, shares and ranks in a group. 4.0
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
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
TotalThe core 29.5 and 17 blocks 64.493.9

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/bars-and-line.json report.html. Saved as page.json, it composes the same with the skill's own runtime/compose.js.

bars-and-line.json

{
  "title": "Stacked bars with a line, and shares of the stack",
  "data": {
    "revenue": [
      { "quarter": "Q1", "line": "Hardware", "amount": 30, "margin": 0.21 },
      { "quarter": "Q1", "line": "Software", "amount": 20, "margin": 0.21 },
      { "quarter": "Q1", "line": "Services", "amount": 10, "margin": 0.21 },
      { "quarter": "Q2", "line": "Hardware", "amount": 35, "margin": 0.24 },
      { "quarter": "Q2", "line": "Software", "amount": 25, "margin": 0.24 },
      { "quarter": "Q2", "line": "Services", "amount": 15, "margin": 0.24 },
      { "quarter": "Q3", "line": "Hardware", "amount": 28, "margin": 0.19 },
      { "quarter": "Q3", "line": "Software", "amount": 30, "margin": 0.19 },
      { "quarter": "Q3", "line": "Services", "amount": 22, "margin": 0.19 },
      { "quarter": "Q4", "line": "Hardware", "amount": 40, "margin": 0.27 },
      { "quarter": "Q4", "line": "Software", "amount": 28, "margin": 0.27 },
      { "quarter": "Q4", "line": "Services", "amount": 26, "margin": 0.27 }
    ]
  },
  "charts": [
    {
      "title": "Revenue by product line, $M (bars) and operating margin (line, right)",
      "note": "Revenue grew every quarter, from $60M to $94M; the margin dipped to 19% in Q3 and peaked at 27% in Q4.",
      "how": "All marks of a chart draw from one table. After the stack there is a row per quarter and product, so the line's field must have the same value in every row of a quarter (margin here), or the line jumps up and down within it. The line goes on a second scale y2 with its axis on the right.",
      "spec": {
        "data": "revenue",
        "size": [720, 340],
        "transforms": [{ "type": "stack", "field": "amount", "by": "quarter", "group": "line" }],
        "scales": {
          "x": { "type": "band" },
          "y": { "type": "linear", "zero": true },
          "y2": { "type": "linear", "zero": true, "range": "height" },
          "color": { "type": "color" }
        },
        "guides": [
          { "type": "grid", "scale": "y" },
          { "type": "axis", "scale": "x" },
          { "type": "axis", "scale": "y" },
          { "type": "axis", "scale": "y2", "position": "right", "format": "percent" },
          { "type": "legend", "scale": "color", "position": "auto" }
        ],
        "marks": [
          { "type": "rect", "x": "quarter", "y": "amount1", "y2": "amount0", "color": "line" },
          { "type": "line", "x": "quarter", "y": { "field": "margin", "scale": "y2" }, "stroke": "var(--bc-text, #1e293b)" },
          { "type": "point", "x": "quarter", "y": { "field": "margin", "scale": "y2" }, "r": 4, "fill": "var(--bc-text, #1e293b)" }
        ],
        "interaction": [
          {
            "type": "tooltip",
            "fields": ["line", "amount", "margin"],
            "formats": { "margin": "percent" },
            "labels": { "line": "Product line", "amount": "Revenue, $M", "margin": "Operating margin" }
          }
        ]
      }
    },
    {
      "title": "Each line's share of its quarter",
      "note": "Services grew from 17% of the quarter’s revenue in Q1 to 28% in Q4.",
      "how": "transform.window with op \"total\" writes the sum of each quarter on every row of it, and op \"share\" each row as a part of that sum, without collapsing the rows as aggregate would. The share labels the segments, and the total stands over each stack: it is on every row of its quarter, and a label repeated on one spot is written once.",
      "spec": {
        "data": "revenue",
        "size": [720, 340],
        "transforms": [
          { "type": "window", "field": "amount", "op": "total", "group": "quarter", "as": "quarterTotal" },
          { "type": "window", "field": "amount", "op": "share", "group": "quarter", "as": "share" },
          { "type": "stack", "field": "amount", "by": "quarter", "group": "line" }
        ],
        "scales": {
          "x": { "type": "band" },
          "y": { "type": "linear", "zero": true },
          "color": { "type": "color" }
        },
        "guides": [
          { "type": "grid", "scale": "y" },
          { "type": "axis", "scale": "x" },
          { "type": "axis", "scale": "y" },
          { "type": "legend", "scale": "color", "position": "auto" }
        ],
        "marks": [
          { "type": "rect", "x": "quarter", "y": "amount1", "y2": "amount0", "color": "line" },
          { "type": "text", "x": "quarter", "y": "amount1", "y2": "amount0", "text": "share", "format": "percent", "on": "line" },
          { "type": "text", "x": "quarter", "y": "quarterTotal", "text": "quarterTotal", "baseline": "auto", "dy": -6, "weight": 600 }
        ],
        "interaction": [
          {
            "type": "tooltip",
            "fields": ["line", "amount", "share", "quarterTotal"],
            "formats": { "share": { "style": "percent", "maximumFractionDigits": 1 } },
            "labels": { "line": "Product line", "amount": "Revenue, $M", "share": "Share of the quarter", "quarterTotal": "Quarter, $M" }
          }
        ]
      }
    }
  ]
}

The report the composer makes from it is one standalone file (102.7 KB): open it from disk, mail it, print it.

In a page of your own

The charts above use the full kit (81.8 KB gzipped), the smallest one that has the blocks this example needs: lib.curve, lib.pointer, lib.ticks, scale.band, scale.color, scale.linear, transform.stack, transform.window, mark.line, mark.point, mark.rect, mark.text, guide.axis, guide.grid, 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/full.js"></script>

<script type="application/json" id="bc-data">
{"revenue":[{"quarter":"Q1","line":"Hardware","amount":30,"margin":0.21},{"quarter":"Q1","line":"Software","amount":20,"margin":0.21},{"quarter":"Q1","line":"Services","amount":10,"margin":0.21},{"quarter":"Q2","line":"Hardware","amount":35,"margin":0.24},{"quarter":"Q2","line":"Software","amount":25,"margin":0.24},{"quarter":"Q2","line":"Services","amount":15,"margin":0.24},{"quarter":"Q3","line":"Hardware","amount":28,"margin":0.19},{"quarter":"Q3","line":"Software","amount":30,"margin":0.19},{"quarter":"Q3","line":"Services","amount":22,"margin":0.19},{"quarter":"Q4","line":"Hardware","amount":40,"margin":0.27},{"quarter":"Q4","line":"Software","amount":28,"margin":0.27},{"quarter":"Q4","line":"Services","amount":26,"margin":0.27}]}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "revenue",
  "size": [
    720,
    340
  ],
  "transforms": [
    {
      "type": "stack",
      "field": "amount",
      "by": "quarter",
      "group": "line"
    }
  ],
  "scales": {
    "x": {
      "type": "band"
    },
    "y": {
      "type": "linear",
      "zero": true
    },
    "y2": {
      "type": "linear",
      "zero": true,
      "range": "height"
    },
    "color": {
      "type": "color"
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x"
    },
    {
      "type": "axis",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "y2",
      "position": "right",
      "format": "percent"
    },
    {
      "type": "legend",
      "scale": "color",
      "position": "auto"
    }
  ],
  "marks": [
    {
      "type": "rect",
      "x": "quarter",
      "y": "amount1",
      "y2": "amount0",
      "color": "line"
    },
    {
      "type": "line",
      "x": "quarter",
      "y": {
        "field": "margin",
        "scale": "y2"
      },
      "stroke": "var(--bc-text, #1e293b)"
    },
    {
      "type": "point",
      "x": "quarter",
      "y": {
        "field": "margin",
        "scale": "y2"
      },
      "r": 4,
      "fill": "var(--bc-text, #1e293b)"
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "fields": [
        "line",
        "amount",
        "margin"
      ],
      "formats": {
        "margin": "percent"
      },
      "labels": {
        "line": "Product line",
        "amount": "Revenue, $M",
        "margin": "Operating margin"
      }
    }
  ],
  "alt": "Revenue by product line, $M (bars) and operating margin (line, right)"
}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "revenue",
  "size": [
    720,
    340
  ],
  "transforms": [
    {
      "type": "window",
      "field": "amount",
      "op": "total",
      "group": "quarter",
      "as": "quarterTotal"
    },
    {
      "type": "window",
      "field": "amount",
      "op": "share",
      "group": "quarter",
      "as": "share"
    },
    {
      "type": "stack",
      "field": "amount",
      "by": "quarter",
      "group": "line"
    }
  ],
  "scales": {
    "x": {
      "type": "band"
    },
    "y": {
      "type": "linear",
      "zero": true
    },
    "color": {
      "type": "color"
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x"
    },
    {
      "type": "axis",
      "scale": "y"
    },
    {
      "type": "legend",
      "scale": "color",
      "position": "auto"
    }
  ],
  "marks": [
    {
      "type": "rect",
      "x": "quarter",
      "y": "amount1",
      "y2": "amount0",
      "color": "line"
    },
    {
      "type": "text",
      "x": "quarter",
      "y": "amount1",
      "y2": "amount0",
      "text": "share",
      "format": "percent",
      "on": "line"
    },
    {
      "type": "text",
      "x": "quarter",
      "y": "quarterTotal",
      "text": "quarterTotal",
      "baseline": "auto",
      "dy": -6,
      "weight": 600
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "fields": [
        "line",
        "amount",
        "share",
        "quarterTotal"
      ],
      "formats": {
        "share": {
          "style": "percent",
          "maximumFractionDigits": 1
        }
      },
      "labels": {
        "line": "Product line",
        "amount": "Revenue, $M",
        "share": "Share of the quarter",
        "quarterTotal": "Quarter, $M"
      }
    }
  ],
  "alt": "Each line's share of its quarter"
}
</script>