#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.
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.
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.
pivotColumns is empty, so each region gets one row with its total units, total
revenue and average revenue.
#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, likeRevenue (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:
pivotGroupColumns: [
{ id: 'region-group', groupBy: 'region', label: 'Region' },
{ id: 'product-group', groupBy: 'product', label: 'Product' },
],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. WhenpivotColumnsis empty, the aggregation columns are the only value columns, so they are shown regardless.
Units and revenue are pivoted by product, then quarter, and grouped by region.
showTotalColumns: 'start' places the totals before the pivoted columns.
#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.UI.ViewPivotColumnsEditor
and the compact Adaptable.UI.PivotEditor, and users create new pivot views
from the Views page (see Managing views in the UI).
For custom UIs, use the hooks:
usePivotColumns—pivotColumns,setPivotColumns,addPivotColumn,removePivotColumn,showTotalColumnsandsetShowTotalColumns;usePivotGrouping— the pivot group columns, with the same operations asuseColumnGroupingin table views;useColumnAggregations— reads and setspivotAggregationColumnswhen the current view is a pivot view.