Open In App

How to create Doughnut chart in react using material UI and DevExpress ?

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A doughnut chart represents a circular piechart-like visualization of data in a round figure with different colors or part of the circle represents an element with the area covered respective to their values.

DevExpress: DevExpress is a package for controlling and building the user interface of Windows, Mobile, and other applications.

Doughnut Charts: Doughnut charts are the modified version of Pie Charts with the area of center cut out. A donut is more concerned about the use of an area of arcs to represent the information in the most effective manner instead of a Pie chart which is more focused on comparing the proportion area between the slices.

Prerequisites

Approach

To create a Doughnut chart in React using material UI and DevExpress, we will install the devexpress and MUI packages first. Then create dummy data in the form of an array of objects with numeric values and represent them on the doughnut chart using the Chart component and respective attributes.

Steps to create React Application

Step 1: Create a React application using the following command.

npx create-react-app foldername

Step 2: After creating your project folder i.e. folder name, 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 i --save @devexpress/dx-react-core @devexpress/dx-react-chart @material-ui/core 
@devexpress/dx-react-chart-material-ui

Project Structure:

Project Structure

The updated dependencies in package.json file will look like:

{
"dependencies": {
"@devexpress/dx-react-chart": "^4.0.5",
"@devexpress/dx-react-chart-material-ui": "^4.0.5",
"@devexpress/dx-react-core": "^4.0.5",
"@material-ui/core": "^4.12.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}

Example: Used the chart component form teh dev express to visualise the data as a doughnut chart.

Javascript




// Filename - App.js
 
import React from "react";
import Paper from '@material-ui/core/Paper';
import {
  Chart,
  PieSeries,
  Title
} from '@devexpress/dx-react-chart-material-ui';
 
 
const App = () => {
 
// Sample data
const data = [
  { argument:'Monday', value:10 },
  { argument:'Tuesday', value:40 },
  { argument:'Wednesday', value:10 },
  { argument:'Thursday', value:20 },
  { argument:'Friday', value:20 },
];
return (
    <Paper>
    <Chart
      data={data}
    >
      <PieSeries valueField="value"
        argumentField="argument"
        innerRadius={0.6} />
      <Title text="Studies per day"/>
    </Chart>
  </Paper>
);
}
 
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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads