demo D171
Click a chart to filter: JavaScript Data Grid Demo
Click a slice and the grid narrows to it
chart.on('click', …)
This lets you click a slice or bar in a chart and narrows the grid to it. It turns a chart into a filter control, so a reader drills from the picture into the exact rows behind it with one click.
Loading a live grid…
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.css">
<script src="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.js"></script>
<div id="chart" style="height: 260px"></div>
<div id="grid" style="height: 320px; margin-top: 14px"></div>
<script type="module">
import { createChart } from '@toclocoinc/lattice-grid/modules/charts';
const grid = LatticeGrid.createGrid(document.getElementById('grid'), {
rowKey: 'id',
toolPanel: { side: 'left', panels: ['filters', 'columns'], exportName: 'circuits' },
columns: [
{ field: 'circuit', title: 'Circuit', layout: { width: 180 } },
{ field: 'region', title: 'Region', filter: { type: 'set' } },
{ field: 'status', title: 'Status', filter: { type: 'set' } },
{ field: 'bandwidth', title: 'Bandwidth', type: 'number' },
{ field: 'charge', title: 'Monthly charge', type: 'number', format: { style: 'currency', currency: 'GBP' }, total: 'sum' },
],
rows, // circuit records
});
const chart = createChart({ grid, container: document.getElementById('chart'), type: 'pie', x: 'status', y: 'charge',
title: 'Charge by status (click to filter)' });
// Click a slice to filter the grid to it; click the same slice again to
// clear. The chart reads the grid, so the bars redraw as the filter lands.
let active = null;
chart.on('click', ({ category }) => {
const value = category;
if (value == null) return;
if (active === value) {
grid.filters?.clear?.();
active = null;
} else {
grid.filters?.set?.({ col: 'status', op: 'eq', value });
active = value;
}
});
</script>