# Charting

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

- AdapTable works with **AG Grid Charts** (Enterprise) and optional **external** chart libraries
- AdapTable adds persistence in Adaptable State, named chart definitions, custom display locations, layout-driven opening, UI entitlements, and external-chart integration
- Run-time behaviour is covered in [Using Charts](https://www.adaptabletools.com/docs/handbook-charts-using/index.md); design-time setup in [Configuring Charts](https://www.adaptabletools.com/docs/handbook-charts-configuring/index.md)

AdapTable fully supports AG Grid charting and adds features around chart **accessibility**, **persistence**, and **placement**.

## Topics in this Section

<section className="my-8 sm:my-10 grid grid-cols-1 lg:grid-cols-2 gap-x-8 gap-y-4">
  <div className="flex flex-col justify-center">
    
  </div>
  <div className="flex flex-col justify-center">
    
  </div>
  <div className="flex flex-col justify-center">
    
  </div>
  <div className="flex flex-col justify-center">
    
  </div>
</section>

## Setup

Integrated AG Grid charts require Enterprise chart modules and `enableCharts` on `gridOptions`:

- Register `IntegratedChartsModule` with `AgChartsEnterpriseModule` — see [AG Grid Modules](https://www.adaptabletools.com/docs/getting-started-aggrid-modules/index.md)
- Set `enableCharts: true` (and usually `cellSelection: true`) in `gridOptions`

External charting does not use AG Grid integrated charts; see [External Chart Libraries](https://www.adaptabletools.com/docs/handbook-charts-external/index.md).

## Saving Charts

Charts created in AG Grid can be saved into [Adaptable State](https://www.adaptabletools.com/docs/dev-guide-adaptable-state/index.md).

The `saveChartBehaviour` property in [Charting Options](https://www.adaptabletools.com/docs/technical-reference-charts/index.md) has 3 potential values:

- `auto`: all created charts are saved automatically in Adaptable State
- `manual`: a popup is shown prompting the user to save the chart and provide a name
- `none`: charts are not saved into AdapTable State (the default value)

See [Using Charts](https://www.adaptabletools.com/docs/handbook-charts-using/index.md) for run-time saving, opening, and managing charts

**Example: Saving AG Grid Charts**

How to leverage AG Grid's Charts in AdapTable

- This demo shows how straightforward it is to save AG Grid Charts in AdapTable
- Because `saveChartBehaviour` in [Charting Options](https://www.adaptabletools.com/docs/technical-reference-charts/index.md) is set to `manual`, a Notification appears when creating a Chart
- Any charts which are created will then appear in the Charts Toolbar, Tool Panel and Status Panel where they can be re-opened if required

- Create a chart using the currently selected cells and give it a name in the popup provided
- Close the chart and see that you can open it again at any time using the Charts Toolbar

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template, with charts]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template] + ag-charts-enterprise
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 {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Saving Charts',
  chartingOptions: {
    saveChartBehaviour: 'manual',
    chartContainers: [],
  },
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Charting',
          Toolbars: ['Charting'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Charting'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'github_stars',
            'github_watchers',
            'name',
            'language',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,

    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  // enable charts and set a theme
  enableCharts: true,
  chartThemes: ['ag-vivid-dark'],
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  setTimeout(() => {
    // Pick 5 cells in 2 columns
    const colsToSelect: string[] = ['github_stars', 'github_watchers'];
    const startIndex: number = 1;
    const endIndex: number = 5;

    // Select them in AdapTable using AdapTable's GridAPI
    adaptableApi.gridApi.selectCellRange({
      columnIds: colsToSelect,
      rowIndexStart: startIndex,
      rowIndexEnd: endIndex,
    });
  }, 10);
};
```

## Displaying Charts

AdapTable can be configured to display charts in various locations provided by users.

There is no restriction on the number of Charts which can be displayed in each location.

See [Configuring Charts](https://www.adaptabletools.com/docs/handbook-charts-configuring/index.md) for more information and accompanying demos

## External Charts

AdapTable supports both AG Grid Charts (the most common use case) and External Charts.

Developers are able to provide their own charts, and use AdapTable to save and re-render them as required.

See [External Chart Libraries](https://www.adaptabletools.com/docs/handbook-charts-external/index.md) for full details

## Cross Filter Charts

Cross Filtering Charts is semi-supported in AdapTable.

Changing a Filter in AdapTable will update the Chart; however changing the chart will not update the Filter.

Filtering the grid from chart interactions is not supported today

**Example: Cross Filter Charts**

Supporting AG Grid's Cross Filter Charts

- Two linked charts are opened when AdapTable loads; grid filters drive the chart data

- Filter the Language and License columns and see the Charts update
- Note: Changing the Chart will not filter the Grid

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template, with charts]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template + shown below]
index.html             [template]
package.json           [template] + ag-charts-enterprise
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 {
  AdaptableColumn,
  AdaptableColumnContext,
  AdaptableOptions,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Displaying Charts',
  filterOptions: {
    columnFilterOptions: {
      defaultTextColumnFilter: 'IN',
    },
  },
  chartingOptions: {
    saveChartBehaviour: 'none',
    chartContainers: [
      {
        name: 'Above Grid',
        element: '#demoOutputAbove',
        chartsDisplay: 'multiple',
      },
    ],
  },
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Charting', Toolbars: ['Charting']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Charting'],
        },
      ],
    },
    Theme: {
      CurrentTheme: 'dark',
    },

    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'language',
            'github_stars',
            'license',
            'github_watchers',
            'name',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          ColumnSorts: [{ColumnId: 'github_stars', SortOrder: 'Desc'}],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,

    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  enableCharts: true,
  chartThemes: ['ag-vivid-dark'],
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo, ChartDefinition} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({agGridApi}: AdaptableReadyInfo) => {
  agGridApi.createCrossFilterChart({
    chartType: 'pie',
    cellRange: {
      columns: ['language', 'github_stars'],
      rowStartIndex: 0,
      rowEndIndex: 24,
    },
    aggFunc: 'sum',
    chartThemeOverrides: {
      common: {
        title: {
          enabled: true,
          text: 'Github Stars by Language',
        },
      },
      pie: {
        series: {
          title: {
            enabled: false,
          },
        },
      },
    },
    chartContainer: document.querySelector('#demoOutputAbove') as HTMLElement,
  });
  agGridApi.createCrossFilterChart({
    chartType: 'bar',
    cellRange: {
      columns: ['license', 'github_watchers'],
      rowStartIndex: 0,
      rowEndIndex: 24,
    },
    aggFunc: 'count',
    chartThemeOverrides: {
      common: {
        title: {
          enabled: true,
          text: 'Watchers by License',
        },
        legend: {
          enabled: false,
        },
      },
    },
    chartContainer: document.querySelector('#demoOutputAbove') as HTMLElement,
  });

  //adaptableApi.chartingApi.showChartDefinition(chartDefs[1], topChartContainer);
};
```

**`src/styles.css`**

```css
#demoOutputAbove {
  display: flex;
}
```

## UI Entitlements

The UI Entitlements behaviour for Charts is as expected for `Full` and `Hidden` [`Access Levels`](https://www.adaptabletools.com/docs/reference/accesslevel.md).

The `ReadOnly` Entitlement behaviour is that Charts will display, and users can create AG Grid charts, but will not be permitted to save them.

See [Module Entitlements](https://www.adaptabletools.com/docs/handbook-permissioning-modules/index.md) for Charting module access.

### FAQ

**Can I save Charts into AdapTable State?**
Yes — set `saveChartBehaviour` to `manual` or `auto` in [Charting Options](https://www.adaptabletools.com/docs/technical-reference-charts/index.md). This capability was introduced in [AdapTable Version 12](https://www.adaptabletools.com/support/version-12-release-note).

**Where do I configure chart locations and design-time definitions?**
See [Configuring Charts](https://www.adaptabletools.com/docs/handbook-charts-configuring/index.md).

**Can I use Highcharts or another library instead of AG Grid Charts?**
Yes — see [External Chart Libraries](https://www.adaptabletools.com/docs/handbook-charts-external/index.md).

**Where are Options, State, and API documented?**
See [Charts Technical Reference](https://www.adaptabletools.com/docs/technical-reference-charts/index.md).
