Adaptable

#Pivot Views

A pivot view summarizes your data: it turns the distinct values of one or more columns into column groups, and fills them with aggregated values — sums, averages, counts — for each group of rows.

A view is a pivot view when it has a pivotColumns array. Pivot views don't use the table column model (columnOrder, columnOverrides, groupColumns); instead they are built from three lists:

  • pivotColumns — the columns to pivot by. Each distinct value becomes a column group in the header.
  • pivotAggregationColumns — the aggregated values shown in every column group. Without them, a pivot view has nothing to show.
  • pivotGroupColumns — the row grouping, like group columns in a table view.
const pivotView: AdaptablePivotView = {
  id: 'revenue-by-quarter',
  label: 'Revenue by quarter',
  pivotColumns: [{ columnId: 'quarter' }],
  pivotAggregationColumns: [{ columnId: 'revenue', aggregation: 'sum' }],
  pivotGroupColumns: [{ id: 'region-group', groupBy: 'region' }],
};

All three reference columns by id — columns from globalEntities.availableColumns or the view's own privateEntities.availableColumns (see Views).

#Pivot columns

Each entry in pivotColumns has a columnId — the column whose values become column groups. The generated columns have no stable ids of their own (there is one per value in the data), which is why pivot columns use columnId instead of id.

With several pivot columns, the column groups nest in array order: each value of the first column contains a group for each value of the second, and so on. Columns with pivotable: false can't be picked as pivot columns in the UI.

#
A pivot view

Revenue is summed for each quarter (the pivot column) and each region (the group column). Switch to the Raw data view to see the rows being aggregated.

open
loading demo…

pivotColumns is required, but it can be empty. With no pivot columns there are no column groups: the view shows its group columns plus one column per aggregation — a compact grouped summary.

#
A pivot view without pivot columns

pivotColumns is empty, so each region gets one row with its total units, total revenue and average revenue.

open
loading demo…

#Aggregation columns

Each entry in pivotAggregationColumns has:

  • aggregation (required) — the aggregation function;
  • columnId (required) — the column to aggregate;
  • label — the header of the generated columns. Defaults to the column label followed by the aggregation name, like Revenue (Sum).

The aggregations are the same as in table views — the built-in ones and any custom aggregation types. You can aggregate the same column more than once — say, its sum and its average:

pivotAggregationColumns: [
  { columnId: 'revenue', aggregation: 'sum', label: 'Revenue' },
  { columnId: 'revenue', aggregation: 'avg', label: 'Avg revenue' },
],

Every column group contains all the aggregation columns, in array order: with pivotColumns: [{ columnId: 'quarter' }] and the two aggregations above, each quarter gets a Revenue and an Avg revenue column.

#Group columns

pivotGroupColumns is an array of group columns, each with:

  • field — binds the group column to a data field, so nested group rows show that field's value. It's ignored on the last group column, since pivot views have no leaf rows;
  • groupBy (required) — the column id to group by, or an array of ids;
  • id (required) — the id of the group column;
  • label — the header;
  • checkboxSelection — render a selection checkbox in the group column, to select group rows with 'multi-row' selection (see Selection checkboxes).

As in table views, you either use one group column per field, or a single group column that groups by several fields:

One group column per field
pivotGroupColumns: [
  { id: 'region-group', groupBy: 'region', label: 'Region' },
  { id: 'product-group', groupBy: 'product', label: 'Product' },
],
One group column for both fields
pivotGroupColumns: [
  { id: 'group', groupBy: ['region', 'product'], label: 'Region / Product' },
],

#Column totals

showTotalColumns controls the total columns that summarize each column group:

  • 'end' (default) — show them after the pivot columns;
  • 'start' — show them before;
  • false — hide them. When pivotColumns is empty, the aggregation columns are the only value columns, so they are shown regardless.
#
Two pivot columns with totals first

Units and revenue are pivoted by product, then quarter, and grouped by region. showTotalColumns: 'start' places the totals before the pivoted columns.

open
loading demo…

#Sorting

Pivot views sort with columnSorting and singleColumnSort, just like table views.

#Editing pivot views in the UI

When a pivot view is active, the settings panel shows Pivot columns (dimensions and totals), Sorting, Row groups, Aggregations and Selection & navigation pages.

The pivot editors are also available as <adaptable-view-pivot-columns-editor> and the compact <adaptable-pivot-editor>, and users create new pivot views from the Views page (see Managing views in the UI).

For custom UIs, use the inject functions (each returns a signal of an object; call them in a component rendered inside <adaptable-provider>):

  • injectPivotColumns() — pivotColumns, setPivotColumns, addPivotColumn, removePivotColumn, showTotalColumns and setShowTotalColumns;
  • injectPivotGrouping() — the pivot group columns, with the same operations as injectColumnGrouping() in table views;
  • injectColumnAggregations() — reads and sets pivotAggregationColumns when the current view is a pivot view.