Create a Brush 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).
Brush Bar charts are those bar charts that have a large number of data points. So to view and analyze them efficiently, there is a slider down them that helps the viewer to select some data points that the viewer needs to be displayed.
Approach: We create a normal bar chart using BarChart and Bar component of recharts npm package. To convert it to Brush bar chart we add Brush component to BarChart 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.
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: In this example, we will create a basic bar chart using BarChart component. To convert it to Brushed chart we will add Brush Component inside BarChart component.
Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
Step to Run Application: Run the application using the following command from the root directory of the project:
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Example 2: In this example, in order to add more categories of data, we will add more Bar components and add a Tooltip that shows data related to the bar on hover using the Tooltip component of recharts npm package. We can even use negative values and make reference lines using the Reference line component of recharts npm package.
Now change the following code in the App.js file.
Output: Save the file using CTRL+S. Now open your browser and go to http://localhost:3000/, you will see the following output: