Search results

Getting started with React Stock Chart component

This section explains you the steps required to create a simple Stock Chart and demonstrate the basic usage of the Stock 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
    |-- @syncfusion/ej2-navigations
    |-- @syncfusion/ej2-calendars
    |-- @syncfusion/ej2-popups
    |-- @syncfusion/ej2-lists
    |-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-buttons
    |-- @syncfusion/ej2-splitbuttons

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
create-react-app quickstart

cd quickstart
  • Install Syncfusion packages using below command.
npm install @syncfusion/ej2-react-charts --save

Add Stock 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 {StockChartComponent} from '@syncfusion/ej2-react-charts';
import * as React from 'react';

class  App  extends  React.Component  {

  render() {
    return  (<StockChartComponent />);
  }

}
export  default  App;
import { StockChartComponent } from '@syncfusion/ej2-react-charts';
import * as React from 'react';
class App extends React.Component {
    render() {
        return (<StockChartComponent />);
    }
}
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
index.jsx
import { StockChartComponent  } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";

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

  render() {
    return <StockChartComponent  id='stockcharts'>
             <StockChartSeriesCollectionDirective> </StockChartSeriesCollectionDirective>
            </StockChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("stockcharts"));
import { StockChartComponent } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
class App extends React.Component {
    render() {
        return <StockChartComponent id='stockcharts'>
             <StockChartSeriesCollectionDirective> </StockChartSeriesCollectionDirective>
            </StockChartComponent>;
    }
}
;
ReactDOM.render(<App />, document.getElementById("stockcharts"));

Module Injection

Stock 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 stock value of a company. For this application we are going to use candle series, tooltip, data label, DateTime axis feature of the chart. Please find the relevant feature service name and description as follows.

  • CandleSeries - Inject this module in to services to use candle series.
  • Tooltip - Inject this module in to services to use tooltip feature.
  • DataLabel - Inject this module in to services to use datalabel feature.
  • DateTime - Inject this module in to services to use DateTime feature.

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

import { DateTime, StockChartComponent, DataLabel, CandleSeries, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";

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

  render() {
    return <StockChartComponent id='stockcharts'>
      <Inject services={[CandleSeries, Tooltip, DataLabel, Category]} />
    </StockChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));
import { StockChartComponent, DataLabel, CandleSeries, Tooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
class App extends React.Component {
    render() {
        return <StockChartComponent id='stockcharts'>
      <Inject services={[CandleSeries, Tooltip, DataLabel, Category]}/>
    </StockChartComponent>;
    }
}
;
ReactDOM.render(<App />, document.getElementById("charts"));

Populate Chart with Data

This section explains how to plot below JSON data to the Stock Chart.Please find the below imported datasource.

export let data: any[] = [{
            "x": new Date('2012-04-02T00:00:00.000Z'),
            "open": 320.705719,
            "high": 324.074066,
            "low": 317.737732,
            "close": 323.783783,
            "volume": 45638000
        }, {
            "x": new Date('2012-04-03T00:00:00.000Z'),
            "open": 323.028015,
            "high": 324.299286,
            "low": 319.639648,
            "close": 321.631622,
            "volume": 40857000
        }, {
            "x": new Date('2012-04-04T00:00:00.000Z'),
            "open": 319.544556,
            "high": 319.819824,
            "low": 315.865875,
            "close": 317.892883,
            "volume": 32519000
        }, {
            "x": new Date('2012-04-05T00:00:00.000Z'),
            "open": 316.436432,
            "high": 318.533539,
            "low": 314.599609,
            "close": 316.476471,
            "volume": 46327000
        }]
export let data = [{
        "x": new Date('2012-04-02T00:00:00.000Z'),
        "open": 320.705719,
        "high": 324.074066,
        "low": 317.737732,
        "close": 323.783783,
        "volume": 45638000
    }, {
        "x": new Date('2012-04-03T00:00:00.000Z'),
        "open": 323.028015,
        "high": 324.299286,
        "low": 319.639648,
        "close": 321.631622,
        "volume": 40857000
    }, {
        "x": new Date('2012-04-04T00:00:00.000Z'),
        "open": 319.544556,
        "high": 319.819824,
        "low": 315.865875,
        "close": 317.892883,
        "volume": 32519000
    }, {
        "x": new Date('2012-04-05T00:00:00.000Z'),
        "open": 316.436432,
        "high": 318.533539,
        "low": 314.599609,
        "close": 316.476471,
        "volume": 46327000
    }];

Add a series object to the chart by using series property and then set the JSON data to dataSource property.

Since the JSON contains DateTime data, set the valueTypefor horizontal axis to DateTime. By default, the axis valueType is Numeric.

Source
Preview
index.tsx
index.jsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, CrosshairSettingsModel,FontModel }
from'@syncfusion/ej2-react-charts';
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { chartData } from 'datasource.ts';

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

 render() {
    return <StockChartComponent  id="charts"
     <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines,EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export,       AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
      <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective  dataSource={chartData} type='Candle'  xName='date' high='high' low='low' open='open' close='close'name='Apple' />
      </StockChartSeriesCollectionDirective>
    </StockChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { chartData } from 'datasource.ts';
class App extends React.Component {
    render() {
        return <StockChartComponent id="charts"/>
            ,
                <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines, EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
                    ,
                        <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective dataSource={chartData} type='Candle' xName='date' high='high' low='low' open='open' close='close' name='Apple'/>
      </StockChartSeriesCollectionDirective>;
        StockChartComponent >
        ;
    }
}
;
ReactDOM.render(<App />, document.getElementById("charts"));

Add Stock Chart Title

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

Source
Preview
index.tsx
index.jsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, CrosshairSettingsModel,FontModel }
from'@syncfusion/ej2-react-charts';
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { chartData } from 'datasource.ts';

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

 render() {
    return <StockChartComponent id="charts"
      title='AAPL Stock Price'>
     <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines,EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export,AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
      <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective  dataSource={chartData} xName='date' high='high' low='low' type='Candle' Open='open' Close='close' />
      </StockChartSeriesDirective >
    </StockChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { chartData } from 'datasource.ts';
class App extends React.Component {
    render() {
        return <StockChartComponent id="charts" title='AAPL Stock Price'>
     <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines, EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
      <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective dataSource={chartData} xName='date' high='high' low='low' type='Candle' Open='open' Close='close'/>
      </StockChartSeriesDirective>
    </StockChartComponent>;
    }
}
;
ReactDOM.render(<App />, document.getElementById("charts"));

Add Crosshair

Crosshair has a vertical and horizontal line to view the value of the axis at mouse or touch position.

Crosshair lines can be enabled by using enable property in the crosshair. Likewise tooltip label for an axis can be enabled by using enable property of crosshairTooltip in the corresponding axis.

Source
Preview
index.tsx
index.jsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, CrosshairSettingsModel,FontModel }
from'@syncfusion/ej2-react-charts';
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { chartData } from 'datasource.ts';

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

  public primaryxAxis: AxisModel= {
            valueType: 'DateTime',
            majorGridLines: { width: 0 },
            majorTickLines: { color: 'transparent' }
    };
    public primaryyAxis: AxisModel= {
            majorTickLines: { width: 0 }
    };
    public crosshair: CrosshairSettingsModel ={ enable: true } ;


render() {
    return <StockChartComponent id="charts"
     primaryXAxis={ this.primaryxAxis }
     primaryYAxis={ this.primaryyAxis }
     crosshair={this.crosshair}
      title='AAPL Stock Price'>
     <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines,EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export,AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
      <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective  dataSource={chartData} xName='date' high='high' low='low' type='Candle' Open='open' Close='close' />
      </StockChartSeriesDirective >
    </StockChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { chartData } from 'datasource.ts';
class App extends React.Component {
    constructor() {
        super(...arguments);
        this.primaryxAxis = {
            valueType: 'DateTime',
            majorGridLines: { width: 0 },
            majorTickLines: { color: 'transparent' }
        };
        this.primaryyAxis = {
            majorTickLines: { width: 0 }
        };
        this.crosshair = { enable: true };
    }
    render() {
        return <StockChartComponent id="charts" primaryXAxis={this.primaryxAxis} primaryYAxis={this.primaryyAxis} crosshair={this.crosshair} title='AAPL Stock Price'>
     <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines, EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
      <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective dataSource={chartData} xName='date' high='high' low='low' type='Candle' Open='open' Close='close'/>
      </StockChartSeriesDirective>
    </StockChartComponent>;
    }
}
;
ReactDOM.render(<App />, document.getElementById("charts"));

Add Trackball

Trackball is used to track a data point closest to the mouse or touch position. Trackball marker indicates the closest point and trackball tooltip displays the information about the point. To use trackball feature, we need to inject Crosshair and Tooltip module into the services.

Trackball can be enabled by setting the enable property of the crosshair to true and shared property in tooltip to true in chart.

Source
Preview
index.tsx
index.jsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, CrosshairSettingsModel,FontModel }
from'@syncfusion/ej2-react-charts';
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, TooltipSettingsModel,HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { trackData } from  'datasource.ts';

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

  public primaryxAxis: AxisModel = {
    title: 'Years', minimum: new Date(2000, 1, 1),
    maximum: new Date(2006, 2, 11), intervalType: 'Years', valueType: 'DateTime'
  };
  public primaryyAxis: AxisModel = { crosshairTooltip: { enable: true, fill: 'green' } };
  public crosshair: CrosshairSettingsModel = { enable: true, lineType: 'Vertical' };
  public marker = { visible: true };
  public tooltip: TooltipSettingsModel = { enable: true, shared: true, format: '${series.name} : ${point.x} : ${point.y}' };

  render() {
    return <StockChartComponent id='charts'
      primaryXAxis={this.primaryxAxis}
      crosshair={this.crosshair}
      tooltip={this.tooltip}
      title='Average Sales per Person'>
     <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines,EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
      <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y' name='John' type='Line' width={2}
          marker={this.marker} >
        </StockChartSeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y1' name='Andrew' type='Line' width={2}
          marker={this.marker} ></StockChartSeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y2' name='Thomas' type='Line' width={2}
          marker={this.marker} ></StockChartSeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y3' name='Mark' type='Line' width={2}
          marker={this.marker} ></SeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y4' name='William' type='Line' width={2}
          marker={this.marker} ></StockChartSeriesDirective>
      </StockChartSeriesCollectionDirective>
    </StockChartComponent>
  }

};
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { StockChartComponent, StockChartSeriesCollectionDirective, StockChartSeriesDirective, Inject, DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines } from '@syncfusion/ej2-react-charts';
import { EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator, Export } from '@syncfusion/ej2-react-charts';
import { trackData } from 'datasource.ts';
class App extends React.Component {
    constructor() {
        super(...arguments);
        this.primaryxAxis = {
            title: 'Years', minimum: new Date(2000, 1, 1),
            maximum: new Date(2006, 2, 11), intervalType: 'Years', valueType: 'DateTime'
        };
        this.primaryyAxis = { crosshairTooltip: { enable: true, fill: 'green' } };
        this.crosshair = { enable: true, lineType: 'Vertical' };
        this.marker = { visible: true };
        this.tooltip = { enable: true, shared: true, format: '${series.name} : ${point.x} : ${point.y}' };
    }
    render() {
        return <StockChartComponent id='charts' primaryXAxis={this.primaryxAxis} crosshair={this.crosshair} tooltip={this.tooltip} title='Average Sales per Person'>
     <Inject services={[DateTime, Tooltip, RangeTooltip, Crosshair, LineSeries, SplineSeries, CandleSeries, HiloOpenCloseSeries, HiloSeries, RangeAreaSeries, Trendlines, EmaIndicator, RsiIndicator, BollingerBands, TmaIndicator, MomentumIndicator, SmaIndicator, AtrIndicator, Export, AccumulationDistributionIndicator, MacdIndicator, StochasticIndicator]}/>
      <StockChartSeriesCollectionDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y' name='John' type='Line' width={2} marker={this.marker}>
        </StockChartSeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y1' name='Andrew' type='Line' width={2} marker={this.marker}></StockChartSeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y2' name='Thomas' type='Line' width={2} marker={this.marker}></StockChartSeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y3' name='Mark' type='Line' width={2} marker={this.marker}></SeriesDirective>
        <StockChartSeriesDirective dataSource={trackData} xName='x' yName='y4' name='William' type='Line' width={2} marker={this.marker}></StockChartSeriesDirective>
      </StockChartSeriesCollectionDirective>
    </StockChartComponent>;
    }
}
;
ReactDOM.render(<App />, document.getElementById("charts"));