Create a BiAxial Line Chart using Recharts in ReactJS
Introduction: Rechart JS is a library that is used for creating charts for React JS. This library is used for building Line charts, Bar charts, Pie charts, etc, with the help of React and D3 (Data-Driven Documents).
BiAxial line chart is a Line chart that has two Y-Axis instead of one. It is used to compare two different series of data points with different Y-Axis.
Approach: To create a Biaxial Line chart in react using recharts, we firstly create a data variable with all the data points and labels. Then we create a cartesian grid and all three axes i.e. one X-Axis and two Y-Axis. Also, add y-axisId to both Y-Axis so that they can be referenced while plotting the Line chart. Finally, point the line chart around it using the Line component from recharts npm package.
Syntax for Creating React Application And Installing Module:
Step 1: Create a React application using the following command.
npx create-react-app foldername
Step 2: After creating your project folder i.e. folder name, move to it using the following command.
Step 3: After creating the ReactJS application, Install the required modules using the following command.
npm install --save recharts
Project Structure: It will look like the following.
Example 1: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
Step to Run Application: Run the application using the following command from the root directory of the project:
Output: Now open your browser and go to
http://localhost:3000/, you will see the following output:
Example 2: Now change the following code in the App.js file.
Output: Save the code using CTRL+S . Now open your browser and go to http://localhost:3000/, you will see the following output: