How to create a Brush Bar Chart using Recharts?

How to create a Brush Bar Chart using Recharts?

Rechart JS is a library that is used for creating charts for React JS. So, in this article, we will see how to create a Brush Bar Chart using Recharts.

How to create a Brush Bar Chart using Recharts?

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. It helps the viewer to select some data points that the viewer needs to be displayed. We create a normal bar chart using the BarChart and Bar component of recharts npm package. To convert it to the Brush bar chart we add the Brush component to the BarChart component.

Creating React Application And Installing Module:

1) Create a React application using the following command.

npx create-react-app foldername

2) After creating your project folder i.e. folder name, move to it using the following command.

cd foldername

3) After creating the ReactJS application, Install the required modules using the following command.

npm install --save recharts

Example 1:

In this example, we will create a basic bar chart using the BarChart component. To convert it to a Brushed chart we will add Brush Component inside the 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.

import React from 'react';
import { BarChart, Bar, Brush, XAxis, 
  YAxis, CartesianGrid} from 'recharts';

const App = () => {

// Sample data
const 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;

Run the application using the following command from the root directory of the project:

npm start

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.

import React from 'react';
import { BarChart, Bar, Brush, ReferenceLine, 
  XAxis, YAxis, CartesianGrid, Tooltip} from 'recharts';

const App = () => {

// Sample data
const 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;

Conclusion:

So, in this article, we have been through how to create a Brush Bar Chart using Recharts. Also, feel free to comment with your suggestions and feedback on the post. Moreover, at BOSC Tech Labs, we have a team of highly experienced React JS developers. They can assist you in developing your customized web app. So contact us to hire experienced  ReactJS developers.

Get in touch






    Stay up-to-date with our blogs