HelpBot Assistant

How can I help you?

Getting Started with React Charts of Syncfusion

19 Feb 202624 minutes to read

This section describes the steps to create a simple Chart and demonstrates the basic usage of the Chart component.

Ready to streamline your Syncfusion® React development? Discover the full potential of Syncfusion® React components with Syncfusion® AI Coding Assistant. Effortlessly integrate, configure, and enhance your projects with intelligent, context-aware code suggestions, streamlined setups, and real-time insights—all seamlessly integrated into your preferred AI-powered IDEs like VS Code, Cursor, Syncfusion® CodeStudio and more. Explore Syncfusion® AI Coding Assistant

A quick video overview of the React Charts setup is available:

Dependencies

Below is the list of minimum dependencies required to use the Chart component.

|-- @syncfusion/ej2-react-charts
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-react-base
    |-- @syncfusion/ej2-pdf-export
    |-- @syncfusion/ej2-file-utils
    |-- @syncfusion/ej2-compression
    |-- @syncfusion/ej2-svg-base

Once you install @syncfusion/ej2-react-charts, the other required dependencies will be installed automatically along with the main package.

Installation and configuration

Create a React application

To easily set up a React application, use the Vite CLI (npm create vite), which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.

Note: To create a React application using create-react-app instead, refer to this documentation for more details.

To create a new React application, run the following command.

npm create vite@latest my-app

This command will prompt you for a few settings for the new project, such as selecting a framework and a variant.

Initial_setup

To set up a React application in TypeScript environment, run the following command.

npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev

To set up a React application in JavaScript environment, run the following command.

npm create vite@latest my-app -- --template react
cd my-app
npm run dev

Install Syncfusion® Chart package

All the available Essential® JS 2 packages are published in the npmjs.com public registry.

To install the Syncfusion® Chart package, use the following command

npm install @syncfusion/ej2-react-charts --save

The –save will instruct NPM to include the Chart package inside of the dependencies section of the package.json.

Add Chart to the project

Add the Chart component to src/App.tsx using the following code.

import { ChartComponent } from '@syncfusion/ej2-react-charts';
import * as React from 'react';
function App() {
  return (<ChartComponent />);
}
export default App;
import { ChartComponent } from '@syncfusion/ej2-react-charts';
import * as React from 'react';
function App() {
    return (<ChartComponent />);
}
export default App;

Now run the npm run dev command in the console to start the development server. This command compiles your code and serves the application locally, opening it in the browser.

npm run dev

The below example shows a basic Chart.

import { ChartComponent } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
function App() {
    return <ChartComponent id='charts'/>;
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
import { ChartComponent  } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';

function App() {
  return <ChartComponent id='charts' />
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);

Module injection

Chart components are segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature service in the Chart component. In the current application, we are going to modify the above basic Chart to visualize sales data for a particular year. For this application we are going to use line series, tooltip, data label, category axis and legend feature of the Chart. Please find the relevant feature service name and description as follows.

  • LineSeries - Inject this module in to services to use line series.
  • Legend - Inject this module in to services to use legend feature.
  • Tooltip - Inject this module in to services to use tooltip feature.
  • DataLabel - Inject this module in to services to use datalabel feature.
  • Category - Inject this module in to services to use category feature.

Import the above-mentioned modules from the Chart package and inject them into the services section of the Chart component as follows.

import { ChartComponent, LineSeries, Legend, Tooltip, DataLabel, Category, Inject } from '@syncfusion/ej2-react-charts';
import * as React from "react";

function App() {
  return <ChartComponent>
    <Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category]}/>
  </ChartComponent>;
}
export default App;
import { ChartComponent, LineSeries, Legend, Tooltip, DataLabel, Category, Inject } from '@syncfusion/ej2-react-charts';
import * as React from "react";
function App() {
    return <ChartComponent>
    <Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category]}/>
  </ChartComponent>;
}
export default App;

Populate Chart with data

This section explains how to plot the following JSON data to the Chart.

export let data = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
export let data: Object[] = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];

Add series to the Chart using the <SeriesCollectionDirective> and <SeriesDirective> components. Map the JSON fields month and sales to the series xName and yName properties, and set the JSON array as the dataSource property.

Since the JSON contains category data, set the valueType for the horizontal axis (primaryXAxis) to Category. By default, the axis valueType is Numeric.

import { Category, ChartComponent, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';
function App() {
  const primaryXAxis = { valueType: 'Category' };
  return <ChartComponent id="charts" primaryXAxis={primaryXAxis}>
    <Inject services={[LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>;
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
import { AxisModel, Category, ChartComponent, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';

function App() {

  const primaryXAxis: AxisModel = { valueType: 'Category' };

  return <ChartComponent id="charts" primaryXAxis={primaryXAxis}>
    <Inject services={[LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>

}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
export let data = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
export let data: Object[] = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];

The sales data are in thousands, so format the vertical axis label by adding $ as a prefix and K as a suffix to each label. This can be achieved by setting the ${value}K to the labelFormat property of axis. Here, {value} act as a placeholder for each axis label.

import { Category, ChartComponent, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';
function App() {
  const primaryXAxis = { valueType: 'Category' };
  const primaryYAxis = { labelFormat: '${value}K' };
  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} primaryYAxis={primaryYAxis}>
    <Inject services={[LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>;
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
import { AxisModel, Category, ChartComponent, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';

function App() {

  const primaryXAxis: AxisModel = { valueType: 'Category' };
  const primaryYAxis: AxisModel = { labelFormat: '${value}K' };

  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} primaryYAxis={primaryYAxis}>
    <Inject services={[LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
export let data = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
export let data: Object[] = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];

Add Chart title

You can add a title using the title property to the Chart to provide quick information to the user about the data plotted in the Chart.

import { Category, ChartComponent, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';
function App() {
  const primaryXAxis = { valueType: 'Category' };
  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} title='Sales Analysis'>
    <Inject services={[LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>;
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
import { AxisModel, Category, ChartComponent, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';

function App() {

  const primaryXAxis: AxisModel = { valueType: 'Category' };

  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} title='Sales Analysis'>
    <Inject services={[LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
export let data = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
export let data: Object[] = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];

Enable legend

You can use legend for the Chart by setting the visible property to true in legendSettings object and by injecting the Legend module into the services.

import { Category, ChartComponent, Inject, Legend, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';
function App() {
  const legendSettings = { visible: true };
  const primaryXAxis = { valueType: 'Category' };
  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} legendSettings={legendSettings}>
    <Inject services={[Legend, LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>;
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
import { AxisModel, Category, ChartComponent, Inject, Legend, LegendSeriesModel, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';

function App() {

  const legendSettings: LegendSeriesModel = { visible: true };
  const primaryXAxis: AxisModel = { valueType: 'Category' };

  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} legendSettings={legendSettings}>
    <Inject services={[Legend, LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
export let data = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
export let data: Object[] = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];

Add data label

You can add data labels to improve the readability of the Chart. This can be achieved by setting the visible property to true in the dataLabel object and by injecting DataLabel module into the services. Now, the data labels are arranged smartly based on series.

import { Category, ChartComponent, DataLabel, Inject, Legend, LineSeries, SeriesCollectionDirective, SeriesDirective } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';
function App() {
  const primaryYAxis = { labelFormat: '${value}K' };
  const primaryXAxis = { valueType: 'Category' };
  const legendSettings = { visible: true };
  const marker = { dataLabel: { visible: true } };
  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} legendSettings={legendSettings} primaryYAxis={primaryYAxis}>
    <Inject services={[DataLabel, Legend, LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker} type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>;
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
import {
  AxisModel, Category, ChartComponent, DataLabel, Inject, Legend,
  LegendSeriesModel, LineSeries, SeriesCollectionDirective, SeriesDirective, MarkerSettingsModel
} from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';

function App() {
  const primaryYAxis: AxisModel = { labelFormat: '${value}K' };
  const primaryXAxis: AxisModel = { valueType: 'Category' };
  const legendSettings: LegendSeriesModel = { visible: true };
  const marker: MarkerSettingsModel = { dataLabel: { visible: true } };

  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} legendSettings={legendSettings} primaryYAxis={primaryYAxis}>
    <Inject services={[DataLabel, Legend, LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker} type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
export let data = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
export let data: Object[] = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];

Enable tooltip

The tooltip is useful when you cannot display information by using the data labels due to space constraints. You can enable tooltip by setting the enable property as true in tooltip object and by injecting Tooltip module into the services.

import { Category, ChartComponent, DataLabel, Inject, Legend, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';
function App() {
  const tooltip = { enable: true, shared: false };
  const primaryYAxis = { labelFormat: '${value}K' };
  const primaryXAxis = { valueType: 'Category' };
  const legendSettings = { visible: true };
  const marker = { dataLabel: { visible: true } };
  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} legendSettings={legendSettings} primaryYAxis={primaryYAxis} tooltip={tooltip}>
    <Inject services={[DataLabel, Tooltip, Legend, LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker} type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>;
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
import {
  AxisModel, Category, ChartComponent, DataLabel, Inject,
  Legend, LegendSeriesModel, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip, TooltipSettingsModel, MarkerSettingsModel
} from '@syncfusion/ej2-react-charts';
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { data } from './datasource';

function App() {
  const tooltip: TooltipSettingsModel = { enable: true, shared: false };
  const primaryYAxis: AxisModel = { labelFormat: '${value}K' };
  const primaryXAxis: AxisModel = { valueType: 'Category' };
  const legendSettings: LegendSeriesModel = { visible: true };
  const marker: MarkerSettingsModel = { dataLabel: { visible: true } };

  return <ChartComponent id="charts" primaryXAxis={primaryXAxis} legendSettings={legendSettings} primaryYAxis={primaryYAxis} tooltip={tooltip}>
    <Inject services={[DataLabel, Tooltip, Legend, LineSeries, Category]} />
    <SeriesCollectionDirective>
      <SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker} type='Line'/>
    </SeriesCollectionDirective>
  </ChartComponent>
}
export default App;
createRoot(document.getElementById('charts')).render(<App />);
export let data = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
export let data: Object[] = [
    { month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
    { month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
    { month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
    { month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
    { month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
    { month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];

You can refer to our React Charts feature tour page for its groundbreaking feature representations. You can also explore our React Charts example that shows various Chart types and how to represent time-dependent data, showing trends in data at equal intervals.