Create a Brush Bar Chart using Recharts in ReactJS - GeeksforGeeks

19 Aug, 2021
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.
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: 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.
Javascript
import React from 'react';import { BarChart, Bar, Brush, XAxis,     YAxis, CartesianGrid} from 'recharts';  const App = () => {  // Sample dataconst data = [  {name:'A', x:861},  {name:'B', x:862},  {name:'C', x:343},  {name:'D', x:454},  {name:'E', x:435},  {name:'F', x:653},  {name:'G', x:734},  {name:'H', x:845},  {name:'I', x:932},  {name:'J', x:133},  {name:'K', x:222},  {name:'L', x:332},  {name:'M', x:554},  {name:'N', x:554},  {name:'O', x:633},  {name:'P', x:722},  {name:'Q', x:638},  {name:'R', x:229},  {name:'S', x:321},  {name:'T', x:222},  {name:'U', x:573},  {name:'V', x:464},  {name:'W', x:565},  {name:'X', x:656},  {name:'Y', x:764},  {name:'Z', x:348},];  return (  <BarChart width={500} height={700} data={data} >      <CartesianGrid/>      <XAxis dataKey="name" />      <YAxis />      <Brush dataKey="name" height={30} stroke="#8884d8" />      <Bar dataKey="x" fill="green" />  </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, 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.
Javascript
import React from 'react';import { BarChart, Bar, Brush, ReferenceLine,     XAxis, YAxis, CartesianGrid, Tooltip} from 'recharts';  const App = () => {  // Sample dataconst data = [  {name:'A', x:21, y:23,  z:122},  {name:'B', x:22, y:-3,   z:-73},  {name:'C', x:-32, y:15,  z:32},  {name:'D', x:-14, y:-35,  z:23},  {name:'E', x:-51, y:45,  z:-20},  {name:'F', x:16, y:-25,  z:29},  {name:'G', x:7, y:-17,  z:-61},  {name:'H', x:-8, y:32,  z:45},  {name:'I', x:9, y:43,  z:-93},];  return (  <BarChart width={500} height={700} data={data} >          <CartesianGrid/>          <XAxis dataKey="name" />          <YAxis />          <Tooltip />          <ReferenceLine y={0} stroke="gray" />          <Brush dataKey="name" height={30} stroke="green" />          <Bar dataKey="x" fill="red" />          <Bar dataKey="y" fill="blue" />          <Bar dataKey="z" fill="yellow" />        </BarChart>);}  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
Reference: https://recharts.org/en-US/examples
React-Questions
Recharts
JavaScript
ReactJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
How to fetch data from an API in ReactJS ?
How to redirect to another page in ReactJS ?
How to pass data from child component to its parent in ReactJS ?
How to pass data from one component to other component in ReactJS ?
ReactJS Functional Components