React PieChart - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

The PieChart UI component visualizes data as a circle divided into portions (slices) to illustrate data proportions.

This tutorial shows how to add a PieChart to the page and configure the component's core settings. As a result, you will create the following UI component:

Each section in this tutorial describes a single configuration step. You can also find the full source code in the following GitHub repository: getting-started-with-pie-chart.

Create a PieChart

Add DevExtreme to your React application and use the following code to create a PieChart:

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { PieChart } from 'devextreme-react/pie-chart';
  •  
  • function App() {
  • return (
  • <PieChart
  • // Configuration goes here
  • >
  • </PieChart>
  • );
  • }
  •  
  • export default App;

Bind PieChart to Data and Configure Series

The PieChart can visualize data from different sources. Refer to the following demos for details on how to bind the PieChart to your data source:

This tutorial uses an array as a PieChart data source. To bind the PieChart to data, pass the array to the PieChart's dataSource property.

Once you assign the data source, specify the series type. The PieChart has two series types: the Pie (default) and Doughnut. The only difference between them is the Doughnut has a blank center.

To display data, specify the series nested options: argumentField and valueField. This allows the component to determine the corresponding object fields (arguments and values) in the array.

App.js
data.js
  • // ...
  • import { PieChart, Series } from 'devextreme-react/pie-chart';
  • import { billionaires } from './data';
  •  
  • function App() {
  • return (
  • <PieChart
  • dataSource={billionaires}
  • type="doughnut"
  • >
  • <Series
  • argumentField="country"
  • valueField="amount"
  • >
  • </Series>
  • </PieChart>
  • );
  • }
  •  
  • export default App;
  • export const billionaires = [
  • {
  • country: "China",
  • amount: 1002
  • },
  • {
  • country: "United States",
  • amount: 716
  • },
  • {
  • country: "India",
  • amount: 215
  • },
  • {
  • country: "United Kingdom",
  • amount: 150
  • },
  • {
  • country: "Germany",
  • amount: 145
  • }
  • ];

Set PieChart Title

Use the title property to specify and configure the chart's title.

App.js
  • // ...
  •  
  • function App() {
  • return (
  • <PieChart ...
  • title="Top-5 Countries by Number of Billionaires"
  • >
  • <!-- ... -->
  • </PieChart>
  • );
  • }
  •  
  • export default App;
See Also

Configure Point Labels

You can accompany each series point with a label that displays the point's value or custom data.

To make point labels visible, assign true to the series.label.visible property. With this configuration, the component displays point labels detached from their respective series points. To make the connection between labels and points visible, set the label.connector.visible property to true.

The component displays labels next to points. Change the label.position property to rearrange labels in columns or place them inside series points.

If you need to change the point's label text, declare the label.customizeText function. It must return a string value.

App.js
  • // ...
  • import { PieChart, Series, Label } from 'devextreme-react/pie-chart';
  •  
  • const customizeText = (pointInfo) => {
  • return pointInfo.value + " billionaires";
  • }
  •  
  • function App() {
  • return (
  • <PieChart ...
  • >
  • <Series ...
  • >
  • <Label
  • visible={true}
  • position="columns"
  • customizeText={customizeText}
  • >
  • <Connector visible={true}></Connector>
  • </Label>
  • </Series>
  • </PieChart>
  • );
  • }
  •  
  • export default App;

Enable Tooltips

To enable tooltips, assign true to the enabled property of the tooltip object. A PieChart tooltip displays information about the point value, but you can display custom content in a tooltip.

In this tutorial, the tooltip displays information about the point argument. Use the tooltip.contentTemplate function to assign a custom template for all PieChart tooltips.

App.js
  • // ...
  • import { PieChart, Tooltip } from 'devextreme-react/pie-chart';
  •  
  • const contentTemplate = (data) => {
  • return data.argumentText;
  • }
  •  
  • function App() {
  • return (
  • <PieChart ...
  • >
  • <!-- ... -->
  • <Tooltip
  • enabled={true}
  • contentTemplate={contentTemplate}
  • >
  • </Tooltip>
  • </PieChart>
  • );
  • }
  •  
  • export default App;

Handle Selection

Use the onPointClick function to implement the selection functionality. Call a point's isSelected() method to check whether a user selected it.

In this tutorial, when a user clicks on a point, the point is selected. When the user clicks on the point again, the component clears the selection.

App.js
  • // ...
  •  
  • const onPointClick = (e) => {
  • const point = e.target;
  • if (point.isSelected()) {
  • point.clearSelection();
  • } else {
  • point.select();
  • }
  • }
  •  
  • function App() {
  • return (
  • <PieChart ...
  • onPointClick={onPointClick}
  • >
  • <!-- ... -->
  • <Tooltip
  • enabled={true}
  • contentTemplate={contentTemplate}
  • >
  • </Tooltip>
  • </PieChart>
  • );
  • }
  •  
  • export default App;