demo D182
Correlation and weighted averages in Angular
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 Angular version. A standalone component takes the whole configuration through one config input, surfaces grid events as outputs, and exposes the live grid on a getter for anything the inputs do not cover.
The configuration
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { LatticeGridComponent, provideLattice } from '@toclocoinc/lattice-grid/angular';
const 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' } },
];
@Component({
selector: 'app-root',
standalone: true,
imports: [LatticeGridComponent],
template:
'<lattice-grid [config]="grid" (filter-changed)="report()" style="display:block;height:460px"></lattice-grid>' +
'<pre style="margin-top:12px">{{ out }}</pre>',
})
export class AppComponent implements AfterViewInit {
@ViewChild(LatticeGridComponent) gridRef!: LatticeGridComponent;
out = '';
grid = {
rowKey: 'id',
toolPanel: { side: 'left', panels: ['filters', 'columns'], exportName: 'book' },
columnDefaults: { filter: true },
columns,
rows: [/* trading book records */],
};
// Two-column figures read straight from the live grid: correlation and a
// regression between two prices, and a volume-weighted price. Every figure is
// over the filtered rows, so filter a desk to see them move.
report() {
const grid = this.gridRef && this.gridRef.grid;
if (!grid) return;
const s = grid.statistics;
this.out = JSON.stringify({
correlation: s.correlation('price', 'bid'),
regression: s.regression('price', 'bid'),
weighted: s.weightedAverage('price', 'volume'),
}, null, 2);
}
ngAfterViewInit() { this.report(); }
}
bootstrapApplication(AppComponent, {
providers: [provideLattice({ createGrid })],
});