demo D183
Statistical values in a column in React
A column's median, deviation and percentile, read into computed columns
value.compute
This grid reads a column's median, deviation and percentile into computed columns. It lets a per-row calculation reference a whole-column statistic, so a value can be compared against its column's own figures inline.
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="grid"></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 rows = [/* instrument records, each with a price */];
// A column's median, mean, standard deviation and 90th percentile, computed
// once and read into computed columns that sort and filter. The figures match
// the totals row by the same R type 7 percentile definition.
const prices = rows.map((r) => r.price).sort((a, b) => a - b);
const n = prices.length;
const median = n % 2 ? prices[(n - 1) / 2] : (prices[n / 2 - 1] + prices[n / 2]) / 2;
const mean = prices.reduce((s, x) => s + x, 0) / n;
const sd = Math.sqrt(prices.reduce((s, x) => s + (x - mean) ** 2, 0) / n) || 1;
const p90 = prices[Math.floor(0.9 * (n - 1))];
const columns = [
{ field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
{ field: 'price', title: 'Price', type: 'number', format: { decimals: 4 } },
{ id: 'vsMedian', title: 'vs median', type: 'number', format: { decimals: 4, signed: true },
value: { deps: ['price'], pure: true, compute: (d) => Math.round((d.price - median) * 1e4) / 1e4 } },
{ id: 'z', title: 'Z-score', type: 'number', format: { decimals: 2, signed: true },
value: { deps: ['price'], pure: true, compute: (d) => Math.round(((d.price - mean) / sd) * 100) / 100 } },
{ id: 'p90', title: 'over 90th percentile', type: 'boolean',
value: { deps: ['price'], pure: true, compute: (d) => d.price > p90 } },
];
function App() {
return (
<LatticeGrid
rowKey="id"
columns={columns}
rows={rows}
style={{ height: '540px' }}
/>
);
}
createRoot(document.getElementById('grid')).render(<App />);
</script>