The PieChartView component visualizes data as a circle chart divided into slices to illustrate numerical proportion. This example allows you to get started with the PieChartView component and explore its basic functionality.
The following step-by-step tutorial details how to reproduce this application.
-
In Visual Studio 2022, create a new .NET MAUI project. Name it PieChartGetStarted.
If the wizard does not propose a template for .NET MAUI projects, you can call the following command in a CLI to create a new .NET MAUI project:
dotnet new maui -n PieChartGetStarted
-
Install the DevExpress.Maui.Charts package from this feed.
DevExpress Pie Chart for .NET MAUI supports iOS and Android. The project should contain only these platforms.
In the MauiProgram.cs file, call the UseDevExpress
method to register handlers for the PieChartView
and other DevExpress controls:
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Hosting;
using DevExpress.Maui.Charts;
using PieChartGetStarted;
namespace PieChartGetStarted {
public static class MauiProgram {
public static MauiApp CreateMauiApp() {
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseDevExpress()
.ConfigureFonts(fonts => {
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
}
In the MainPage.xaml file, use the dxc prefix to declare the DevExpress.Maui.Charts namespace and add a PieChartView instance to the ContentPage:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PieChartGetStarted.MainPage"
xmlns:dxc="clr-namespace:DevExpress.Maui.Charts;assembly=DevExpress.Maui.Charts">
<dxc:PieChartView>
</dxc:PieChartView>
</ContentPage>
In this example, the pie chart compares countries by area. Create a ViewModel.cs file with the following classes:
using System.Collections.Generic;
using Microsoft.Maui.Graphics;
class ViewModel {
public IReadOnlyList<LandAreaItem> LandAreas { get; }
public ViewModel() {
LandAreas = new List<LandAreaItem>() {
new LandAreaItem("Russia", 17.098),
new LandAreaItem("Canada", 9.985),
new LandAreaItem("People's Republic of China", 9.597),
new LandAreaItem("United States of America", 9.834),
new LandAreaItem("Brazil", 8.516),
new LandAreaItem("Australia", 7.692),
new LandAreaItem("India", 3.287),
new LandAreaItem("Others", 81.2)
};
}
}
class LandAreaItem {
public string CountryName { get; }
public double Area { get; }
public LandAreaItem(string countryName, double area) {
this.CountryName = countryName;
this.Area = area;
}
}
In the MainPage.xaml file, add a new DonutSeries object to the PieChartView.Series collection. To bind the series to data, set the DonutSeries.Data property to a PieSeriesDataAdapter object. Use the adapter’s properties to specify the data source and its fields that contain values and labels for series points:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PieChartGetStarted.MainPage"
xmlns:local="clr-namespace:PieChartGetStarted"
xmlns:dxc="clr-namespace:DevExpress.Maui.Charts;assembly=DevExpress.Maui.Charts">
<ContentPage.BindingContext>
<local:ViewModel/>
</ContentPage.BindingContext>
<dxc:PieChartView>
<dxc:PieChartView.Series>
<dxc:DonutSeries>
<dxc:DonutSeries.Data>
<dxc:PieSeriesDataAdapter DataSource="{Binding LandAreas}"
LabelDataMember="CountryName"
ValueDataMember="Area"/>
</dxc:DonutSeries.Data>
</dxc:DonutSeries>
</dxc:PieChartView.Series>
</dxc:PieChartView>
</ContentPage>
Create a Legend object, change its settings, and assign it to the PieChartView.Legend property:
<dxc:PieChartView>
<!--...-->
<dxc:PieChartView.Legend>
<dxc:Legend Orientation="TopToBottom"
HorizontalPosition="Center"
VerticalPosition="BottomOutside"/>
</dxc:PieChartView.Legend>
</dxc:PieChartView>
Set the DonutSeries.Label property to a PieSeriesLabel object with the specified TextPattern, Position, and Indent settings:
<dxc:PieChartView>
<dxc:PieChartView.Series>
<dxc:DonutSeries>
<!--...-->
<dxc:DonutSeries.Label>
<dxc:PieSeriesLabel TextPattern="{}{VP}%" Position="TwoColumns" Indent="20"/>
</dxc:DonutSeries.Label>
</dxc:DonutSeries>
</dxc:PieChartView.Series>
</dxc:PieChartView>
To show hints, set the PieChartView.Hint property to a PieHint object with its Enabled property set to True:
<dxc:PieChartView>
<dxc:PieChartView.Hint>
<dxc:PieHint Enabled="True"/>
</dxc:PieChartView.Hint>
<!--...-->
</dxc:PieChartView>
Specify the text pattern for tooltips of donut segments:
<dxc:PieChartView>
<!--...-->
<dxc:PieChartView.Series>
<dxc:DonutSeries>
<!--...-->
<dxc:DonutSeries.HintOptions>
<dxc:PieSeriesHintOptions PointTextPattern="{}{L}: {V}M sq km"/>
</dxc:DonutSeries.HintOptions>
</dxc:DonutSeries>
</dxc:PieChartView.Series>
</dxc:PieChartView>
In the view model, define a palette of custom colors for donut segments:
using Microsoft.Maui.Graphics;
// ...
class ViewModel {
// ...
readonly Color[] palette;
public Color[] Palette => palette;
public ViewModel() {
// ...
palette = PaletteLoader.LoadPalette("#975ba5", "#03bfc1", "#f8c855", "#f45a4e",
"#496cbe", "#f58f35", "#d293fd", "#25a966");}
}
static class PaletteLoader {
public static Color[] LoadPalette(params string[] values) {
Color[] colors = new Color[values.Length];
for (int i = 0; i < values.Length; i++)
colors[i] = Color.FromArgb(values[i]);
return colors;
}
}
Set the PieChartView.ChartStyle property to a PieChartStyle object with the specified Palette and BackgroundColor properties to change the chart's color scheme:
<ContentPage>
<ContentPage.BindingContext>
<local:ViewModel/>
</ContentPage.BindingContext>
<dxc:PieChartView>
<!--...-->
<dxc:PieChartView.ChartStyle>
<dxc:PieChartStyle Palette="{Binding Palette}"
BackgroundColor="#2d313d"/>
</dxc:PieChartView.ChartStyle>
</dxc:PieChartView>
</ContentPage>
Use the PieChartView.SelectionBehavior property to specify how to highlight a donut segment when a user selects it:
<dxc:PieChartView SelectionBehavior="Hatch">
<!--...-->
</dxc:PieChartView>
To change the legend appearance, set the Legend.Style property to a LegendStyle object, and specify this object's properties to customize the legend's border, background, and items:
<dxc:PieChartView.Legend>
<dxc:Legend>
<dxc:Legend.Style>
<dxc:LegendStyle BorderColor="LightGray" BorderThickness="1"
BackgroundColor="#424651"
MarkerSize="18" TextIndent="6"
ItemsVerticalIndent="12">
<dxc:LegendStyle.TextStyle>
<dxc:TextStyle Color="White" Size="12"/>
</dxc:LegendStyle.TextStyle>
</dxc:LegendStyle>
</dxc:Legend.Style>
</dxc:Legend>
</dxc:PieChartView.Legend>
Change text settings and connector thickness for the pie chart's labels. To do this, set the PieSeriesLabel.Style property to a PieSeriesLabelStyle object with the specified TextStyle and ConnectorThickness properties:
<dxc:PieChartView>
<dxc:PieChartView.Series>
<dxc:DonutSeries>
<!--...-->
<dxc:DonutSeries.Label>
<dxc:PieSeriesLabel Position="TwoColumns" TextPattern="{}{VP}%" Indent="20">
<dxc:PieSeriesLabel.Style>
<dxc:PieSeriesLabelStyle ConnectorThickness="1">
<dxc:PieSeriesLabelStyle.TextStyle>
<dxc:TextStyle Color="#eff2f6" Size="12"/>
</dxc:PieSeriesLabelStyle.TextStyle>
</dxc:PieSeriesLabelStyle>
</dxc:PieSeriesLabel.Style>
</dxc:PieSeriesLabel>
</dxc:DonutSeries.Label>
</dxc:DonutSeries>
</dxc:PieChartView.Series>
</dxc:PieChartView>
Use the BackgroundColor property to change the background of hints:
<dxc:PieChartView>
<dxc:PieChartView.Hint>
<dxc:PieHint Enabled="True">
<dxc:PieHint.Style>
<dxc:PieHintStyle BackgroundColor="#424651"/>
</dxc:PieHint.Style>
</dxc:PieHint>
</dxc:PieChartView.Hint>
</dxc:PieChartView>