Wednesday, July 3, 2024
HomeLanguagesJavascriptCreate a Radial Bar Chart using Recharts in ReactJS

Create a Radial Bar 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).  

Radial Bar chart is a categorical bar chart that is displayed in polar coordinates. It is also known as a circular bar chart. It is used to show comparisons among categorical data by using a circular shape plot.

Approach: To create Radial Bar chart in react using recharts, we use RadialBarChart component of Recharts npm package. To create bar’s in the chart, we use RadialBar component.

 

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: 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 { RadialBarChart, RadialBar } from 'recharts';
  
  
const App = () => {
  
// Sample data
const data = [
  {name:'A', x:1,fill:"green"},
  {name:'B', x:2, fill:"yellow"},
  {name:'C', x:3, fill:"aqua"},
  {name:'D', x:4, fill: "blue"},
  {name:'E', x:5, fill:"orange"},
  {name:'F', x:6, fill:"red"},
  {name:'G', x:7, fill:"black"},
  {name:'H', x:8, fill:"purple"},
  {name:'I', x:9, fill:"gray"},
];
  
  
return (
  <RadialBarChart width={500} height={500} data={data}>
    <RadialBar minAngle={15} dataKey="x"/>
  </RadialBarChart>
);
}
  
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 { RadialBarChart, RadialBar } from 'recharts';
  
  
const App = () => {
  
// Sample data
const data = [
  {
    name: 'A',
    x: 31.47,
    fill: '#8784d8',
  },
  {
    name: 'B',
    x: 26.69,
    fill: '#84a6ed',
  },
  {
    name: 'C',
    x: 15.69,
    fill: '#8ed1e1',
  },
  {
    name: 'D',
    x: 8.22,
    fill: '#82da9d',
  },
  {
    name: 'E',
    x: 8.63,
    fill: '#a2de6c',
  },
  {
    name: 'F',
    x: 2.63,
    fill: '#d0dd57',
  },
  {
    name: 'G',
    x: 6.67,
    fill: '#ffa658',
  },
];
  
  
return (
  <RadialBarChart width={500} height={500} data={data} 
    innerRadius="20%" outerRadius="70%">
    <RadialBar minAngle={30} dataKey="x" clockWise/>
  </RadialBarChart>
);
}
  
export default App;


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

Output

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments