Friday, October 17, 2025
HomeLanguagesJavascriptCreate a Stacked Area Chart using Recharts in ReactJS

Create a Stacked Area Chart using Recharts in ReactJS

RechartJS is a library that is used for creating charts for ReactJS. This library is used for building Line charts, Bar charts, Pie charts, etc with the help of React and D3 (Data-Driven Documents).  

Stacked Area Chart is the extension of a basic area chart. It displays various continuous data in the same area chart for a better comparison of data. An area chart combines the line chart and bar chart to demonstrate numeric value deviation over a certain progressive variable.

Approach: To create a Stacked Area Chart we use AreaChart component of recharts npm package. We firstly create a cartesian grid and X-axis and Y-Axis. Then add multiple Area chart using Area component and get them stacked on top of each other. We use the same stackId for all charts.

 

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. foldername, 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: Here, we will create a basic stacked Area chart using AreaChart and Area component of recharts npm package. To stack the two area chart component on top of each other we will add the same stackId to both Area components.

Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

Javascript




import React from 'react';
import {AreaChart, Area, XAxis, YAxis, 
    CartesianGrid, Tooltip} from 'recharts';
   
const App = () => {
  
    // Sample data
    const data = [
        {name:"A", x:30, y:70},
        {name:"B", x:12, y:88},
        {name:"C", x:15, y:85},
        {name:"D", x:35, y:65},
        {name:"E", x:54, y:46},
        {name:"F", x:72, y:28},
        {name:"G", x:32, y:68}
    ];
   
   
    return (
    <AreaChart width={500} height={700} data={data}>
        <CartesianGrid/>
        <XAxis dataKey="name" />
        <YAxis />
        <Tooltip />
        <Area dataKey="x" stackId="1" 
            stroke="green" fill="green" />
        <Area dataKey="y" stackId="1" 
            stroke="blue" fill="blue" />
    </AreaChart>
    );
}
   
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:

Example 2: Here, to convert this from an area chart to a curved graph we will set the type property to monotone. And if we want area charts to stack on top of each other and third to overlap them instead of stacking we will give different stack id to one which needs to overlap and the same to those which have to be stacked.

Now change the following code in the App.js file. 

Javascript




import React from 'react';
import {AreaChart, Area, XAxis, YAxis, 
    CartesianGrid } from 'recharts';
   
const App = () => {
  
    // Sample data
    const data = [
        {name:'A', x:12, y:23,  z:122},
        {name:'B', x:22, y:3,   z:73},
        {name:'C', x:13, y:15,  z:32},
        {name:'D', x:42, y:35,  z:23},
        {name:'E', x:51, y:45,  z:20},
        {name:'F', x:16, y:25,  z:29},
        {name:'G', x:17, y:17,  z:61},
        {name:'H', x:81, y:32,  z:45},
        {name:'I', x:19, y:43,  z:93},
    ];
   
    return (
    <AreaChart width={500} height={700} data={data}>
        <CartesianGrid/>
        <XAxis dataKey="name" />
        <YAxis />
        <Area type="monotone" dataKey="x" 
            stackId="1" stroke="black" fill="black" />
        <Area type="monotone" dataKey="y" 
            stackId="1" stroke="blue" fill="blue" />
        <Area type="monotone" dataKey="z" 
            stackId="2" stroke="green" fill="green" />
    </AreaChart>
    );
}
   
export default App;


Output: Save the project 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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS