Create a Stacked Bar Chart using Recharts in ReactJS
Last Updated :
30 Oct, 2023
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:
OutputExample 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
Similar Reads
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). Â Recharts Stacked Area Chart is the extension of a basic area chart. It displays various continuous
4 min read
Create a Bar chart using Recharts in ReactJS
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). Â To create a Bar Chart using Recharts, we create a dataset with x and y coordinate details. Then
2 min read
Create a Scatter Chart using Recharts in ReactJS
RechartJS 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). To create a Scatter chart using Recharts, we create a dataset with x and y coordinate details. Then
2 min read
Create a Brush Bar Chart using Recharts in ReactJS
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). Â In this article, we will learn how to Create a Brush Bar Chart using Recharts in ReactJS.Prerequi
4 min read
Create a Radial Bar Chart using Recharts in ReactJS
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 Radial Bar chart is a categorical bar chart that is displayed in polar coordinates. It is also
3 min read
Create an Area chart using Recharts in React JS
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). Â Prerequisites:React JSReact RechartsApproach to create Area Chart:To create an Area Chart using
2 min read
Create a Pie Chart Using Recharts in ReactJS
Rechart JS 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). Â Pie Charts:Â Pie chart is more focused on comparing the proportion area between the slices to repre
2 min read
Create a BiAxial Line Chart using Recharts in ReactJS
Y-axis Rechart JS is a library Reactused 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). Approach: To create a Biaxial Line chart in React using recharts, we first React and two Y-Axi
3 min read
Create a Line Chart using Recharts in ReactJS
This article focuses on creating Line Charts, vital for displaying trends over time. Leveraging Recharts within the React framework, you'll seamlessly integrate sophisticated charts, elevating user experience and uncovering valuable data insights. Prerequisites:Node JS or NPMReact JSRecharts ReactCr
2 min read
Create a Radar Chart using Recharts in ReactJS
Radar charts, also known as spider or star charts, provide a powerful way to display data having multiple variable in a circular layout. Recharts is a popular charting library that is used for creating charts for React JS, provides an easy and efficient method to implement radar charts within your R
2 min read