React PieChart - Getting Started
jQuery
Angular
Vue
React
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 GitHub repository.
Create a PieChart
jQuery
Add DevExtreme to your jQuery application and use the following code to create a PieChart:
$(function() { $("#pie-chart").dxPieChart({ // Configuration goes here }); });
<html> <head> <!-- ... --> <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/23.1.13/css/dx.light.css"> <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/23.1.13/js/dx.all.js"></script> <script type="text/javascript" src="index.js"></script> </head> <body> <div id="pie-chart"></div> </body> </html>
Angular
Add DevExtreme to your Angular application and use the following code to create a PieChart:
<dx-pie-chart // Configuration goes here > </dx-pie-chart>
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { }
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { DxPieChartModule } from 'devextreme-angular'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, DxPieChartModule ], providers: [ ], bootstrap: [AppComponent] }) export class AppModule { }
Vue
Add DevExtreme to your Vue application and use the following code to create a PieChart:
<template> <DxPieChart // Configuration goes here > </DxPieChart> </template> <script> import 'devextreme/dist/css/dx.light.css'; import { DxPieChart } from 'devextreme-vue/pie-chart'; export default { components: { DxPieChart } } </script>
React
Add DevExtreme to your React application and use the following code to create a PieChart:
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.
jQuery
$(function() { $("#pie-chart").dxPieChart({ dataSource: billionaires series: { argumentField: "country", valueField: "amount", }, type: "doughnut" }); });
<head> <!-- ... --> <script type="text/javascript" src="data.js"></script> </head>
const billionaires = [ { country: "China", amount: 1002 }, { country: "United States", amount: 716 }, { country: "India", amount: 215 }, { country: "United Kingdom", amount: 150 }, { country: "Germany", amount: 145 } ];
Angular
<dx-pie-chart [dataSource]="billionaires" type="doughnut" > <dxi-series argumentField="country" valueField="amount" > </dxi-series> </dx-pie-chart>
import { Component } from '@angular/core'; import { Billionaires, Service } from './app.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], providers: [Service], }) export class AppComponent { billionaires: Billionaires[]; constructor(private service: Service) { this.billionaires = service.getBillionaires(); } }
import { Injectable } from '@angular/core'; export class Billionaires { country: string; amount: number; } const billionaires: Billionaires[] = [ { country: "China", amount: 1002 }, { country: "United States", amount: 716 }, { country: "India", amount: 215 }, { country: "United Kingdom", amount: 150 }, { country: "Germany", amount: 145 } ]; @Injectable() export class Service { getBillionaires(): Billionaires[] { return billionaires; } }
Vue
<template> <DxPieChart :data-source="billionaires" type="doughnut" > <DxSeries argument-field="country" value-field="amount" > </DxSeries> </DxPieChart> </template> <script> // ... import { DxPieChart, DxSeries } from 'devextreme-vue/pie-chart'; import { billionaires } from './data'; export default { components: { DxPieChart, DxSeries }, data() { return { billionaires } } } </script>
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 } ];
React
// ... 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.
jQuery
$(function() { $("#pie-chart").dxPieChart({ // ... title: "Top-5 Countries by Number of Billionaires" }); });
Angular
<dx-pie-chart ... title="Top-5 Countries by Number of Billionaires" > <!-- ... --> </dx-pie-chart>
Vue
<template> <DxPieChart ... title="Top-5 Countries by Number of Billionaires" > <!-- ... --> </DxPieChart> </template> <script> // ... </script>
React
// ... 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.
jQuery
$(function() { $("#pie-chart").dxPieChart({ // ... series: { // ... label: { visible: true, connector: { visible: true }, position: "columns", customizeText: function (pointInfo) { return pointInfo.value + " billionaires"; } } } }); });
Angular
<dx-pie-chart ... > <dxi-series ... > <dxo-label [visible]="true" position="columns" [customizeText]="customizeText" > <dxo-connector [visible]="true"></dxo-connector> </dxo-label> </dxi-series> </dx-pie-chart>
// ... export class AppComponent { // ... customizeText (pointInfo: any) { return pointInfo.value + " billionaires"; } }
Vue
<template> <DxPieChart ... > <DxSeries ... > <DxLabel :visible="true" position="columns" :customize-text="customizeText" > <DxConnector :visible="true"></DxConnector> </DxLabel> </DxSeries> </DxPieChart> </template> <script> // ... import { DxPieChart, DxSeries, DxLabel } from 'devextreme-vue/pie-chart'; export default { components: { DxPieChart, DxSeries, DxLabel }, data() { return { // ... } }, methods: { customizeText (pointInfo) { return pointInfo.value + " billionaires"; } } } </script>
React
// ... 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.
jQuery
$(function() { $("#pie-chart").dxPieChart({ // ... tooltip: { enabled: true, contentTemplate: function (data) { return data.argumentText; } } }); });
Angular
<dx-pie-chart ... > <!-- ... --> <dxo-tooltip [enabled]="true" [contentTemplate]="contentTemplate" > </dxo-tooltip> </dx-pie-chart>
// ... export class AppComponent { // ... contentTemplate (data: any) { return data.argumentText; } }
Vue
<template> <DxPieChart ... > <!-- ... --> <DxTooltip :enabled="true" :content-template="contentTemplate" > </DxTooltip> </DxPieChart> </template> <script> // ... import { DxPieChart, DxTooltip } from 'devextreme-vue/pie-chart'; export default { components: { DxPieChart, DxTooltip }, data() { return { // ... } }, methods: { // ... contentTemplate (data) { return data.argumentText; } } } </script>
React
// ... 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.
jQuery
$(function() { $("#pie-chart").dxPieChart({ // ... onPointClick: function (e) { const point = e.target; if (point.isSelected()) { point.clearSelection(); } else { point.select(); } } }); });
Angular
<dx-pie-chart ... (onPointClick)="onPointClick($event)" > <!-- ... --> </dx-pie-chart>
// ... export class AppComponent { // ... onPointClick (e: any) { const point = e.target; if (point.isSelected()) { point.clearSelection(); } else { point.select(); } } }
Vue
<template> <DxPieChart ... @point-click="onPointClick" > <!-- ... --> </DxPieChart> </template> <script> // ... export default { components: { // ... }, data() { return { // ... } }, methods: { // ... onPointClick (e) { const point = e.target; if (point.isSelected()) { point.clearSelection(); } else { point.select(); } } } } </script>
React
// ... 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;
If you have technical questions, please create a support ticket in the DevExpress Support Center.