Skip to main content

Lesson 2 - Create a Pie Chart

  • 5 minutes to read

This lesson demonstrates how to create a Pie chart, bind it to data, add a legend, and customize Pie slice labels.

DevExpress WinUI | Get Started - Pie Chart

Before you proceed, review the following topics:

Create a New Project

Create a new Blank App, Packaged with Windows Application Packaging Project (WinUI 3 in Desktop) project and add the DevExpress.WinUI NuGet package.

Refer to the following topic for more information on how to create a new project: DevExpress WinUI Controls: Get Started.

Add a Data Model

In the MainWindow.xaml.cs, add a new DataPoint class that defines a data model:

public class DataPoint
{
    public string Product { get; set; }
    public double Income { get; set; }
}

Add a View Model

In the MainWindow.xaml.cs, add a new ChartViewModel class that specifies a view model:

using DevExpress.Mvvm;
using Microsoft.UI.Xaml;
using System.Collections.Generic;

public class ChartViewModel : ViewModelBase
{
    public ChartViewModel()
    {
        List<DataPoint> dataPoints = new List<DataPoint> {
            new DataPoint() { Product = "Camera", Income = 78 },
            new DataPoint() { Product = "Flash", Income = 92 },
            new DataPoint() { Product = "Smartphone", Income = 43 },
            new DataPoint() { Product = "Tripod", Income = 98 },
            new DataPoint() { Product = "Mobile Phone", Income = 70 },
            new DataPoint() { Product = "Smart Watch", Income = 98 },
            new DataPoint() { Product = "Laptop", Income = 85 },
            new DataPoint() { Product = "Tablet", Income = 99 },
            new DataPoint() { Product = "Headphone", Income = 94 }
        };
        DataPoints = dataPoints;
    }
    public List<DataPoint> DataPoints { get; }
}

In the MainWindow.xaml, specify the window’s data context as follows:

<Window
    ...
    xmlns:local="using:PieChartSample">
    <Grid>
        <Grid.DataContext>
            <local:ChartViewModel/>
        </Grid.DataContext>
    </Grid> 
    ...
</Window>    

Add the PieChart Control to the Window

Add a PieChart instance to the window:

<Window
    ...
    xmlns:Charts="using:DevExpress.WinUI.Charts">
    <Grid>
        <Charts:PieChart x:Name="chart">
            ...
        </Charts:PieChart>
    </Grid>
</Window>

Add a Series and Bind it to Data

<Charts:PieChart x:Name="chart" ToolTipEnabled="True">
    <Charts:PieChart.Series>
        <Charts:PieSeries x:Name="series">
            <Charts:PieSeries.View>
                <Charts:PieSeriesView ToolTipPointPattern="{}{A}: {VP:p}"
                                      LegendPointPattern="{}{A} (${V}K)" 
                                      ShowLabels="True" LabelPattern="{}{VP:p}" 
                                      LabelPosition="TwoColumns">
                    <Charts:PieSeriesView.Title>
                        <TextBlock Text="Sales Volume by Products" Margin="10"/>
                    </Charts:PieSeriesView.Title>
                </Charts:PieSeriesView>
            </Charts:PieSeries.View>
            <Charts:PieSeries.Data>
                <Charts:DataSource PointSource="{Binding DataPoints}" 
                                    ArgumentDataMember="Product"
                                    ArgumentScaleType="Qualitative"
                                    ValueDataMember="Income"
                                    ValueScaleType="Numerical"/>
            </Charts:PieSeries.Data>
        </Charts:PieSeries>
    </Charts:PieChart.Series>
</Charts:PieChart>

Add a Legend

<Charts:PieChart.Legend>
    <Charts:Legend HorizontalPosition="RightOutside" 
                   VerticalPosition="Center" 
                   Orientation="Horizontal" 
                   MaximumRowsOrColumns="1"/>
</Charts:PieChart.Legend>      

Results

The chart is ready. Your code and XAML should look as follows:

<Window
    x:Class="PieChartSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PieChartSample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Charts="using:DevExpress.WinUI.Charts"
    mc:Ignorable="d">
    <Grid>
        <Grid.DataContext>
            <local:ChartViewModel/>
        </Grid.DataContext>
        <Charts:PieChart x:Name="chart" ToolTipEnabled="True">
            <Charts:PieChart.Legend>
                <Charts:Legend HorizontalPosition="RightOutside" 
                               VerticalPosition="Center" 
                               Orientation="Horizontal" 
                               MaximumRowsOrColumns="1"/>
            </Charts:PieChart.Legend>
            <Charts:PieChart.Series>
                <Charts:PieSeries x:Name="series">
                    <Charts:PieSeries.View>
                        <Charts:PieSeriesView ToolTipPointPattern="{}{A}: {VP:p}"
                                              LegendPointPattern="{}{A} (${V}K)" 
                                              ShowLabels="True" LabelPattern="{}{VP:p}" 
                                              LabelPosition="TwoColumns">
                            <Charts:PieSeriesView.Title>
                                <TextBlock Text="Sales Volume by Products" Margin="10"/>
                            </Charts:PieSeriesView.Title>
                        </Charts:PieSeriesView>
                    </Charts:PieSeries.View>
                    <Charts:PieSeries.Data>
                        <Charts:DataSource PointSource="{Binding DataPoints}" 
                                           ArgumentDataMember="Product"
                                           ArgumentScaleType="Qualitative"
                                           ValueDataMember="Income"
                                           ValueScaleType="Numerical"/>
                    </Charts:PieSeries.Data>
                </Charts:PieSeries>
            </Charts:PieChart.Series>
        </Charts:PieChart>
    </Grid>
</Window>
using DevExpress.Mvvm;
using Microsoft.UI.Xaml;
using System.Collections.Generic;

namespace PieChartSample {
    public sealed partial class MainWindow : Window {
        public MainWindow() {
            this.InitializeComponent();
        }
    }
    public class DataPoint {
        public string Product { get; set; }
        public double Income { get; set; }
    }
    public class ChartViewModel : ViewModelBase {
        public ChartViewModel() {
            List<DataPoint> dataPoints = new List<DataPoint> {
                new DataPoint() { Product = "Camera", Income = 78 },
                new DataPoint() { Product = "Flash", Income = 92 },
                new DataPoint() { Product = "Smartphone", Income = 43 },
                new DataPoint() { Product = "Tripod", Income = 98 },
                new DataPoint() { Product = "Mobile Phone", Income = 70 },
                new DataPoint() { Product = "Smart Watch", Income = 98 },
                new DataPoint() { Product = "Laptop", Income = 85 },
                new DataPoint() { Product = "Tablet", Income = 99 },
                new DataPoint() { Product = "Headphone", Income = 94 }
            };
            DataPoints = dataPoints;
        }
        public List<DataPoint> DataPoints { get; }
    }
}

Run the project. The following image illustrates the resulting chart:

DevExpress WinUI | Get Started - Pie Chart