demo D199
Statistics of a selection in Web Component
Drag a rectangle; it is summarised as one set of figures
grid.selection.statistics()
This grid summarises a dragged rectangle of cells as one set of figures. It answers 'what do these add up to' for an ad-hoc block, so a reader checks a subtotal by selecting it, the way a spreadsheet status bar does.
This is the web component version. A lattice-grid custom element renders the grid with no build step, its configuration set as a property, so it works on a page that has no bundler at all.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<script type="module">
import { defineLatticeGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/webcomponent.esm.min.js';
defineLatticeGrid();
</script>
<lattice-grid row-key="id" style="display: block; height: 440px"></lattice-grid>
<div id="out" style="margin-top: 12px; font-family: monospace">Select a block of numeric cells</div>
<script type="module">
const el = document.querySelector('lattice-grid');
el.config = {
selection: { mode: 'multiple', ranges: true },
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' },
],
};
el.rows = rows;
// el.grid is the live grid inside the element. Drag a rectangular block of
// numeric cells and the count, median, quartiles and spread come off that
// range as it changes.
const grid = el.grid;
const out = document.getElementById('out');
grid.on('range:changed', () => {
const s = grid.selection.statistics();
out.textContent = s
? s.cells + ' cells · median ' + s.median + ' · q1 ' + s.q1 + ' · q3 ' + s.q3 + ' · sd ' + s.stddev
: 'Select a block of numeric cells';
});
</script>