Search results

Getting Started

This section explains you the steps required to create a simple chart and demonstrate the basic usage of the chart control.

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 setup basic React sample use following commands.
create-react-app quickstart --scripts-version=react-scripts-ts

cd quickstart

npm install
  • 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';

class  App  extends  React.Component  {

  render() {
    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

he below example shows a basic Chart.

Source
Preview
index.tsx
import { ChartComponent  } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";

class App extends React.Component<{}, {}> {

  render() {
    return <ChartComponent id='charts' />
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));

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 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.

These modules should be injected to the services section as follows,

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

class App extends React.Component<{}, {}> {

 render() {
   return <ChartComponent id='charts'>
     <Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category]} />
   </ChartComponent>
 }

};
ReactDOM.render(<App />, document.getElementById("charts"));

Populate Chart with Data

This section explains how to plot below JSON data to the chart.

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 valueTypefor horizontal axis to Category. By default, the axis valueType is Numeric.

Source
Preview
index.tsx
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";

class App extends React.Component<{}, {}>{

  public 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 }
  ];
  public primaryxAxis: AxisModel = { valueType: 'Category' };

  render() {
    return <ChartComponent id="charts" primaryXAxis={this.primaryxAxis}>
      <Inject services={[ColumnSeries, Tooltip, LineSeries, Category]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={this.data} xName='month' yName='sales' name='Sales' />
      </SeriesCollectionDirective>
    </ChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));

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.

Source
Preview
index.tsx
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";

class App extends React.Component<{}, {}> {

  public 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 }
  ];
  public primaryxAxis: AxisModel = { valueType: 'Category' };
  public primaryyAxis: AxisModel = { labelFormat: '${value}K' };

  render() {
    return <ChartComponent id="charts" primaryXAxis={this.primaryxAxis}
      primaryYAxis={this.primaryyAxis}>
      <Inject services={[ColumnSeries, Tooltip, LineSeries, Category]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={this.data} xName='month' yName='sales' name='Sales' />
      </SeriesCollectionDirective>
    </ChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));

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.

Source
Preview
index.tsx
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";

class App extends React.Component<{}, {}> {

  public 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 }
  ];
  public primaryxAxis: AxisModel = { valueType: 'Category' };

  render() {
    return <ChartComponent id="charts" primaryXAxis={this.primaryxAxis}
      title='Sales Analysis'>
      <Inject services={[ColumnSeries, Tooltip, LineSeries, Category]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={this.data} xName='month' yName='sales' name='Sales' />
      </SeriesCollectionDirective>
    </ChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));

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.

Source
Preview
index.tsx
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";

class App extends React.Component<{}, {}> {

  public 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 }
  ];
  public legendSettings: LegendSeriesModel = { visible: true };
  public primaryxAxis: AxisModel = { valueType: 'Category' };

  render() {
    return <ChartComponent id="charts" primaryXAxis={this.primaryxAxis}
      legendSettings={this.legendSettings}>
      <Inject services={[ColumnSeries, Tooltip, Legend, LineSeries, Category]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={this.data} xName='month' yName='sales' name='Sales' />
      </SeriesCollectionDirective>
    </ChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));

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.

Source
Preview
index.tsx
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";

class App extends React.Component<{}, {}> {

  public 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 }
  ];
  public primaryyAxis: AxisModel = { labelFormat: '${value}K' };
  public primaryxAxis: AxisModel = { valueType: 'Category' };
  public legendSettings: LegendSeriesModel = { visible: true };
  public marker = { dataLabel: { visible: true } };

  render() {
    return <ChartComponent id="charts" primaryXAxis={this.primaryxAxis}
      legendSettings={this.legendSettings}
      primaryYAxis={this.primaryyAxis}>
      <Inject services={[ColumnSeries, DataLabel, Tooltip, Legend, LineSeries, Category]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={this.data} xName='month' yName='sales' name='Sales' marker={this.marker} />
      </SeriesCollectionDirective>
    </ChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));

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.

Source
Preview
index.tsx
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";

class App extends React.Component<{}, {}> {

  public 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 }
  ];
  public tooltip: TooltipSettingsModel = { enable: true, shared: false }
  public primaryyAxis: AxisModel = { labelFormat: '${value}K' }
  public primarxyAxis: AxisModel = { valueType: 'Category' }
  public legendSettings: LegendSeriesModel = { visible: true }
  public marker = { dataLabel: { visible: true } };

  render() {
    return <ChartComponent id="charts" primaryXAxis={this.primarxyAxis}
      legendSettings={this.legendSettings}
      primaryYAxis={this.primaryyAxis} tooltip={this.tooltip}>
      <Inject services={[ColumnSeries, DataLabel, Tooltip, Legend, LineSeries, Category]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={this.data} xName='month' yName='sales' name='Sales' marker={this.marker} />
      </SeriesCollectionDirective>
    </ChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));