demo D172
Chart events in React
Click, hover and legend, wired to your app
chart.on('click' | 'hover' | 'legend')
This wires a chart's click, hover and legend interactions to your own application code. It lets a chart drive whatever your app needs when a reader interacts with it, beyond the grid, from a small set of events.
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 */];
// Every chart event hands you one flat payload. Read the parts you want off
// it: category and value on a click, category on a hover, label and hidden
// on a legend toggle.
function App() {
const [grid, setGrid] = React.useState(null);
return (
<>
{grid && (
<LatticeChart
grid={grid} type="pie" x="status" y="charge" title="Charge by status"
onClick={(e) => console.log('click', e.category, e.value)}
onHover={(e) => console.log('hover', e.category)}
onLegend={(e) => console.log('legend', e.label, e.hidden)}
style={{ height: '260px' }}
/>
)}
<LatticeGrid
rowKey="id"
columns={columns}
rows={rows}
onGridReady={setGrid}
style={{ height: '340px', marginTop: '14px' }}
/>
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>