# Displaying Column Aggregations

Canonical page: https://www.adaptabletools.com/docs/handbook-aggregation

- Aggregations are supported via Layouts
- AdapTable provides a special Aggregation for Weighted Averages

AG Grid's [Aggregation functionality](https://www.ag-grid.com/javascript-data-grid/aggregation/) displays aggregations of column values.

- Aggregations are displayed by AG Grid in used when [Grouped Rows](https://www.adaptabletools.com/docs/handbook-grouping-rows/index.md)
- By default, Aggregations display `sum` of values, but many different aggregation types are available

AdapTable [Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) provide full support in  for AG Grid Aggregations, including:

- Ability to **define** ָAggregations in [Initial Adaptable State](https://www.adaptabletools.com/docs/technical-reference-initial-state/index.md) - for both [Table Layouts](https://www.adaptabletools.com/docs/handbook-layouts-table/index.md) and [Pivot Layouts](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md)

- Automatic **Persistence** of Aggregations created at run-time into Layout section of [Adaptable State](https://www.adaptabletools.com/docs/dev-guide-adaptable-state/index.md), and then be dynamically re-applied when the Layout next loads

- Two **custom** Aggregations for [Weighted Averages](https://www.adaptabletools.com/docs/handbook-aggregation-weighted-average/index.md) and [Only](https://www.adaptabletools.com/docs/handbook-aggregation-only/index.md)

- Support for [Grand Total Rows](https://www.adaptabletools.com/docs/handbook-aggregation-grand-total-row/index.md) which display the totals for all cells in the Grid (and not a single Group)

- AdapTable additionally provides a very useful and powerful [Row Summary](https://www.adaptabletools.com/docs/handbook-summarising-rows/index.md) feature
- This allows users to see aggregation information even **when there is no Row Grouping**

## Aggregations in Column Header

By default AG Grid prefixes the type of the aggregation function (aka `aggFunc`) to the Column's header.

For instance adding `sum` to a `Price` column will result in a Column header of "(sum) Price".

AdapTable allows this to be overridden through the `SuppressAggFuncInHeader` property in each [Layout](https://www.adaptabletools.com/docs/handbook-layouts/index.md).

This override is **per Layout** in AdapTable and not applied globally

- There is an AG Grid GridOptions property called `suppressAggFuncInHeader` which does the same thing 
- The Layout's `SuppressAggFuncInHeader` property **replaces** this and is only place where this functionality can be set

**Example: Aggregations: Column Headers**

Hiding the aggFunc from the Column Header

 

- This example includes 2 Layouts - one Table (with [Row Grouping](https://www.adaptabletools.com/docs/handbook-grouping-rows/index.md)) and one Pivot - each with 2 `sum` Aggregations - on `Github Watchers` & `Github Stars` Columns
- We have set `SuppressAggFuncInHeader` to *true* so that in both Layouts you see just "Github Stars" instead of "(sum) Github Stars".  

- Click the Custom Toolbar to show / hide AggFuncs in the Column Header (the buttons invoke the [updateCurrentLayout](https://www.adaptabletools.com/docs/handbook-layouts-updating/index.md) function in [Layout API](https://www.adaptabletools.com/docs/technical-reference-layouts/index.md))

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableButton,
  AdaptableOptions,
  ApplicationDataEntry,
  CustomToolbarButtonContext,
  DashboardButtonContext,
  Layout,
} from '@adaptabletools/adaptable';
import {tr} from 'date-fns/locale';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Suppressing Agg Funcs in Header',
  dashboardOptions: {
    customToolbars: [
      {
        name: 'custombuttons',
        toolbarButtons: [
          {
            label: 'Show Agg Funcs',
            buttonStyle: {
              tone: 'neutral',
              variant: 'outlined',
            },
            onClick: (
              button: AdaptableButton<CustomToolbarButtonContext>,
              context: CustomToolbarButtonContext
            ) => {
              context.adaptableApi.layoutApi.updateCurrentLayout(
                (layout: Layout) => {
                  layout.SuppressAggFuncInHeader = false;
                  return layout;
                }
              );
            },
          },
          {
            label: 'Hide Agg Funcs',
            buttonStyle: {
              tone: 'neutral',
              variant: 'outlined',
            },
            onClick: (
              button: AdaptableButton<CustomToolbarButtonContext>,
              context: CustomToolbarButtonContext
            ) => {
              context.adaptableApi.layoutApi.updateCurrentLayout(
                (layout: Layout) => {
                  layout.SuppressAggFuncInHeader = true;
                  return layout;
                }
              );
            },
          },
        ],
      },
    ],
  },
  initialState: {
    Dashboard: {
      PinnedToolbars: ['custombuttons', 'Layout'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Grouped Table Layout',
      Layouts: [
        {
          Name: 'Grouped Table Layout',
          RowGroupedColumns: ['language'],
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          SuppressAggFuncInHeader: true,
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'github_watchers',
            'updated_at',
            'license',
            'created_at',
          ],
        },
        {
          Name: 'Pivot Layout',
          PivotColumns: ['language'],
          PivotGroupedColumns: ['license'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
        },
      ],
    },
  },
};
```

## Formatting & Styling Aggregations

AdapTable allows users to [format and style](https://www.adaptabletools.com/docs/handbook-column-formatting/index.md) AggFuncs in a grouped row, in exactly the same way as a data row.

See [Formatting and Styling Aggregations](https://www.adaptabletools.com/docs/handbook-aggregation-formatting/index.md) for more information and demos

## Defining Aggregations

### Defining a Layout with Aggregations

A Table Layout can define which Aggregations are available in **Grouped Rows**, using the `TableAggregationColumns` property.

The [`TableAggregationColumns`](https://www.adaptabletools.com/docs/reference/tableaggregationcolumns.md) object contains 2 items:

- a `ColumnId`  (string)
- either an `aggFunc` (e.g. sum) or *true* (uses default `aggFunc`)

You can supply [Weighted Average Aggregations](https://www.adaptabletools.com/docs/handbook-aggregation-weighted-average/index.md) if needed.

In this case 2 properties must be provided to the definition:

- `type` - always set to 'weightedAverage'
- `weightedColumnId` - column which provides the Weight

Set `GrandTotalRow` prop to display a [Row providing Totals of all aggregations](https://www.adaptabletools.com/docs/handbook-aggregation-grand-total-row/index.md).

Options for the property are:
- `top` - shows it at top of grid
- `bottom` - shows it at bottom of grid
- `true` - equivalent to `top`
- `false` - doesn't display it

Setting `SuppressAggFuncInHeader` to *true* hides the name of the aggFunc in the Column Header, e.g. it will show 'Open PRs' instead of 'sum(Open PRs)'

```ts [[1, 14, "TableAggregationColumns"],  [2, 30, "weightedAverage"],  [3, 34, "GrandTotalRow"],  [4, 35, "SuppressAggFuncInHeader"]]
// Define a Layout with 3 Aggregations:
// open_pr_count (avg), github_watchers (sum) & github_stars (default aggFunc for col)
// Weighted Aggregation for examResult (using attendance as Weighted Column)
// Provide a GrandTotalRow at top of Grid
// Set Aggregation Columns to display without name of the aggFunc
const initialState: InitialState = {
  Layout: {
    CurrentLayout: 'Grouping Layout',
    Layouts: [
    {
      Name: 'Grouping Layout',
      TableColumns: ['github_stars', 'open_pr_count', 'github_watchers', 'examResult', 'attendance'],
      RowGroupedColumns: ['license', 'language'],
      TableAggregationColumns: [
      {
        ColumnId: 'open_pr_count',
        AggFunc: 'avg'
      },
      {
        ColumnId: 'github_watchers',
        AggFunc: 'sum'
      },
      {
        ColumnId: 'github_stars',
        AggFunc: true
      },
      {
        ColumnId: 'examResult',
        AggFunc: {
          type: 'weightedAverage',
          weightedColumnId: 'attendance',
        },
      }],
      GrandTotalRow: 'top',
      SuppressAggFuncInHeader: true,
     }],
  },
}
```
