Skip to content

Data Grid charts

Charts of Data Grid data, drawn with the AbsoluteUI chart components:

  • sparklineColumn(options): spread into a column; each cell draws a named Sparkline of the row’s values.
  • chartRange(grid, options?): a panel docked below the grid with a bar, stacked bar, line, area, or pie chart of the selected cells.
  • chartPivot(grid, options?): the same panel for pivot results.
import { DataGrid } from '@absoluteui/components/data-grid';
import { chartRange, sparklineColumn } from '@absoluteui/components/data-grid/charts';
interface Region {
id: string;
name: string;
q1: number;
q2: number;
}
const host = document.createElement('div');
host.style.setProperty('block-size', '200px');
document.body.append(host);
const grid = DataGrid.create<Region>(host, {
label: 'Sales',
rows: [{ id: 'n', name: 'North', q1: 120, q2: 150 }, { id: 's', name: 'South', q1: 90, q2: 80 }],
rowId: (r) => r.id,
cellSelection: true,
columns: [
{ id: 'name', header: 'Region', field: 'name' },
{ id: 'q1', header: 'Q1', field: 'q1', type: 'number' },
{ id: 'q2', header: 'Q2', field: 'q2', type: 'number' },
{ id: 'trend', header: 'Trend', ...sparklineColumn<Region>({ values: (r) => [r.q1, r.q2], label: (r) => `${r.name} by quarter` }) },
],
});
grid.addCellRange({ startRow: 'n', endRow: 's', startColumn: 'name', endColumn: 'q2' });
const panel = chartRange(grid, { type: 'line', title: 'Sales' });
console.log(panel.element.isConnected);
  • Sparkline cells copy and export their numbers as text; sorting compares the last value, or sortBy: 'sum' | 'min' | 'max'.
  • The chart panel is a region named “Chart of {title}”; Escape or Close closes it and focus returns to the grid. It follows edits, sorting, and filtering, and each chart has its own data table.
  • Series run by column or by row (seriesBy); text columns label the categories.
  • To offer charts in the cell menu, add an item to contextMenu({ items }) that calls chartRange(grid).

chartPivot

Chart the pivot results in a docked panel: the pivot rows of a group level are the categories, the value columns the series. The chart follows pivot changes, filters, and row updates.

function chartPivot<Row>(grid: DataGrid<Row>, options?: DataGridChartOptions): DataGridChartPanel;

chartRange

Chart the cells of a range in a panel docked below the grid (a region; Escape or Close closes it and focus returns to the grid). The first text column names the categories; numeric columns are the series (or the rows are, with seriesBy: 'rows'). The chart follows edits, transactions, sorting, and filtering; every chart has its own data table. Load the Bar, Line, and Pie Chart styles.

function chartRange<Row>(grid: DataGrid<Row>, options?: DataGridChartOptions): DataGridChartPanel;

sparklineColumn

Column options drawing a Sparkline in each cell (spread them into a column with its id and header). The cell reads its values out (“Sales, last 12 months: 12 values, from 4 to 9, …”); copy, CSV, Excel, and print get the numbers as text; sorting uses one number per row (sortBy). Load the Sparkline styles.

function sparklineColumn<Row>(options: SparklineColumnOptions<Row>): Pick<DataGridColumn<Row>, 'accessor' | 'render' | 'format' | 'compare'>;

DataGridChartOptions

interface DataGridChartOptions {
readonly type?: DataGridChartType;
readonly seriesBy?: 'columns' | 'rows';
readonly title?: string;
readonly range?: {
readonly startRow: string;
readonly endRow: string;
readonly startColumn: string;
readonly endColumn: string;
};
readonly level?: number;
}

DataGridChartPanel

interface DataGridChartPanel {
readonly element: HTMLElement;
update(options: Pick<DataGridChartOptions, 'type' | 'seriesBy' | 'title'>): void;
close(): void;
}

SparklineColumnOptions

interface SparklineColumnOptions<Row> {
readonly values: (row: Row) => readonly (number | null)[];
readonly label: string | ((row: Row) => string);
readonly variant?: SparklineVariant;
readonly color?: SparklineColor;
readonly min?: number;
readonly max?: number;
readonly valueFormat?: Intl.NumberFormatOptions;
readonly sortBy?: 'last' | 'sum' | 'min' | 'max';
readonly locale?: string;
}

DataGridChartType

type DataGridChartType = 'bar' | 'stackedBar' | 'line' | 'area' | 'pie';