demo D182
Correlation and weighted averages in React
Two-column figures from the API, over the filtered rows
grid.statistics.correlation(a, b)
This grid computes two-column figures, correlation and weighted averages, from the API over the filtered rows. It answers how two measures relate across the current selection, updating as the filter changes.
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 createLatticeGrid from 'https://cdn.latticegrid.dev/lattice-grid@1.107.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', format: { decimals: 4 } },
{ field: 'bid', title: 'Bid', type: 'number', format: { decimals: 4 } },
{ field: 'volume', title: 'Volume', type: 'number', format: { notation: 'compact' } },
];
const rows = [/* trading book records */];
function App() {
const gridRef = React.useRef(null);
const [out, setOut] = React.useState('');
// Two-column figures read straight from the live grid through the ref:
// correlation and a regression between two prices, and a volume-weighted
// price. Every figure is over the filtered rows, so filter a desk to see
// them move.
const report = () => {
const grid = gridRef.current && gridRef.current.grid;
if (!grid) return;
const s = grid.statistics;
setOut(JSON.stringify({
correlation: s.correlation('price', 'bid'),
regression: s.regression('price', 'bid'),
weighted: s.weightedAverage('price', 'volume'),
}, null, 2));
};
React.useEffect(report, []);
return (
<>
<LatticeGrid
ref={gridRef}
rowKey="id"
toolPanel={{ side: 'left', panels: ['filters', 'columns'], exportName: 'book' }}
columnDefaults={{ filter: true }}
columns={columns}
rows={rows}
onFilterChanged={report}
style={{ height: '460px' }}
/>
<pre style={{ marginTop: '12px' }}>{out}</pre>
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>