Your own block
A block is a small classic script that registers itself with BC.define. The built-in ones are made exactly this way, so a block of your own is a first-class part of the chart: the spec names it by type, and only the pages that use it carry it.
A target line, in twenty lines
A new guide, guide.target, written in the page after the kit. The spec uses it like any other guide: {"type": "target", "scale": "y", "value": 50000}. Its parameters are declared, so a missing value or a misspelt name is reported the same way as for built-in blocks.
HTML
<script src="/release/kits/basic.js"></script>
<script>
// A new block: a dashed target line across the plot, at a value of a scale.
BC.define({
role: 'guide', type: 'target', version: 1,
params: {
scale: { kind: 'scale', required: true },
value: { kind: 'number', required: true },
label: { kind: 'string' },
},
render(spec, ctx) {
const y = ctx.scales[spec.scale](spec.value);
const { x, w } = ctx.plot;
const color = 'var(--bc-c2, #e15759)';
return {
overlay: [
{ type: 'path', d: `M${x} ${y}H${x + w}`, cls: 'bc-target', style: { fill: 'none', stroke: color, strokeWidth: 1.5, strokeDash: [5, 4] } },
{ type: 'text', x: x + w, y: y - 6, text: spec.label || String(spec.value), anchor: 'end', size: 11, weight: 600, style: { fill: 'var(--bc-text, #444)' } },
],
};
},
});
</script>
<script type="application/json" id="bc-data">
{"sales":[{"region":"North","total":42000},{"region":"South","total":58000},{"region":"East","total":31000},{"region":"West","total":66000}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "sales",
"size": [640, 260],
"scales": {
"x": { "type": "band" },
"y": { "type": "linear", "zero": true, "domain": [0, 80000] }
},
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x" },
{ "type": "axis", "scale": "y", "format": "compact" },
{ "type": "target", "scale": "y", "value": 50000, "label": "Target 50K" }
],
"marks": [{ "type": "rect", "x": "region", "y": "total", "radius": 2 }],
"interaction": [{ "type": "tooltip" }]
}
</script> What a block can be
Six roles, one contract (src/bc.d.ts): a scale maps values, a transform reshapes rows, a mark turns rows into shapes, a guide explains a scale, an interaction listens to the reader, a renderer draws. Marks and guides return plain shapes (path, rect, circle, text, group) with CSS-variable colors, so a block of your own follows themes and dark mode, and works on the canvas renderer too. A scale says what it makes of a value and what it takes (output: "position", input: "number"), and each channel of a mark accepts scales by these, never by name: a scale of your own is drawn by every mark, axis, legend and zoom that takes what it declares. Give it requires and the composer and the loader bring its dependencies along. Defining the same block twice is harmless.