Lattice Grid

blog

Monthly Revenue Trend Grid That Feeds a Line Chart

Building…
Loading a live grid…

“How is the North doing this year?” The sales manager filters the orders table to North and looks at the revenue line above it, and the line hasn’t moved: it was bucketed by month once, when the page loaded, from every order. So the trend is exported to a spreadsheet, bucketed again by hand and pasted into a deck, and next Monday the same question starts the same afternoon over. Grouping by month in JavaScript, inside the grid the manager is already filtering, ends that.

A derived grid removes the second bucketing routine instead of fixing it. Build the orders grid the way you already do, then build a second grid whose source reads the first: round each order’s date down to its month, group on that, sum the revenue and count the orders. Point a line chart at the derived grid rather than at a dataset of its own, and the chart reads whatever the derived grid holds on every frame.

source: {
  mode: 'derived',
  from: orders,
  groupBy: 'date',
  bucket: { of: 'date', by: 'month' },
  select: {
    revenue: { of: 'amount', fn: 'sum' },
    orders: { fn: 'count' },
  },
}

Filter the orders grid once, to a region, a customer or a date range, and the monthly grid re-derives on the next frame, because it was never a second query to begin with. The chart below it moves the same way, because it reads the grid rather than a snapshot taken when it was drawn.

For you that means the trend chart stops being a second thing to maintain alongside the table. There is no bucketing loop to re-run on a filter event, no redraw to wire up by hand, and no moment where the chart and the table disagree, because the chart is reading rows the table’s own filter already shaped. One filter moves the table, the monthly grid and the chart together, every time.

Plain JavaScript

Twenty orders across five months, the table that holds them, a derived grid that buckets them by month, and a line chart reading the derived grid:

import { createGrid } from '@toclocoinc/lattice-grid';
import { createChart } from '@toclocoinc/lattice-grid/modules/charts';

const orders = [
  { id: 1,  date: '2026-01-04', amount: 182.50 },
  { id: 2,  date: '2026-01-11', amount: 94.00 },
  { id: 3,  date: '2026-01-19', amount: 310.25 },
  { id: 4,  date: '2026-01-27', amount: 58.75 },
  { id: 5,  date: '2026-02-02', amount: 215.00 },
  { id: 6,  date: '2026-02-09', amount: 140.40 },
  { id: 7,  date: '2026-02-17', amount: 76.10 },
  { id: 8,  date: '2026-02-23', amount: 190.00 },
  { id: 9,  date: '2026-02-28', amount: 332.90 },
  { id: 10, date: '2026-03-03', amount: 121.00 },
  { id: 11, date: '2026-03-08', amount: 268.50 },
  { id: 12, date: '2026-03-15', amount: 99.99 },
  { id: 13, date: '2026-03-22', amount: 410.00 },
  { id: 14, date: '2026-04-01', amount: 175.25 },
  { id: 15, date: '2026-04-06', amount: 230.00 },
  { id: 16, date: '2026-04-14', amount: 88.60 },
  { id: 17, date: '2026-04-20', amount: 305.75 },
  { id: 18, date: '2026-04-27', amount: 119.00 },
  { id: 19, date: '2026-04-29', amount: 247.30 },
  { id: 20, date: '2026-05-02', amount: 60.00 },
];

const orderColumns = [
  { field: 'id', title: 'Order' },
  { field: 'date', title: 'Date', type: 'dateString' },
  { field: 'amount', title: 'Amount', type: 'number', total: 'sum' },
];

// The table a reader already knows how to build.
const ordersGrid = createGrid(document.querySelector('#orders'), {
  rowKey: 'id',
  columns: orderColumns,
  rows: orders,
});

// The trend beside it: one row per month, revenue summed and orders counted.
const byMonth = createGrid(document.querySelector('#by-month'), {
  columns: [
    { field: 'date', title: 'Month', type: 'dateString' },
    { field: 'revenue', title: 'Revenue', type: 'number' },
    { field: 'orders', title: 'Orders', type: 'number' },
  ],
  source: {
    mode: 'derived',
    from: ordersGrid,
    groupBy: 'date',
    bucket: { of: 'date', by: 'month' },
    select: {
      revenue: { of: 'amount', fn: 'sum' },
      orders: { fn: 'count' },
    },
    sort: [{ col: 'date', dir: 'asc' }],
  },
});

// The chart reads byMonth, not a copy of its rows.
const chart = createChart({
  grid: byMonth,
  container: document.querySelector('#trend'),
  type: 'line',
  x: 'date',
  y: 'revenue',
});

Filter ordersGrid to orders of $150 or more, and byMonth drops from five months to four: May disappears entirely because its one order falls under the floor, and the chart redraws on the remaining four points, re-summed, with no second call behind it.

React

The same three pieces through the React adapter. The orders grid hands back its live instance through onGridReady, which is what the monthly grid’s from needs; the chart mounts once the monthly grid’s own onGridReady fires:

import React from 'react';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { createLatticeGrid } from '@toclocoinc/lattice-grid/modules/react';
import { createChart } from '@toclocoinc/lattice-grid/modules/charts';

const LatticeGrid = createLatticeGrid({ React, createGrid });

const orderColumns = [
  { field: 'id', title: 'Order' },
  { field: 'date', title: 'Date', type: 'dateString' },
  { field: 'amount', title: 'Amount', type: 'number', total: 'sum' },
];

const monthColumns = [
  { field: 'date', title: 'Month', type: 'dateString' },
  { field: 'revenue', title: 'Revenue', type: 'number' },
  { field: 'orders', title: 'Orders', type: 'number' },
];

function MonthlyRevenue({ orders }) {
  const [book, setBook] = React.useState(null);
  const [month, setMonth] = React.useState(null);
  const chartHost = React.useRef(null);

  // Mounts the chart once the monthly grid exists, and tears it down with it.
  React.useEffect(() => {
    if (!month || !chartHost.current) return;
    const chart = createChart({
      grid: month,
      container: chartHost.current,
      type: 'line',
      x: 'date',
      y: 'revenue',
    });
    return () => chart.destroy();
  }, [month]);

  return (
    <>
      <LatticeGrid
        rowKey="id"
        columns={orderColumns}
        rows={orders}
        onGridReady={setBook}
        style={{ height: 320 }}
      />
      {book && (
        <LatticeGrid
          columns={monthColumns}
          source={{
            mode: 'derived',
            from: book,
            groupBy: 'date',
            bucket: { of: 'date', by: 'month' },
            select: {
              revenue: { of: 'amount', fn: 'sum' },
              orders: { fn: 'count' },
            },
            sort: [{ col: 'date', dir: 'asc' }],
          }}
          onGridReady={setMonth}
          style={{ height: 160, marginTop: 16 }}
        />
      )}
      <div ref={chartHost} style={{ height: 240, marginTop: 16 }} />
    </>
  );
}

source takes the derived config directly, the same shape as the plain JavaScript example; only the way each piece is mounted, and the ref the chart reaches for, changes.

The options used

Option Type What it does
from Grid Required. The grid to read.
groupBy string | string[] The dimension, or dimensions, to group by. Omit to pass rows through.
bucket { of, by } Round a date column down to the start of its period and group on that. by is day, week, month, quarter or year.
select Record<string, { of, fn }> The reduced columns, by output id. fn is any key of the totals row kernels, so sum and count are as available as median or p95.
sort { col, dir }[] Order the derived rows before any limit is applied.

Group by month in JavaScript: the result

Running the plain JavaScript example above against those twenty orders, with createHeadlessGrid from the published package, and printing byMonth’s rows gives five months:

Month Revenue Orders
2026-01-01 645.5 4
2026-02-01 954.4 5
2026-03-01 899.49 4
2026-04-01 1165.9 6
2026-05-01 60 1

Filter the orders grid to orders of $150 or more and byMonth re-derives to four rows, January at 492.75 across 2 orders, February at 737.9 across 3, March at 678.5 across 2 and April at 958.3 across 4; May is gone, because its one order no longer qualifies. The chart draws exactly those points, because it was never holding a copy to begin with.

Read the derived and chained grids guide for grouping, bucketing and chaining in full, or see the charts module for every chart type a grid, derived or not, can feed.

Read next

All posts RSS