blockcharts alpha

Gallery / Change and ranking /

Slope chart

How each item changed between two points (before and after, two years).

71.9 KB of code · core 29.5 + 12 blocks 42.4

Net Promoter Score by region, 2024 → 2025

North gained the most, from 31 to 44; South is the only region that fell.

How it is made: A slope chart: mark.line on a band x-scale joins each region's two values, mark.point marks the ends. The order of the domain is the order of the line, so name it when the rows may come in another order. End labels replace the legend. On a band scale the last point stands in the middle of its band, so the outer padding of the band holds the labels: give paddingOuter more for long names, not padding.right.

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, 71.9 KB of code, minified, which is 33% of the 215.9 KB that the core and every block weigh together. The report file is 78.2 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
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
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
TotalThe core 29.5 and 12 blocks 42.471.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/slope.json report.html. Saved as page.json, it composes the same with the skill's own runtime/compose.js.

slope.json

{
  "title": "Slope chart",
  "data": {
    "nps": [
      { "year": "2024", "region": "North", "score": 31 },
      { "year": "2025", "region": "North", "score": 44 },
      { "year": "2024", "region": "South", "score": 42 },
      { "year": "2025", "region": "South", "score": 38 },
      { "year": "2024", "region": "East", "score": 25 },
      { "year": "2025", "region": "East", "score": 29 },
      { "year": "2024", "region": "West", "score": 47 },
      { "year": "2025", "region": "West", "score": 52 }
    ]
  },
  "charts": [
    {
      "title": "Net Promoter Score by region, 2024 → 2025",
      "note": "North gained the most, from 31 to 44; South is the only region that fell.",
      "how": "A slope chart: mark.line on a band x-scale joins each region's two values, mark.point marks the ends. The order of the domain is the order of the line, so name it when the rows may come in another order. End labels replace the legend. On a band scale the last point stands in the middle of its band, so the outer padding of the band holds the labels: give `paddingOuter` more for long names, not `padding.right`.",
      "spec": {
        "data": "nps",
        "size": [480, 340],
        "scales": {
          "x": { "type": "band", "domain": ["2024", "2025"], "paddingOuter": 0.25 },
          "y": { "type": "linear", "zero": true, "nice": true },
          "color": { "type": "color" }
        },
        "guides": [
          { "type": "grid", "scale": "y" },
          { "type": "axis", "scale": "x" },
          { "type": "axis", "scale": "y" }
        ],
        "marks": [
          { "type": "line", "x": "year", "y": "score", "color": "region", "label": "both" },
          { "type": "point", "x": "year", "y": "score", "color": "region", "r": 4 }
        ],
        "interaction": [{ "type": "tooltip" }]
      }
    }
  ]
}

The report the composer makes from it is one standalone file (78.2 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.color, scale.linear, mark.line, mark.point, 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">
{"nps":[{"year":"2024","region":"North","score":31},{"year":"2025","region":"North","score":44},{"year":"2024","region":"South","score":42},{"year":"2025","region":"South","score":38},{"year":"2024","region":"East","score":25},{"year":"2025","region":"East","score":29},{"year":"2024","region":"West","score":47},{"year":"2025","region":"West","score":52}]}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "nps",
  "size": [
    480,
    340
  ],
  "scales": {
    "x": {
      "type": "band",
      "domain": [
        "2024",
        "2025"
      ],
      "paddingOuter": 0.25
    },
    "y": {
      "type": "linear",
      "zero": true,
      "nice": true
    },
    "color": {
      "type": "color"
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x"
    },
    {
      "type": "axis",
      "scale": "y"
    }
  ],
  "marks": [
    {
      "type": "line",
      "x": "year",
      "y": "score",
      "color": "region",
      "label": "both"
    },
    {
      "type": "point",
      "x": "year",
      "y": "score",
      "color": "region",
      "r": 4
    }
  ],
  "interaction": [
    {
      "type": "tooltip"
    }
  ],
  "alt": "Net Promoter Score by region, 2024 → 2025"
}
</script>