Thursday, December 26, 2024
Google search engine
HomeUncategorisedCreate a BiAxial Line Chart using Recharts in ReactJS

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.

cd foldername

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.

App.js




import React from 'react';
import { LineChart, Line, XAxis, YAxis, CartesianGrid} from 'recharts';
  
  
const App = () => {
  
// Sample data
const data = [
  {x:1, y:23,  z:122},
  {x:2, y:3,   z:73},
  {x:3, y:15,  z:32},
  {x:4, y:35,  z:23},
  {x:5, y:45,  z:20},
  {x:6, y:25,  z:29},
  {x:7, y:17,  z:61},
  {x:8, y:32,  z:45},
  {x:9, y:43,  z:93},
];
  
  
return (
        <LineChart width={500} height={700} data={data}>
          <CartesianGrid/>
          <XAxis dataKey="x" />
          <YAxis yAxisId="left-axis" />
          <YAxis yAxisId="right-axis" orientation="right" />
          <Line yAxisId="left-axis" type="monotone" dataKey="y" 
          stroke="green"/>
          <Line yAxisId="right-axis" type="monotone" dataKey="z" 
          stroke="red" />
        </LineChart>
);
}
  
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to

 http://localhost:3000/, you will see the following output:

Output

Example 2: Now change the following code in the App.js file.

App.js




import React from 'react';
import { LineChart, Line, XAxis, YAxis, CartesianGrid} from 'recharts';
  
  
const App = () => {
  
// Sample data
const data = [
  {x:1, y:123,  z:122},
  {x:2, y:113,   z:713},
  {x:3, y:125,  z:312},
  {x:4, y:235,  z:123},
  {x:5, y:145,  z:420},
  {x:6, y:25,  z:529},
  {x:7, y:117,  z:61},
  {x:8, y:32,  z:435},
  {x:9, y:143,  z:93},
];
  
  
return (
        <LineChart width={500} height={700} data={data}>
          <CartesianGrid strokeDasharray="3 3" />
          <XAxis dataKey="x" />
          <YAxis yAxisId="left-axis" />
          <YAxis yAxisId="right-axis" orientation="right" />
          <Line yAxisId="left-axis" type="dashed" dataKey="y" 
           stroke="pink"/>
          <Line yAxisId="right-axis" type="dashed" dataKey="z" 
          stroke="blue" />
        </LineChart>
);
}
  
export default App;


Output: Save the code using CTRL+S . Now open your browser and go to http://localhost:3000/, you will see the following output:

Output

Reference: https://recharts.org/en-US/examples

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments