Skip to main content

Get Started with Pie Chart for .NET MAUI

  • 4 minutes to read

This topic explains how to create a sample application that uses the PieChartView to visualize the distribution of the world’s land by country.

Scatter Plot

View Example: DevExpress Pie Chart for .NET MAUI

Create a Project

  1. Create a new .NET MAUI project. Name it PieChartGetStarted.

    If the wizard does not suggest 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
    
  2. Register your personal NuGet package source in Visual Studio. See the following topic for information on how to add the DevExpress NuGet feed to Visual Studio: Get Started with DevExpress Controls for .NET Multi-platform App UI (.NET MAUI).

    If you are an active DevExpress Universal customer, DevExpress Controls for .NET MAUI are available in your personal NuGet feed.

  3. Install the DevExpress.Maui.Charts package from this feed.

Note

DevExpress Pie Chart for .NET MAUI supports iOS and Android. The project should contain only these platforms.

Add a Pie Chart to the Main Page

In the MauiProgram.cs file, call the UseDevExpress method to register a handler for the PieChartView class.

using DevExpress.Maui;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Compatibility.Hosting;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;

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");
                    fonts.AddFont("univia-pro-regular.ttf", "Univia-Pro");
                    fonts.AddFont("roboto-bold.ttf", "Roboto-Bold");
                    fonts.AddFont("roboto-regular.ttf", "Roboto");
                })
                .UseMauiCompatibility();

            return builder.Build();
        }
    }
}

In the MainPage.xaml file, define the dxc XAML namespace that refers to the DevExpress.Maui.Charts CLR namespace. Then, remove the default content and add an instance of the PieChartView class to the page.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dxc="clr-namespace:DevExpress.Maui.Charts;assembly=DevExpress.Maui.Charts"
             xmlns:local="clr-namespace:PieChartGetStarted"
             xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
             ios:Page.UseSafeArea="true"
             x:Class="PieChartGetStarted.MainPage">
    <dxc:PieChartView/>
</ContentPage>

Remove the default content’s event handlers in the code-behind. We also recommend that you remove the default styles (fonts, colors, and other settings) in the App.xaml file.

Populate the Pie Chart with Data

In this example, the chart compares countries by area. Create a ViewModel.cs file with the following classes:

namespace PieChartGetStarted
{
    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 fields that contain values and labels for series points.

<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>

Add a Legend to the Pie Chart

Create a Legend object, modify its settings, and assign it to the PieChartView.Legend property.

<dxc:PieChartView.Legend>
    <dxc:Legend Orientation="TopToBottom"
                HorizontalPosition="Center"
                VerticalPosition="BottomOutside">
                <!-- ... -->
    </dxc:Legend>
</dxc:PieChartView.Legend>

Enable Series Labels

Set the DonutSeries.Label property to a PieSeriesLabel object with the specified TextPattern, Position, and Indent settings.

<dxc:DonutSeries.Label>
    <dxc:PieSeriesLabel Position="TwoColumns" TextPattern="{}{VP}%" Indent="20">
    <!-- ... -->
    </dxc:PieSeriesLabel>
</dxc:DonutSeries.Label>

Enable Tooltips for Chart Segments

Set the PieChartView.Hint property to a PieHint object. Set the PieHint.Enabled property to True.

<dxc:PieChartView.Hint>
    <dxc:PieHint Enabled="True">
    <!-- ... -->
    </dxc:PieHint>
</dxc:PieChartView.Hint>

Specify the tooltip pattern for donut segments.

<dxc:DonutSeries.HintOptions>
    <dxc:PieSeriesHintOptions PointTextPattern="{}{L}: {V}M sq km"/>
</dxc:DonutSeries.HintOptions>

Modify the Pie Chart Appearance

In the view model, define a palette for donut segments.

class ViewModel
{
// ...
    public ViewModel()
    {
    // ...
        palette = PaletteLoader.LoadPalette("#975ba5", "#03bfc1", "#f8c855", "#f45a4e",
                                            "#496cbe", "#f58f35", "#d293fd", "#25a966");
    }
    // ...
    readonly Color[] palette;
    public Color[] Palette => palette;
}
// ...
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.

<dxc:PieChartView.ChartStyle>
    <dxc:PieChartStyle Palette="{Binding Palette}" 
                       BackgroundColor="#2d313d"/>
</dxc:PieChartView.ChartStyle>

Use the PieChartView.SelectionBehavior property to specify how to highlight a donut segment when a user selects it.

<dxc:PieChartView SelectionBehavior="Hatch">

Set the Legend.Style property to a LegendStyle object to change legend appearance. Specify this object’s properties that customize the legend colors, indents, and text size.

<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>

Set the PieSeriesLabel.Style property to a PieSeriesLabelStyle object with the specified TextStyle and ConnectorThickness properties.

<dxc:PieSeriesLabel.Style>
    <dxc:PieSeriesLabelStyle ConnectorThickness="1">
        <dxc:PieSeriesLabelStyle.TextStyle>
            <dxc:TextStyle Color="#eff2f6" Size="12"/>
        </dxc:PieSeriesLabelStyle.TextStyle>
    </dxc:PieSeriesLabelStyle>
</dxc:PieSeriesLabel.Style>

Use the PieHintStyle.BackgroundColor property to change the hint background color.

<dxc:PieHint.Style>
    <dxc:PieHintStyle BackgroundColor="#424651"/>
</dxc:PieHint.Style>