Open In App

Create a Stacked Bar Chart using Recharts in ReactJS

Improve
Improve
Like Article
Like
Save
Share
Report

Creating a Stacked Bar Chart using Recharts in ReactJS is an important aspect of visualizing the data in the React application. 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). A stacked Bar Chart is the extension of a basic bar chart. It displays various discrete data in the same bar chart for a better comparison of data. 

Prerequisites

Approach

To create a Stacked Bar Chart we use the BarChart component of recharts npm package. We first create a cartesian grid and X-axis and Y-axis. Then add multiple Bar charts using the Bar component and to get them stacked on top of each other use the same stackId for all charts.

Creating React Application

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 i --save recharts

Project Structure

It will look like the following.

Dependencies list after installing packages

{
"dependencies": {
"@material-ui/core": "^4.12.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"recharts": "^2.9.0",
"web-vitals": "^2.1.4"
}
}

Example 1: Create a basic stacked bar chart using BarChart and Bar component of recharts npm package. To stack the two bars on top of each other we will add same stackId to both Bar 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




// Filename - App.js
 
import React from 'react';
import { BarChart, Bar, 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: 44, y: 35, z: 23 },
        { name: 'E', x: 35, y: 45, z: 20 },
        { name: 'F', x: 62, y: 25, z: 29 },
        { name: 'G', x: 37, y: 17, z: 61 },
        { name: 'H', x: 28, y: 32, z: 45 },
        { name: 'I', x: 19, y: 43, z: 93 },
    ];
 
    return (
        <BarChart width={500} height={500} data={data} >
            <CartesianGrid />
            <XAxis dataKey="name" />
            <YAxis />
            <Bar dataKey="x" stackId="a" fill="#8884d8" />
            <Bar dataKey="y" stackId="a" fill="#82ca9d" />
        </BarChart>
    );
}
 
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: In this example, we will change the color of bars using fill property. To add a tooltip that will display information about bar on hover and legend that will show labels for stacked bars, we will use Tooltip component and Legend component. Now change the following code in the App.js file. 

Javascript




// Filename - App.js
 
import React from 'react';
import { BarChart, Bar, XAxis, YAxis, CartesianGrid,
        Legend, 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 (
        <BarChart width={500} height={500} data={data} >
            <CartesianGrid />
            <XAxis dataKey="name" />
            <YAxis />
            <Tooltip />
            <Legend />
            <Bar dataKey="x" stackId="a" fill="aqua" />
            <Bar dataKey="y" stackId="a" fill="green" />
        </BarChart>
    );
}
 
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



Last Updated : 30 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads