demo D279
A trend line and a forecast in React
Draw a least-squares trend straight through the points and carry it on as a dashed forecast, with a vertical event marker flagging the day a change went live
trend: { method: 'linear', forecast: 8 }
This draws a least-squares trend straight through the points and carries it on as a dashed forecast, with a vertical marker flagging the day a change went live. It shows where a series is heading and lets you see whether an event bent the line.
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.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.71.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.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.71.0/lattice-grid.esm.min.js';
import { createLatticeReact } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.71.0/modules/react.esm.min.js';
import { createChart } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.71.0/modules/charts.esm.min.js';
const { LatticeGrid, LatticeChart } = createLatticeReact({ React, createGrid, createChart });
const columns = [
{ field: 'day', title: 'Day', type: 'number' },
{ field: 'active', title: 'Active users', type: 'number', total: 'mean' },
];
const rows = [/* one clean, sorted point per day: day, active */];
function App() {
const [grid, setGrid] = React.useState(null);
// trend fits a least-squares line through the points; forecast: 8 carries
// it eight steps past the last day as a dashed projection. It reads the
// grid's filtered rows, so narrowing the grid refits the trend.
return (
<>
{grid && (
<LatticeChart
grid={grid} type="line" x="day" y="active" legend
trend={{ method: 'linear', forecast: 8 }}
title="The trend, projected ahead"
style={{ height: '300px' }}
/>
)}
<LatticeGrid rowKey="id" columns={columns} rows={rows} onGridReady={setGrid} style={{ height: '320px', marginTop: '14px' }} />
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>