Lattice Grid Buy a licence

demo D183

Statistical values in a column

A column's median, deviation and percentile, read into computed columns

value.compute

Building…
Loading a live grid…

The configuration

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.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.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.0/lattice-grid.esm.min.js';
  import createLatticeGrid from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.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>