Lattice Grid Buy a licence

demo D183

Statistical values in a column in Svelte

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.

Building…
Loading a live grid…

This is the Svelte version. A use action attaches the grid to a plain element, so no framework runtime is passed in and the component stays small, while the configuration is ordinary Svelte state.

The configuration

<script>
  import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
  import createLatticeAction from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/svelte.esm.min.js';

  const lattice = createLatticeAction({ createGrid });

  // Read the column's shape once over the data, then feed those figures into
  // computed columns. "vs median" and a z-score become columns like any other:
  // sortable, filterable, exportable.
  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 config = {
    rowKey: 'id',
    columns: [
      { field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
      { field: 'price', title: 'Price', type: 'number', format: { decimals: 4 } },
      // pure lets the grid cache the result; a compute reads only its deps.
      { id: 'vsMedian', title: 'vs median', type: 'number', format: { decimals: 4, signed: true },
        value: { deps: ['price'], pure: true, compute: (d) => d.price - median } },
      { id: 'z', title: 'Z-score', type: 'number', format: { decimals: 2, signed: true },
        value: { deps: ['price'], pure: true, compute: (d) => (d.price - mean) / sd } },
      { id: 'p90', title: 'over p90', type: 'boolean',
        value: { deps: ['price'], pure: true, compute: (d) => d.price > p90 } },
    ],
    rows,  // trading book: symbol, price
  };
</script>

<svelte:head>
  <link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
</svelte:head>

<div use:lattice={config} style="height: 460px"></div>