demo D171
Click a chart to filter in React
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.
This is the React version. The grid mounts through the createLatticeGrid adapter, which takes its configuration as ordinary props and hands back the live grid through a ref. The grid below is the same one every other tab runs.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<div id="app"></div>
<script type="module">
import React from 'https://esm.sh/react@18';
import { createRoot } from 'https://esm.sh/react-dom@18/client';
import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
import { createLatticeReact } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/react.esm.min.js';
import { createChart } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/charts.esm.min.js';
const { LatticeGrid, LatticeChart } = createLatticeReact({ React, createGrid, createChart });
const 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' },
];
const rows = [/* circuit records */];
function App() {
const [grid, setGrid] = React.useState(null);
const activeRef = React.useRef(null);
// 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.
const onClick = ({ category }) => {
const value = category;
if (value == null) return;
if (activeRef.current === value) {
grid.filters?.clear?.();
activeRef.current = null;
} else {
grid.filters?.set?.({ col: 'status', op: 'eq', value });
activeRef.current = value;
}
};
return (
<>
{grid && (
<LatticeChart grid={grid} type="pie" x="status" y="charge" onClick={onClick} title="Charge by status (click to filter)" style={{ height: '260px' }} />
)}
<LatticeGrid
rowKey="id"
toolPanel={{ side: 'left', panels: ['filters', 'columns'], exportName: 'circuits' }}
columns={columns}
rows={rows}
onGridReady={setGrid}
style={{ height: '320px', marginTop: '14px' }}
/>
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>