---
title: "Chart Container"
enterprise: true
framework: angular
version: "36.1.0"
---

# Chart Container

This section shows how to specify an alternative chart container to the default grid-provided popup window.

Displaying the generated chart within the grid-provided popup window will suit most needs. However, you may wish to display the chart in a different location. For example, your application may already have popup windows, and you wish to use the same library for consistency.

## Specifying Chart Container

To provide an alternative container for popup windows use the grid callback `createChartContainer(chartRef)`.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `createChartContainer` | `CreateChartContainer` |  |  | Callback to enable displaying the chart in an alternative chart container. Module: [`IntegratedChartsModule`](https://www.ag-grid.com/angular-data-grid/modules/). [Initial](https://www.ag-grid.com/angular-data-grid/grid-interface/#initial-grid-options). |

The callback is called each time the user elects to create a chart via the grid UI. The callback is provided with a `ChartRef` implementation:

Properties available on the `ChartRef` interface.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `chartId` | `string` |  |  | The id of the created chart. |
| `chart` | `any` |  |  | The chart instance that is produced by AG Charts which can be used to interact with the chart directly. |
| `chartElement` | [`HTMLElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement) |  |  | The chart DOM element, which the application is responsible for placing into the DOM. |
| `destroyChart` | `Function` |  |  | The application is responsible for calling this when the chart is no longer needed. |
| `focusChart` | `Function` |  |  | Focuses the chart. If opening the dialog via the API, the chart is not focused by default, and this method can be used. |
| `setMaximized` | `Function` |  |  | If opening the chart in a dialog, sets the maximized status of the dialog, else does nothing. |

The example below demonstrates the `createChartContainer(chartRef)` callback. The example does not use an alternative popup window, but instead places the charts into the DOM below the grid. This crude approach is on purpose to minimise the complexity of the example and focus on just the callback and the interactions of the grid.

> **Note**
>
> When providing an element to display your chart, it is important to always set the `popupParent` to be `document.body`. This will allow floating elements within the chart's menus to be positioned correctly.

From the example below, the following can be noted:

- Select a range of numbers (medal columns) and create a chart from the context menu.
- The chart appears below the grid rather than in a popup window. This is because the `createChartContainer(chartRef)` is implemented.
- Each chart is displayed alongside a 'Destroy' button. The logic behind the destroy button calls `destroyChart()` to destroy the chart instance.

#### Provided Container

```ts
import { HttpClient } from "@angular/common/http";
import type { ElementRef } from "@angular/core";
import { Component, ViewChild, signal } from "@angular/core";
import { AgChartsEnterpriseModule } from "ag-charts-enterprise";

import { AgGridAngular } from "ag-grid-angular";
import type { ChartRef, ColDef, GridReadyEvent } from "ag-grid-community";
import {
  ClientSideRowModelModule,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ContextMenuModule,
  IntegratedChartsModule,
  RowGroupingModule,
} from "ag-grid-enterprise";

import "./styles.css";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  IntegratedChartsModule.with(AgChartsEnterpriseModule),
  ColumnMenuModule,
  ContextMenuModule,
  RowGroupingModule,
]);

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<div id="container">
    <ag-grid-angular
      style="width: 100%; height: 300px;"
      [columnDefs]="columnDefs"
      [defaultColDef]="defaultColDef"
      [cellSelection]="true"
      [enableCharts]="true"
      [popupParent]="popupParent"
      [createChartContainer]="createChartContainer"
      [rowData]="rowData"
      (gridReady)="onGridReady($event)"
    />
    <div #chartParent class="chart-wrapper">
      @if (chartRef()) {
        <div class="chart-wrapper-top">
          <h2 class="chart-wrapper-title">
            Chart created at {{ createdTime() }}
          </h2>
          <button (click)="updateChart()">Destroy Chart</button>
        </div>
      } @else {
        <div class="chart-placeholder">Chart will be displayed here.</div>
      }
    </div>
  </div> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "athlete", width: 150, chartDataType: "category" },
    { field: "gold", chartDataType: "series" },
    { field: "silver", chartDataType: "series" },
    { field: "bronze", chartDataType: "series" },
    { field: "total", chartDataType: "series" },
  ];
  defaultColDef: ColDef = { flex: 1 };
  popupParent: HTMLElement | null = document.body;
  rowData!: any[];
  chartRef = signal<ChartRef | undefined>(undefined);
  createdTime = signal<string | undefined>(undefined);

  @ViewChild("chartParent") chartParent?: ElementRef;

  constructor(private http: HttpClient) {}

  onGridReady(params: GridReadyEvent) {
    this.http
      .get<
        any[]
      >("https://www.ag-grid.com/example-assets/wide-spread-of-sports.json")
      .subscribe((data: any[]) => {
        this.rowData = data;
      });
  }

  updateChart(chartRef: ChartRef | undefined) {
    if (this.chartRef() !== chartRef) {
      // Destroy previous chart if it exists
      this.chartRef()?.destroyChart();
    }
    this.chartRef.set(chartRef);
    this.createdTime.set(new Date().toLocaleString());
  }

  // Arrow function used to correctly bind this to the component
  createChartContainer = (chartRef: ChartRef) => {
    this.updateChart(chartRef);
    this.chartParent?.nativeElement.appendChild(chartRef.chartElement);
  };
}
```

[Live example: Provided Container](https://www.ag-grid.com/examples/integrated-charts-container/provided-container/angular)
