AG Charts TypeScript Types
AG Charts is a high-performance, canvas-based charting library for JavaScript and TypeScript, offering extensive customizability and no third-party dependencies for its core functionality. It integrates seamlessly with popular frameworks like React, Angular, and Vue. The `ag-charts-types` package, currently at stable version 13.2.1, provides comprehensive TypeScript type definitions, enabling type-safe development for AG Charts applications. The library maintains a frequent release cadence, delivering continuous improvements and new features across minor and patch versions, with significant breaking changes typically reserved for major version bumps. Its key differentiators include exceptional rendering performance, a highly flexible API for detailed customization, and a robust feature set suitable for enterprise data visualization.
Common errors
-
Argument of type 'string' is not assignable to parameter of type 'HTMLElement | undefined'.
cause Attempting to pass a string ID for the chart container, which is no longer supported in recent versions of AG Charts.fixChange `container: 'myChartId'` to `container: document.getElementById('myChartId') ?? undefined`. -
Property 'create' does not exist on type 'typeof import("ag-charts-community")'. Did you mean 'AgCharts.create'?cause Incorrect import of `AgCharts` as a named export instead of a namespace import.fixChange `import { AgCharts } from 'ag-charts-community';` to `import * as AgCharts from 'ag-charts-community';`. -
Type '{ type: "bar"; xKey: string; yKey: string; }' is not assignable to type 'AgSeriesOptions'. Types of property 'type' are incompatible. Type '"bar"' is not assignable to type '"line" | "area" | "column" | ...'.cause Using the deprecated `bar` series type after v12.0.0.fixUpdate `type: 'bar'` to `type: 'column'` in your series options.
Warnings
- breaking In AG Charts v13.0.0, the `container` option for `AgCharts.create` and `AgCharts.update` no longer accepts a string ID. You must provide an actual DOM element. Several styling properties were also renamed or moved (e.g., `axis.label.padding` to `axis.label.spacing`, `tooltip.position.xOffset` to `tooltip.position.x`).
- breaking AG Charts v12.0.0 changed `AgCharts.create` and `AgCharts.update` to return `void` instead of the chart instance. If you need a reference to the chart instance, use `AgCharts.getChart(element)`.
- breaking The series type `bar` was renamed to `column` in AG Charts v12.0.0. Using `type: 'bar'` will no longer render a column chart.
- gotcha AG Charts typically expects its data array to be an `Array<any>` or a more specific type. Incorrect data structures or missing keys can lead to charts not rendering or displaying unexpected behavior, often silently.
Install
-
npm install ag-charts-types -
yarn add ag-charts-types -
pnpm add ag-charts-types
Imports
- AgCharts
import { AgCharts } from 'ag-charts-community';import * as AgCharts from 'ag-charts-community';
- AgChartOptions
import type { AgChartOptions } from 'ag-charts-types';import { AgChartOptions } from 'ag-charts-community'; - AgCartesianChartOptions
import { AgCartesianChartOptions } from 'ag-charts-types';import { AgCartesianChartOptions } from 'ag-charts-community'; - AgLineSeriesOptions
import type { AgLineSeriesOptions } from 'ag-charts-community';
Quickstart
import * as AgCharts from 'ag-charts-community';
import { AgChartOptions } from 'ag-charts-community';
const data = [
{ year: '2020', sales: 1500, profit: 800 },
{ year: '2021', sales: 1800, profit: 950 },
{ year: '2022', sales: 2200, profit: 1100 },
{ year: '2023', sales: 2500, profit: 1300 },
];
const options: AgChartOptions = {
container: document.getElementById('myChart') ?? undefined,
data: data,
title: {
text: 'Sales and Profit by Year',
},
subtitle: {
text: 'Example using AG Charts',
},
series: [
{
type: 'line',
xKey: 'year',
yKey: 'sales',
yName: 'Sales',
stroke: '#007bff',
marker: {
enabled: true,
},
},
{
type: 'line',
xKey: 'year',
yKey: 'profit',
yName: 'Profit',
stroke: '#28a745',
marker: {
enabled: true,
},
},
],
axes: [
{
type: 'category',
position: 'bottom',
title: {
text: 'Year',
},
},
{
type: 'number',
position: 'left',
title: {
text: 'Amount',
},
},
],
};
// Ensure the container exists in your HTML: <div id="myChart" style="height: 400px; width: 600px;"></div>
AgCharts.create(options);