blockcharts alpha

Embedding

Put charts into an app or a page you already have. Every chart here is live, drawn by the released blocks, and the code beside it is what draws it.

The same chart, wherever it goes

In an app, a ready component; in a page of your own, one script tag. The chart is the same JSON either way. Click a bar: its row is what your code gets.

Live

Click a bar.

A ready component from blockcharts/react: a spec, your rows, and onClick with the clicked row. On a server it renders an empty element, and the chart is drawn in the browser.

import 'blockcharts/kits/basic.mjs'; // once, before a chart mounts
import { useRef } from 'react';
import { BlockChart } from 'blockcharts/react';

const spec = {
  scales: { x: { type: 'band' }, y: { type: 'linear', zero: true } },
  guides: [
    { type: 'axis', scale: 'x' },
    { type: 'axis', scale: 'y', format: 'compact' },
  ],
  marks: [{ type: 'rect', x: 'region', y: 'total' }],
  interaction: [{ type: 'tooltip' }],
};

export function Regions({ rows, onPick }) {
  const chart = useRef(null); // chart.current.setView('x', null)
  return (
    <BlockChart
      ref={chart}
      spec={spec}
      data={rows}
      onClick={(d) => onPick(d.values.region)}
    />
  );
}
React, Vue, Svelte and <block-chart>

What else it does

A live piece of each demo. Open one for the whole of it, and the code that runs it.

Control a chart from your code

The handle BC.chart returns moves the visible window and swaps the spec, keeping the zoom. Try the buttons:

const chart = BC.chart(element, spec);
chart.setView('x', [march1, march31]);
chart.update(nextSpec); // keeps the zoom
The demo and its code

Clicks on the data

A click on a bar sends bc-click with its row. Here it picks the region the line below shows:

host.addEventListener('bc-click', (e) => {
  const row = e.detail.values;
  showRegion(row.region);
});
The demo and its code

Colors and themes

Every color is a CSS variable, on SVG and on canvas alike. Pick a theme: only a class on the box changes.

.warm {
  --bc-c0: #c1440e; /* first color */
  --bc-grid: #f0e2d0;
  --bc-text: #5b3a1e;
}
The demo and its code

Your own block

A block is a small script calling BC.define, as the built-in ones are. This dashed target line is twenty lines in the page:

BC.define({
  role: 'guide',
  type: 'target',
  version: 1,
  params: { value: { kind: 'number' } },
  render(spec, ctx) { /* shapes */ },
});
The demo and its code

Ways to hand over data

Rows, columns, gzip or binary columns: the same 2000 points draw the same picture. What each form costs the page:

{ "columns": {
    "t": [0, 1, 2],
    "v": [50.2, 50.9, 51.4]
} }
The demo and its code

Many points: the canvas renderer

21,000 points on one canvas instead of an element each, with zoom (Ctrl and the wheel) and a tooltip as on SVG:

{
  "data": "points",
  "renderer": "canvas",
  "marks": [{ "type": "point", ... }]
}
The demo and its code

Or make a report file

For a document with charts in it, skip all of this: write a page.json, and the composer checks it against the data and writes one HTML file that opens anywhere. Or ask Claude, with the skill.