Clicks on the data
With {"type": "click"} among the interactions, a click on a bar, a point, a slice or a cell sends a bc-click event with its row. What happens next is your code: open a page, filter another chart, show the details.
One chart picks what another shows
Click a bar: the line below shows that region by month. The event bubbles, so one listener on the document serves every chart of the page.
Click a bar.
A drag (a pan, a brush) is not a click, a double click sends one, and a click on a legend item stays the legend filter's. Over what can be clicked the pointer is a hand; {"type": "click", "cursor": false} keeps it as it is.
HTML
<script src="/release/kits/basic.js"></script>
<script type="application/json" id="bc-data">
{"totals":[{"region":"North","orders":218},{"region":"South","orders":270},{"region":"East","orders":358},{"region":"West","orders":436}],"orders":[{"month":"2024-01-01","region":"North","orders":38},{"month":"2024-02-01","region":"North","orders":37},{"month":"2024-03-01","region":"North","orders":36},{"month":"2024-04-01","region":"North","orders":32},{"month":"2024-05-01","region":"North","orders":35},{"month":"2024-06-01","region":"North","orders":40},{"month":"2024-01-01","region":"South","orders":44},{"month":"2024-02-01","region":"South","orders":48},{"month":"2024-03-01","region":"South","orders":44},{"month":"2024-04-01","region":"South","orders":42},{"month":"2024-05-01","region":"South","orders":47},{"month":"2024-06-01","region":"South","orders":45},{"month":"2024-01-01","region":"East","orders":57},{"month":"2024-02-01","region":"East","orders":56},{"month":"2024-03-01","region":"East","orders":58},{"month":"2024-04-01","region":"East","orders":62},{"month":"2024-05-01","region":"East","orders":62},{"month":"2024-06-01","region":"East","orders":63},{"month":"2024-01-01","region":"West","orders":72},{"month":"2024-02-01","region":"West","orders":72},{"month":"2024-03-01","region":"West","orders":66},{"month":"2024-04-01","region":"West","orders":73},{"month":"2024-05-01","region":"West","orders":81},{"month":"2024-06-01","region":"West","orders":72}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "totals",
"size": [720, 240],
"scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x" },
{ "type": "axis", "scale": "y", "title": "Orders, first half" }
],
"marks": [{ "type": "rect", "x": "region", "y": "orders", "radius": 3 }],
"interaction": [{ "type": "tooltip" }, { "type": "click" }]
}
</script>
<div id="detail"></div>
<pre class="out" id="clicked">Click a bar.</pre>
<script>
var lake = JSON.parse(document.getElementById('bc-data').textContent);
var spec = {
"data": "picked",
"size": [720, 220],
"scales": { "x": { "type": "time" }, "y": { "type": "linear", "zero": true } },
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x", "format": "month" },
{ "type": "axis", "scale": "y" }
],
"marks": [
{ "type": "line", "x": "month", "y": "orders" },
{
"type": "point",
"x": "month",
"y": "orders",
"fill": "var(--bc-c0, #3a6fe4)"
}
],
"interaction": [{ "type": "tooltip" }]
};
function region(name) {
BC.data('picked', lake.orders.filter(function (r) { return r.region === name; }));
spec.title = name + ', orders by month';
return spec;
}
var detail = BC.chart(document.getElementById('detail'), region('North'));
document.addEventListener('bc-click', function (e) {
var row = e.detail.values; // { region: 'South', orders: 312 }
detail.update(region(row.region));
document.getElementById('clicked').textContent = JSON.stringify(row) + (e.detail.event.shiftKey ? ', with Shift' : '');
});
</script> What the event carries
values is the row with every column, not only the ones the mark reads. row is its index in the chart's table (table, after the transforms) and rows every row a shape stands for (a line, a hexbin). mark and markIndex say which mark of the spec was hit, x and y where it is in the chart, and event is the browser's click, with its modifier keys. What a click hits is what the tooltip shows there. In TypeScript the event is typed by bc.d.ts.
JavaScript
chart.host.addEventListener('bc-click', (e) => {
// e: CustomEvent<BC.ClickDetail>
const { values, row, rows, mark, markIndex, x, y, table, event } = e.detail;
if (event.metaKey || event.ctrlKey) window.open('/regions/' + encodeURIComponent(String(values.region)));
}); Not in a report file
A report made by the composer from a page.json has no script of yours to receive the event, so there the block is a warning. In React, Vue and Svelte the wrappers take an onClick instead: the wrappers.