FOR DEVELOPERS

How to Create Bar Charts in ReactJS?

Ways to Create ReactJS Bar Chart

Reactjs is a comprehensive library for data visualization, widely used by React developers for creating interactive and visually appealing charts. Data visualization is the system of organizing data in different formats to make it easily readable and understandable. There are lots of data visualization formats to simplify data including pie charts, line charts, and many more. For the purpose of this article, we will be working on the ReactJS bar chart which is a popular and understandable form of visualizing data.

React has a lot of packages for data visualization including react-chartjs, recharts, Apex charts, biz charts, and many more. For the purpose of this article, we will be using react-chartjs 2. The React Chartjs is a comprehensive library that features different methods including bar charts, doughnuts, pie charts, and many more. It uses the canvas API to build different forms of charts.

A bar chart is an important tool for visualizing data due to its simplistic nature. Its data can easily be understood in correlation to the value it represents. The bar chart is a chart or graph that represents data in correlation to a given value. It helps to represent data flow for a given period based on certain predefined parameters. It represents data stacked vertically or horizontally in proportion to each other.

The react-chartjs 2 package excels among other packages for its easy documentation and examples.

Creating a bar chart

To work with react-chartjs, we will be using Vite to scaffold our application. Vite is a front-end tooling application for scaffolding libraries like React and vue. To get started, we will run this command on the terminal, 'npm create vite@latest'. This will unfold the latest features of Vite while bringing a few prompts that needs to be answered.

bar chart in react.webp

Next, we will be installing react-chartjs with npm with the following command, “npm install - save chart.js react-chartjs-2”. After installing this, we are ready to create ReactJS bar charts. To start, we will be changing to our new react directory via the terminal, 'cd react-barchart' then run 'code' to start the code editor. Finally, we run 'npm run dev' to start the Hot module reloading on the terminal.

The react chartjs2 provides 4 types of bar charts,

  1. Vertical Bar chart
  2. Horizontal Bar chart
  3. Stacked Bar chart
  4. Grouped Bar chart

To create any type of bar chart, we need to import and register some plugins. Since we are working on the bar chart, we first import the bar component then we also import plugins necessary for creating the chart like the bar element, chart among others provided below.

With the react-chartjs, we need to register the plugins after importing them to show that they are being used for the project.

import { Bar } from "react-chartjs-2";
import { BarElement,  CategoryScale,Chart as ChartJS,Legend, LinearScale,Title, Tooltip } from "chart.js";

ChartJS.register(CategoryScale, LinearScale, BarElement,Title,Tooltip,Legend);

The bar chart is a component from react-chartjs2 and it requires two props to visualize data namely,

  1. Options
  2. Data

Vertical bar chart

Vertical bar charts are charts where bars are stacked vertically (upright facing). It is also known as simple bar charts. To create a vertical bar chart, we will be supplying the options and data props.

const option = {
  responsive: true,
  plugins: {
    legend: { position: "chartArea" },
    title: {
      display: true,
      text: "Modular Bar Chart",
    },
  },
};

const data = { labels=["Jan", "Feb", "Mar", "Apr", "May", "Jun"];, datasets: [ { label: "Product A", data: [ 20, 30, 40, 50, 60,70], backgroundColor: "green", }, { label:'Product B', data:[15,20,25,40,45,60], backgroundColor:'blue' },

],

};

export default function App() { return ( <div className="App"> <Bar options={option} data={data} /> </div> ); }

Vertical Bar Charts in ReactJS.webp

Running this code, we get a vertical bar chart which represents our data, it contains a few options to adjust its orientation.

  1. Responsive is set to true to make our chart responsive i.e adjust to device screen size.
  2. Legend is set to the top position to show a description of labels of each data at the top. Legend has other positions depending on preference including bottom, left, right, chart area.
  3. Title is set to display true that displays the title of our bar chart, its text is set to title of our bar chart.

Horizontal bar chart

Horizontal bar charts are stacked horizontally. Here is how we provided the options and data needed to create a horizontal bar chart.

const option={
  indexAxis:'y',
  elements:{
    bar:{
      borderWidth:1,
    }
  },
  responsive:true,
  plugins:{
    legend:{
      position:'right'
    },
    title:{
      display:true,
      text:' Horizontal bar chart'
    }
  }
}

const data = { labels=["Jan", "Feb", "Mar", "Apr", "May", "Jun"];, datasets: [ { label: "Product A", data: [ 20, 30, 40, 50, 60,70], backgroundColor: "green", }, { label:'Product B', data:15,20,25,40,45,60], backgroundColor:'blue' },

],

};

export default function App() { return ( <div className="App"> <Bar options={option} data={data} /> </div> ); }

Horizontal bar chart in ReactJS.webp

Stacked bar chart

Stacked bar charts are stacked against each other and it requires a scales property with the option, stacked set to true.

const option={
  indexAxis:'y',
  elements:{
    bar:{
      borderWidth:4,
    },
  },
  responsive:true,
  interaction:{
    mode:'index',
    intersect:false
  },
  scales:{
    x:{
      stacked:true
    },
    y:{
      stacked:true
    }
  },
  plugins:{
    legend:{
      position:'right'
    },
    title:{
      display:true,
      text:'Stacked bar chart'
    }
  }
}

const data = { labels=["Jan", "Feb", "Mar", "Apr", "May", "Jun"];, datasets: [ { label: "Product A", data: [20,30,40, 50, 60,70], backgroundColor: "green", }, { label:'Product B', data:[15,20,25,40,45,60], backgroundColor:'blue' }, { label:'Product C', data:[10,12,14,16,18,20], backgroundColor:'orange' } ], };

export default function App() { return ( <div className="App"> <Bar options={option} data={data}/> </div> ); }

Stack bar charts in ReactJS.webp

Grouped bar chart

Grouped bar charts are grouped together relative to the kind of data provided. Similar to other charts, we will be providing the options and data needed for the chart. It also has the scales property where we set the stacked option to true.

const optionX={
  indexAxis:'y',
  elements:{
    bar:{
      borderWidth:4,
    },
  },
  responsive:true,
  interaction:{
    mode:'index',
    intersect:false
  },
  scales:{
    x:{
      stacked:true
    },
    y:{
      stacked:true
    }
  },
  plugins:{
    legend:{
      position:'right'
    },
    title:{
      display:true,
      text:'Grouped bar chart'
    }
  }
}

const data = { labels=["Jan", "Feb", "Mar", "Apr", "May", "Jun"];, datasets: [ { label: "Product A", data:[20,30,40,50,60,70], backgroundColor: "green", }, { label:'Product B', data:[15,20,25,40,45,60], backgroundColor:'blue' }, { label:'Product C', data:[10,12,14,16,18,20], backgroundColor:'orange' } ], };

export default function App() { return ( <div className="App"> <Bar options={option} data={data}/> </div> ); }

Stack bar charts in ReactJS.webp

Conclusion

The react-chartjs2 provides a brilliant implementation of charts, especially the ReactJS bar chart. It is so easy to understand and work with. It also features different props that can be used based on the requirement of the chart being worked on.

Author

  • How to Create Bar Charts in ReactJS?

    David Ikukoyi

    David is a technical writer and front end developer. He has successfully published several articles on web technologies and worked with teams in creating scalable softwares. He has contributed to open source projects and also specializes in frameworks like angular, react, node among other technologies.

Frequently Asked Questions

React has many packages for creating bar charts but the simplest way is with react-chartjs2 as it is easy to implement and has good documentation.

React-chartjs2 is very easy to implement and has a very good documentation which thoroughly explains how to implement it.

It provides 4 different types of bar chart including,

  1. Horizontal bar chart
  2. Vertical bar chart
  3. Stacked bar chart
  4. Grouped bar chart
View more FAQs
Press

Press

What’s up with Turing? Get the latest news about us here.
Blog

Blog

Know more about remote work. Checkout our blog here.
Contact

Contact

Have any questions? We’d love to hear from you.

Hire remote developers

Tell us the skills you need and we'll find the best developer for you in days, not weeks.