React donut chart tutorial; Graphs and charts play a massive role in illustrating complex data. Charts help you understand the data. You can eloquently describe the information in numerical terms with the help of charts.
In this guide, we will help you understand how to create Pie chart in React application. To build the Pie chart in React, we will take the help of the Google charts library. Google charts offer a JavaScript-based charts plugin that helps you create any chart within a shorter time.
Pie charts display a parts-to-whole relationship for categorical or nominal data. Every slice in the pie ideally describes percentages of the whole.
In the first step, we will show you how to build a new React project.
Here is the command that needs to be executed from command prompt.
npx create-react-app react-chart
After using the above command, move inside the project folder.
cd react-chart
If you want to use custom CSS elements then you may install Bootstrap package.
npm install bootstrap
After that, go to App.js and import the bootstrap path in the file.
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
In this step, you have to install the React google charts package. You have to execute the provided command from the terminal.
# npm
npm install react-google-charts
# yarn
yarn add react-google-charts
Next, create the components/ directory and the PieChart.js file.
You may use the Chart directive to define the pie chart, you may customize the donut chart based on the options provided by the plugin.
Insert given code in components/PieChart.js file.
import React, { Component } from 'react'
import Chart from 'react-google-charts'
const pieData = [
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7],
]
const pieOptions = {
title: 'My Daily Activities',
pieHole: 0.4,
}
class PieChart extends Component {
render() {
return (
<div className="container mt-5">
<h2>React Donut Chart Example</h2>
<Chart
width={'600px'}
height={'320px'}
chartType="PieChart"
loader={<div>Loading Chart</div>}
data={pieData}
options={pieOptions}
rootProps={{ 'data-testid': '3' }}
/>
</div>
)
}
}
export default PieChart
Further, in the last step, we have to get into the App.js file.
In this file we have to import and register the pie chart component as given below.
import React from 'react'
import '../node_modules/bootstrap/dist/css/bootstrap.min.css'
import PieChart from './components/PieChart'
function App() {
return (
<div className="App">
<PieChart />
</div>
)
}
export default App
Finally, we need to run the application. It is easy to start the React app, just execute the suggested command.
npm start
In this tutorial, we have learned how to use React Google Charts library to build the Pie or Donut chart component in React.
However, you may also create a simple pie chart, 3d pie chart, rotating pie chart, and exploding pie chart. Click here to get the further assistance related to pie chart.
If you want to learn how to get the current route's name, component pathname or…
React show and hide example. In this tutorial, we will show you how to step…
Tabs are one of the best UI elements for displaying multiple contents in a single…
In this tutorial, we will learn how to create a toast notification component in the…
Bootstrap offers tons of custom UI solutions. On top of that, it is easy to…
React js counter using the useReducer hook example. In this post, we will learn how…