demo D171
Click a chart to filter in Svelte
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 Svelte version. A use action attaches the grid to a plain element, so no framework runtime is passed in and the component stays small, while the configuration is ordinary Svelte state.
The configuration
<script>
import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
import createLatticeAction from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/svelte.esm.min.js';
import { createChart } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/charts.esm.min.js';
const lattice = createLatticeAction({ createGrid });
let chartEl;
const config = {
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, // an array of circuit records
// onGrid hands over the live grid the moment it is created. Click a slice
// and the grid narrows to it; click the same slice again to clear.
onGrid: (grid) => {
const chart = createChart({ grid, container: chartEl, type: 'pie', x: 'status', y: 'charge' });
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>
<svelte:head>
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
</svelte:head>
<div bind:this={chartEl} style="height: 260px"></div>
<div use:lattice={config} style="height: 340px; margin-top: 14px"></div>