Getting Started with React Charts of Syncfusion
19 Sep 202424 minutes to read
This section explains you the steps required to create a simple chart and demonstrate the basic usage of the chart control.
To get start quickly with React Charts, you can check on this video:
Dependencies
Below is the list of minimum dependencies required to use the chart component.
|-- @syncfusion/ej2-react-charts
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-charts
|-- @syncfusion/ej2-react-base
|-- @syncfusion/ej2-pdf-export
|-- @syncfusion/ej2-file-utils
|-- @syncfusion/ej2-compression
|-- @syncfusion/ej2-svg-base
Installation and configuration
You can use create-react-app
to setup the applications.
To install create-react-app
run the following command.
npm install -g create-react-app
-
To set-up a React application in TypeScript environment, run the following command.
create-react-app quickstart --template typescript cd quickstart npm start
- To set-up a React application in JavaScript environment, run the following command.
create-react-app quickstart cd quickstart npm start
-
Install Syncfusion packages using below command.
npm install @syncfusion/ej2-react-charts --save
Add Chart to the Project
Now, you can start adding Chart component in the application.
For getting started, add the Chart component in src/App.tsx
file using 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 start
command in the console, it will run your application and open the browser window.
npm start
The below example shows a basic Chart.
import { ChartComponent } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
return <ChartComponent id='charts'/>;
}
;
export default App;
import { ChartComponent } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
return <ChartComponent id='charts' />
};
export default App;
Module Injection
Chart component are segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature service in the AppModule. 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 toservices
to use line series. -
Legend
- Inject this module in toservices
to use legend feature. -
Tooltip
- Inject this module in toservices
to use tooltip feature. -
DataLabel
- Inject this module in toservices
to use datalabel feature. -
Category
- Inject this module in toservices
to use category feature.
These modules should be injected to the services
section as follows,
import { Category, ChartComponent, DataLabel, LineSeries, Legend, Tooltip, Inject } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
return <ChartComponent id='charts'>
<Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category]}/>
</ChartComponent>;
}
export default App;
import { Category, ChartComponent, DataLabel, LineSeries, Legend, Tooltip, Inject } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
return <ChartComponent id='charts'>
<Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category]}/>
</ChartComponent>;
}
export default App;
Populate Chart with Data
This section explains how to plot below 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: any[] = [
{ 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 a series object to the chart by using series
property. Now map the field names month
and sales
in the JSON data to the xName
and yName
properties of the series, then set the JSON data to dataSource
property.
Since the JSON contains category data, set the valueType
for horizontal axis to Category
. By default, the axis valueType is Numeric
.
import { Category, ChartComponent, ColumnSeries, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const primaryxAxis = { valueType: 'Category' };
return <ChartComponent id="charts" primaryXAxis={primaryxAxis}>
<Inject services={[ColumnSeries, Tooltip, LineSeries, Category]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales'/>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
import { AxisModel, Category,ChartComponent, ColumnSeries, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const primaryxAxis: AxisModel = { valueType: 'Category' };
return <ChartComponent id="charts" primaryXAxis={primaryxAxis}>
<Inject services={[ColumnSeries, Tooltip, LineSeries, Category]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' />
</SeriesCollectionDirective>
</ChartComponent>
};
export default 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, ColumnSeries, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
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={[ColumnSeries, Tooltip, LineSeries, Category]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales'/>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
import { AxisModel, Category,ChartComponent, ColumnSeries, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
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={[ColumnSeries, Tooltip, LineSeries, Category]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' />
</SeriesCollectionDirective>
</ChartComponent>
};
export default 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 title
property to the chart to provide quick information to the user about the data plotted in the chart.
import { Category, ChartComponent, ColumnSeries, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const primaryxAxis = { valueType: 'Category' };
return <ChartComponent id="charts" primaryXAxis={primaryxAxis} title='Sales Analysis'>
<Inject services={[ColumnSeries, Tooltip, LineSeries, Category]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales'/>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
import { AxisModel,Category,ChartComponent, ColumnSeries, Inject, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const primaryxAxis: AxisModel = { valueType: 'Category' };
return <ChartComponent id="charts" primaryXAxis={primaryxAxis}
title='Sales Analysis'>
<Inject services={[ColumnSeries, Tooltip, LineSeries, Category]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' />
</SeriesCollectionDirective>
</ChartComponent>
};
export default 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, ColumnSeries, Inject, Legend, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const legendSettings = { visible: true };
const primaryxAxis = { valueType: 'Category' };
return <ChartComponent id="charts" primaryXAxis={primaryxAxis} legendSettings={legendSettings}>
<Inject services={[ColumnSeries, Tooltip, Legend, LineSeries, Category]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales'/>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
import { AxisModel, Category, ChartComponent, ColumnSeries, Inject, Legend, LegendSeriesModel, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
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={[ColumnSeries, Tooltip, Legend, LineSeries, Category]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' />
</SeriesCollectionDirective>
</ChartComponent>
};
export default 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
.
import { Category, ChartComponent, ColumnSeries, DataLabel, Inject, Legend, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
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={[ColumnSeries, DataLabel, Tooltip, Legend, LineSeries, Category]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker}/>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
import {
AxisModel, Category, ChartComponent, ColumnSeries, DataLabel, Inject, Legend,
LegendSeriesModel, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip
} from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const primaryyAxis: AxisModel = { labelFormat: '${value}K' };
const primaryxAxis: AxisModel = { valueType: 'Category' };
const legendSettings: LegendSeriesModel = { visible: true };
const marker = { dataLabel: { visible: true } };
return <ChartComponent id="charts" primaryXAxis={primaryxAxis}
legendSettings={legendSettings} primaryYAxis={primaryyAxis}>
<Inject services={[ColumnSeries, DataLabel, Tooltip, Legend, LineSeries, Category]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker} />
</SeriesCollectionDirective>
</ChartComponent>
};
export default 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, ColumnSeries, DataLabel, Inject, Legend, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const tooltip = { enable: true, shared: false };
const primaryyAxis = { labelFormat: '${value}K' };
const primarxyAxis = { valueType: 'Category' };
const legendSettings = { visible: true };
const marker = { dataLabel: { visible: true } };
return <ChartComponent id="charts" primaryXAxis={primarxyAxis} legendSettings={legendSettings} primaryYAxis={primaryyAxis} tooltip={tooltip}>
<Inject services={[ColumnSeries, DataLabel, Tooltip, Legend, LineSeries, Category]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker}/>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
import {
AxisModel, Category, ChartComponent, ColumnSeries, DataLabel, Inject,
Legend, LegendSeriesModel, LineSeries, SeriesCollectionDirective, SeriesDirective, Tooltip, TooltipSettingsModel
} from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { data } from './datasource';
function App() {
const tooltip: TooltipSettingsModel = { enable: true, shared: false }
const primaryyAxis: AxisModel = { labelFormat: '${value}K' }
const primarxyAxis: AxisModel = { valueType: 'Category' }
const legendSettings: LegendSeriesModel = { visible: true }
const marker = { dataLabel: { visible: true } };
return <ChartComponent id="charts" primaryXAxis={primarxyAxis} legendSettings={legendSettings}
primaryYAxis={primaryyAxis} tooltip={tooltip}>
<Inject services={[ColumnSeries, DataLabel, Tooltip, Legend, LineSeries, Category]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={data} xName='month' yName='sales' name='Sales' marker={marker} />
</SeriesCollectionDirective>
</ChartComponent>
};
export default 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.