demo D193
Statistics without a panel in React
Every figure available to your own interface, as an object
grid.statistics.*
This grid exposes every figure it computes as a plain object, for your own interface with no panel involved. It lets you take the statistics into a custom dashboard or export, so the numbers are available wherever you need them.
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.108.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.108.0/lattice-grid.esm.min.js';
import createLatticeGrid from 'https://cdn.latticegrid.dev/lattice-grid@1.108.0/modules/react.esm.min.js';
const LatticeGrid = createLatticeGrid({ React, createGrid });
const columns = [
{ field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
{ field: 'desk', title: 'Desk', filter: { type: 'set' } },
{ field: 'price', title: 'Price', type: 'number' },
{ field: 'bid', title: 'Bid', type: 'number' },
{ field: 'volume', title: 'Volume', type: 'number' },
{ field: 'notional', title: 'Notional', type: 'number',
format: { style: 'currency', currency: 'USD' }, total: 'sum' },
];
const rows = [/* trading book records */];
function App() {
const gridRef = React.useRef(null);
const [out, setOut] = React.useState('');
// Read the statistics API straight from the live grid through the ref. Every
// figure is over the filtered rows, so filter a desk and they all follow.
const report = () => {
const grid = gridRef.current && gridRef.current.grid;
if (!grid) return;
const s = grid.statistics;
setOut(JSON.stringify({
profile: s.profile('notional'),
correlation: s.correlation('price', 'bid'),
regression: s.regression('price', 'bid'),
spearman: s.spearman('price', 'bid'),
}, null, 2));
};
React.useEffect(report, []);
return (
<>
<LatticeGrid
ref={gridRef}
rowKey="id"
toolPanel={{ side: 'left', panels: ['filters', 'columns'] }}
columns={columns}
rows={rows}
onFilterChanged={report}
style={{ height: '460px' }}
/>
<pre style={{ marginTop: '12px' }}>{out}</pre>
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>