AG Charts React Component
AG Charts React is a high-performance, canvas-based charting library for React applications. It offers a comprehensive set of 30+ chart types, including common visualizations like bar, line, area, and pie charts, as well as specialized types such as financial charts and maps (some advanced features are part of the Enterprise version). The library prioritizes outstanding performance, especially with large datasets and high-frequency updates, and boasts no third-party runtime dependencies, minimizing bundle size and potential conflicts. The current stable version is 13.2.1, with frequent patch and minor releases, and major versions typically introducing significant API or feature enhancements. Key differentiators include its performance, native React integration, comprehensive feature set (split between Community and Enterprise), and a focus on enterprise application development.
Common errors
-
Uncaught ReferenceError: exports is not defined
cause This error often occurs in older bundler setups (like certain Create React App configurations) when `ag-charts-react` or its dependencies are treated as CommonJS modules in an environment expecting ESM, or vice-versa.fixIf using an older Create React App setup, ensure your bundler is correctly configured to handle ESM modules. Sometimes, using `--force` or `--legacy-peer-deps` during installation might resolve immediate issues, but a proper bundler configuration for ESM is preferred. -
TypeError: Cannot read properties of undefined (reading 'series') (or similar for other options properties like 'data', 'axes')
cause The `options` object or its required nested properties (`data`, `series`) are `undefined` or malformed when passed to `AgChartsReact`.fixVerify that your `options` object is correctly initialized and contains at least `data` (an array of objects) and `series` (an array of series definitions), as shown in the quickstart. Ensure all `xKey` and `yKey` values correctly map to properties in your `data` objects. -
AG Charts - unable to resolve global window
cause This error typically indicates that AG Charts is being initialized in a server-side rendering (SSR) or Node.js environment where the global `window` object is not available.fixFor SSR frameworks like Next.js, ensure that `AgChartsReact` components are rendered only on the client-side. You can achieve this using dynamic imports with `ssr: false` or by conditionally rendering the component after the component mounts (e.g., within a `useEffect` hook).
Warnings
- breaking AG Charts v13.0.0 introduced significant changes to axis configuration, making the `axes` option a dictionary instead of an array. It also simplified secondary axis setup and removed the `axes.keys` option, relying instead on `series._KeyAxes` properties.
- breaking AG Charts v13.0.0 changed how themes are applied when creating custom themes. The `theme` option now requires a `baseTheme` property to inherit from an existing theme, rather than directly specifying the base theme name.
- breaking AG Charts requires React 18 or newer as a peer dependency. Using older versions of React (e.g., React 17) might lead to installation issues or unexpected runtime behavior, even if it might 'work' with `--force` flags.
- gotcha The `options` prop passed to `AgChartsReact` is expected to be an immutable object for optimal performance and correct reactivity. Mutating the `options` object directly will not trigger chart updates; instead, a new `options` object must be provided to React's state management.
- breaking AG Charts v12.0.0 introduced several breaking changes including the removal of deprecated properties like `zoom.minVisibleItemsX/Y`, `tooltip.position.type`, and changes to `contextMenu.extraActions`. It also leveraged TypeScript Generics for improved type safety and developer experience.
Install
-
npm install ag-charts-react -
yarn add ag-charts-react -
pnpm add ag-charts-react
Imports
- AgChartsReact
import AgChartsReact from 'ag-charts-react'; const AgChartsReact = require('ag-charts-react');import { AgChartsReact } from 'ag-charts-react'; - AgChartOptions
import { AgChartOptions } from 'ag-charts-react'; import AgChartOptions from 'ag-charts-react';import type { AgChartOptions } from 'ag-charts-react'; - AgChartInstance
import { AgChartInstance } from 'ag-charts-react';import type { AgChartInstance } from 'ag-charts-react';
Quickstart
import React, { useState, useRef, useEffect } from 'react';
import { AgChartsReact } from 'ag-charts-react';
import type { AgChartOptions, AgChartInstance } from 'ag-charts-react';
const ChartExample: React.FC = () => {
const chartRef = useRef<AgChartInstance | null>(null);
const [options, setOptions] = useState<AgChartOptions>({
data: [
{ month: 'Jan', avgTemp: 2.3, iceCreamSales: 162000 },
{ month: 'Mar', avgTemp: 6.3, iceCreamSales: 302000 },
{ month: 'May', avgTemp: 16.2, iceCreamSales: 800000 },
{ month: 'Jul', avgTemp: 22.8, iceCreamSales: 1254000 },
{ month: 'Sep', avgTemp: 14.5, iceCreamSales: 950000 },
{ month: 'Nov', avgTemp: 8.9, iceCreamSales: 200000 },
],
series: [
{
type: 'bar',
xKey: 'month',
yKey: 'iceCreamSales',
yName: 'Ice Cream Sales',
},
{
type: 'line',
xKey: 'month',
yKey: 'avgTemp',
yName: 'Average Temperature',
marker: { enabled: true },
strokeWidth: 3,
axes: 'secondary-y-axis',
},
],
axes: [
{ type: 'category', position: 'bottom' },
{ type: 'number', position: 'left', title: { text: 'Sales' } },
{
type: 'number',
position: 'right',
id: 'secondary-y-axis',
title: { text: 'Temperature (°C)' },
},
],
title: { text: 'Ice Cream Sales vs. Average Temperature' },
subtitle: { text: '(Monthly Data)' },
legend: { enabled: true },
});
useEffect(() => {
// Example of programmatic API interaction after mount
if (chartRef.current) {
console.log('Chart instance available:', chartRef.current);
// You could call chartRef.current.download('my-chart.png') here for example
}
}, []);
return (
<div style={{ height: '500px', width: '100%' }}>
<AgChartsReact ref={chartRef} options={options} />
</div>
);
};
export default ChartExample;