blockcharts alpha

Guide

The ideas in one page: a chart is a JSON spec; blocks do the work; you pick how the code reaches the page.

A chart spec

A spec names a dataset and says how to draw it. There is no code in it.

chart spec

{
  "data": "sales",
  "size": [640, 300],
  "transforms": [ { "type": "aggregate", "groupby": ["region"], "measures": [ { "op": "sum", "field": "total", "as": "sum" } ] } ],
  "scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
  "guides": [
    { "type": "grid", "scale": "y" },
    { "type": "axis", "scale": "x" },
    { "type": "axis", "scale": "y", "title": "Revenue", "format": { "style": "currency", "currency": "USD" } }
  ],
  "marks": [ { "type": "rect", "x": "region", "y": "sum" } ],
  "interaction": [ { "type": "tooltip" } ]
}

JSON Schema: help from the editor

A spec and a page.json have a JSON Schema each, made from the blocks themselves: every block, parameter and channel, the values it takes and its documentation. Name it in the file, and an editor such as VS Code completes keys and block types, explains each on hover, and underlines a mistake as you type. The core and the composer accept the $schema key and ignore it.

page.json

{
  "$schema": "https://cdn.jsdelivr.net/npm/blockcharts@0.0.5/release/schema/page.schema.json",
  "title": "Sales",
  "data": { "sales": [ { "region": "North", "total": 42 } ] },
  "charts": [ { "spec": { "data": "sales", "marks": [ { "type": "rect", "x": "region", "y": "total" } ] } } ]
}

A chart spec on its own takes schema/chart.schema.json. Both are draft-07, also in the npm package (blockcharts/schema/chart.schema.json) for a validator such as Ajv. A schema knows no data: a field the dataset lacks is found by BC.validate and the composer, which read it.

Data

Datasets are named and shared: several charts can read the same one. Rows, columns, CSV (when composing), gzip and binary columns are all accepted; see them side by side. Dates are ISO strings and are read as UTC. A missing value is null, and a row without a value is simply not drawn.

Numbers and dates

Axes, tooltips, text labels and color bars take a format: a preset (percent, compact, integer, year, month, day, date, datetime) or an object of Intl options, with locale, prefix and suffix on top. The default locale is en-US and dates are UTC, so a report reads the same on every machine.

format examples

{ "style": "currency", "currency": "EUR", "maximumFractionDigits": 0 }
{ "month": "short", "year": "2-digit" }
{ "maximumFractionDigits": 1, "suffix": " kg", "locale": "de-DE" }

The composer and the agent skill

The composer turns a page.json into one HTML file. It works out which blocks the charts need, checks every spec against the real data (each mistake comes back with its path in the JSON), draws every chart once in a sandbox to catch what only shows up when drawing, and writes the page. The same input always gives the same bytes, and nothing is written until every chart is valid.

shell

git clone https://github.com/boligolov/blockcharts.git
cd blockcharts
npm install
npm run build && npm run manifest
node scripts/compose.js skills/blockcharts/recipes/report-from-csv.json report.html

npm run skill packs the composer with instructions and recipes for an AI agent. The agent writes only JSON, runs the composer, and fixes what the messages say. Every example is a recipe from that package.

Files, kits and sizes

The npm package carries the files below in its release/ folder (npm install blockcharts), and the jsDelivr CDN serves each of them at its path there: https://cdn.jsdelivr.net/npm/blockcharts@0.0.5/release/kits/basic.js. In a clone of the repository npm run release writes them. Everything is minified.

KitBlocksSizegzipFor
basic20113.7 KB42.9 KBBars, lines, points and areas on linear, log, time and band scales, with axes, grid, legend, tooltip and click events, and fields calculated from the data.
composition34150.2 KB54.9 KBParts of a whole and distributions: stacked bars and areas, pie, heatmap, histogram, boxplot, waterfall, funnel, treemap, aggregation, click events.
dashboard32150.7 KB55.5 KBBusiness dashboards: KPI tiles with sparklines, bars, lines and areas, waterfall, funnel, treemap, bullet and target lines, donut, with axes, grid, legend, tooltip, legend filter and click events.
full59227.9 KB81.8 KBEvery block.
timeseries30142.4 KB52.1 KBLong series over time: lines and areas with zoom, brush, legend filter, decimation, aggregation, wide tables, moving averages and click events; also the canvas renderer for very large series.
FileSizegzip
core.js43.3 KB17.1 KB
loader.js2.0 KB1.1 KB

Besides kits, every block is a file of its own (blocks/mark.line.js) and manifest.json lists them with their dependencies. A kit for one page: node scripts/build-dist.js --page page.json=report. Kits are also ES modules (.mjs); see the module demo. schema/ holds the JSON Schemas, and frameworks/ the wrappers for React, Vue and Svelte and the <block-chart> element, which an app imports as blockcharts/react, blockcharts/vue, blockcharts/svelte and blockcharts/element: the wrappers.

Pin a version, add integrity

Files of a version never change, so name the version in the path (blockcharts@0.0.5 on the CDN, a folder of its own such as /blockcharts/0.0.5/ on your own site) and browsers can keep them for good. manifest.json has a SHA-384 hash for every file, ready for integrity. This is the real hash of the kit on this site:

HTML

<script src="release/kits/basic.js"
        integrity="sha384-UWAsdbC+wtJjpehXXVP1iChPBCHse2kCAACJTRSDM13eCC6RpjfkqgXQkdCNsqcS"
        crossorigin="anonymous"></script>

Limits