demo D182
Correlation and weighted averages in Web Component
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 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: 520px"></lattice-grid>
<pre id="out" style="margin-top: 12px"></pre>
<script type="module">
const el = document.querySelector('lattice-grid');
el.config = {
toolPanel: { side: 'left', panels: ['filters', 'columns'], exportName: 'book' },
columnDefaults: { filter: true },
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' } },
],
};
el.rows = rows; // an array of instruments, one per row
// el.grid is the live grid inside the element. correlation, regression and a
// volume-weighted average, each computed over the filtered rows. Filter a desk
// and every figure moves with it.
const grid = el.grid;
const out = document.getElementById('out');
const report = () => {
out.textContent = JSON.stringify({
correlation: grid.statistics.correlation('price', 'bid'),
regression: grid.statistics.regression('price', 'bid'),
weighted: grid.statistics.weightedAverage('price', 'volume'),
}, null, 2);
};
grid.on('filter:changed', report);
report();
</script>