React, Vue, Svelte and <block-chart>
Each wrapper is a subpath of the npm package with no blocks of its own: it draws with the kit your app imports once, so the app carries only the blocks it chose. React, Vue and Svelte are optional peer dependencies; the custom element needs no framework.
Any framework, or none: the custom element
Importing blockcharts/element defines <block-chart>. Give it a spec and the chart's own rows as properties (or the spec as JSON in a spec attribute) and listen for bc-click. Click a bar:
Click a bar.
In an app the two imports are blockcharts/kits/basic.mjs and blockcharts/element. The element has no shadow DOM, so your --bc-* variables and .bc-* styles reach the chart.
HTML
<block-chart id="regions"></block-chart>
<pre class="out" id="picked">Click a bar.</pre>
<script type="module">
import '/release/kits/basic.mjs';
import '/release/frameworks/element.mjs';
const chart = document.getElementById('regions');
chart.spec = {
"size": [720, 260],
"scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x" },
{
"type": "axis",
"scale": "y",
"format": { "style": "currency", "currency": "USD", "maximumFractionDigits": 0 }
}
],
"marks": [
{ "type": "rect", "x": "region", "y": "total", "radius": 3 },
{
"type": "text",
"x": "region",
"y": "total",
"text": "total",
"format": "compact",
"baseline": "auto",
"dy": -5
}
],
"interaction": [
{
"type": "tooltip",
"formats": {
"total": { "style": "currency", "currency": "USD", "maximumFractionDigits": 0 }
}
},
{ "type": "click" }
]
};
chart.data = [{"region":"North","total":42000},{"region":"South","total":58000},{"region":"East","total":31000},{"region":"West","total":66000}];
chart.addEventListener('bc-click', (e) => {
document.getElementById('picked').textContent = JSON.stringify(e.detail.values);
});
</script> React
onClick gets the clicked row and adds {"type": "click"} to the spec when it has none. The ref has setView, getView and chart, the chart as drawn now. React 18 and later; on a server the component renders an empty <div>.
React
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)}
/>
);
} Vue 3
@click is a click on the data, with the row. A reactive spec may be changed in place: the chart follows it. Vue 3.3 and later.
Vue
<script setup>
import 'blockcharts/kits/basic.mjs';
import { BlockChart } from 'blockcharts/vue';
defineProps(['rows']);
const emit = defineEmits(['pick']);
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' }],
};
</script>
<template>
<BlockChart
:spec="spec"
:data="rows"
@click="(d) => emit('pick', d.values.region)"
/>
</template> Svelte
An action, plain JavaScript: nothing has to compile it, and it works in Svelte 4 and 5.
Svelte
<script>
import 'blockcharts/kits/basic.mjs';
import { blockchart } from 'blockcharts/svelte';
let { rows, onpick } = $props();
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' }],
};
const onClick = (d) => onpick(d.values.region);
</script>
<div use:blockchart={{ spec, data: rows, onClick }}></div> Angular
Angular sets properties on custom elements and listens to their events, so <block-chart> is the component; CUSTOM_ELEMENTS_SCHEMA lets the template use it.
TypeScript
import 'blockcharts/kits/basic.mjs';
import 'blockcharts/element';
import { Component, CUSTOM_ELEMENTS_SCHEMA, input, output } from '@angular/core';
@Component({
selector: 'app-regions',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<block-chart [spec]="spec" [data]="rows()"
(bc-click)="pick.emit($event.detail.values.region)"></block-chart>
`,
})
export class RegionsComponent {
rows = input.required<object[]>();
pick = output<string>();
spec = {
scales: { x: { type: 'band' }, y: { type: 'linear', zero: true } },
marks: [{ type: 'rect', x: 'region', y: 'total' }],
interaction: [{ type: 'click' }],
};
} What every wrapper does
The chart is drawn again only when the spec changes in content or data is a new array: rendering the same spec again costs nothing, and rows changed in place are not seen, so give a new array. The rows are kept in the data lake under a name of the chart's own and taken away with it; without data, the spec's data names a dataset given to BC.data, which several charts can share. A page with no kit gets a message in the chart's place, not an exception. Types come with every wrapper.