demo D206
A fitted line in React
Least squares per series, with R², agreeing with the API
fit: true
This draws a least-squares line through each series in a scatter, with R squared shown and matching the API. It puts a trend and its goodness of fit on the picture, so the strength of a relationship reads directly off the chart.
This is the React version. The grid mounts through the createLatticeGrid adapter, which takes its configuration as ordinary props and hands back the live grid through a ref. The grid below is the same one every other tab runs.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<div id="app"></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.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
import { createLatticeReact } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/react.esm.min.js';
import { createChart } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/charts.esm.min.js';
const { LatticeGrid, LatticeChart } = createLatticeReact({ React, createGrid, createChart });
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: 'volume', title: 'Volume', type: 'number', format: { notation: 'compact' } },
{ field: 'notional', title: 'Notional', type: 'number',
format: { style: 'currency', currency: 'USD' }, total: 'sum' },
];
const rows = [/* trading records, one per instrument */];
function App() {
const [grid, setGrid] = React.useState(null);
// fit draws a least-squares line through the points, per series, with
// its R2 beside it. The fit recomputes over whatever the filters leave.
return (
<>
{grid && <LatticeChart grid={grid} type="scatter" x="volume" y="notional" fit title="Notional against volume, fitted" style={{ height: '340px' }} />}
<LatticeGrid
rowKey="id"
columns={columns}
rows={rows}
onGridReady={setGrid}
style={{ height: '320px', marginTop: '14px' }}
/>
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>