Getting started with WinUI Pyramid Chart (SfPyramidChart)

10 Jul 20269 minutes to read

This section explains how to populate the WinUI Pyramid Chart with data, a header, data labels, legend, and tooltips, as well as the essential aspects for getting started with the chart.

Creating an application with WinUI Chart

  1. Create a WinUI 3 desktop app for C# and .NET 5.
  2. Add a reference to the Syncfusion.Chart.WinUI NuGet.
  3. Import the control namespace Syncfusion.UI.Xaml.Charts in XAML or C# to initialize the control.
  4. Initialize the SfPyramidChart control.

    <Window
        x:Class="ChartDemo.MainWindow"
           
        <!-- Configure additional chart elements -->
        xmlns:chart="using:Syncfusion.UI.Xaml.Charts">
       
        <chart:SfPyramidChart/>
       
    </Window>
    using Syncfusion.UI.Xaml.Charts;
       
    // Configure additional chart elements
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            SfPyramidChart chart = new SfPyramidChart();
       
            this.Content = chart;
        }
    }

Initialize View Model

Now, let’s define a simple data model that represents a data point in the chart.

public class Model
{
    public string FoodName { get; set; }
    public double Calories { get; set; }
}

Next, create a view model class and initialize a list of Model objects as follows.

public class ChartViewModel
{
    public List<Model> Data { get; set; }

    public ChartViewModel()
    {
        Data = new List<Model>()
        {
            new Model(){FoodName = "Sweet treats", Calories = 250},
            new Model(){FoodName = "Cheese", Calories = 402},
            new Model(){FoodName = "Vegetables", Calories = 65},
            new Model(){FoodName = "Fish", Calories = 206},
            new Model(){FoodName = "Fruits", Calories = 52},
            new Model(){FoodName = "Rice", Calories = 130},
        };
    }
}

Create a ViewModel instance and set it as the chart’s DataContext. This enables property binding from the ViewModel class.

NOTE

If you prefer to set DataContext in XAML, add the namespace of the ViewModel class to your XAML Page.

<Window

    <!-- Configure additional chart elements -->
    xmlns:chart="using:Syncfusion.UI.Xaml.Charts"
    xmlns:model="using:ChartDemo.ViewModel">

    <chart:SfPyramidChart>
        <chart:SfPyramidChart.DataContext>
            <model:ChartViewModel/>
        </chart:SfPyramidChart.DataContext>
    </chart:SfPyramidChart>

</Window>
ChartViewModel viewModel = new ChartViewModel();

chart.DataContext = viewModel;

Add Title

The title of the chart provides quick information to the user about the data being plotted in the chart. The title can be set by using the Header property of the pyramid chart as follows.

<chart:SfPyramidChart Header="Calories in food">

<!-- Configure additional chart elements -->
</chart:SfPyramidChart>
SfPyramidChart chart = new SfPyramidChart();

// Configure additional chart elements
chart.Header = "Calories in food";

Enable Data Labels

The ShowDataLabels property of PyramidChart is used to enable data labels to improve the readability of the pyramid chart. The label visibility is set to False by default.

<chart:SfPyramidChart ShowDataLabels="True">

<!-- Configure additional chart elements -->
</chart:SfPyramidChart>
SfPyramidChart chart = new SfPyramidChart();

// Configure additional chart elements
chart.ShowDataLabels = true;

Enable Legend

The legend provides information about the data points displayed in the pyramid chart. The Legend property of the chart is used to enable it.

<chart:SfPyramidChart>

    <!-- Configure additional chart elements -->
    <chart:SfPyramidChart.Legend>
        <chart:ChartLegend/>
    </chart:SfPyramidChart.Legend>
</chart:SfPyramidChart>
SfPyramidChart chart = new SfPyramidChart();

// Configure additional chart elements
chart.Legend = new ChartLegend();

Enable Tooltip

Tooltips are used to display information about a segment when the mouse is moved over it. Enable the tooltip by setting the EnableTooltip property to true.

<chart:SfPyramidChart EnableTooltip="True">

<!-- Configure additional chart elements -->
</chart:SfPyramidChart>
SfPyramidChart chart = new SfPyramidChart();

// Configure additional chart elements
chart.EnableTooltip = true;

The following code example gives you the complete code for the above configurations.

NOTE

To plot the chart, the XBindingPath and YBindingPath properties must be configured so that the chart can get values from the respective properties in the data model.

<chart:SfPyramidChart 
    x:Name="chart" 
    Header="The Food Comparison Pyramid"
    EnableTooltip="True"
    ShowDataLabels="True"
    ItemsSource="{Binding Data}" 
    XBindingPath="FoodName"
    YBindingPath="Calories">

    <chart:SfPyramidChart.DataContext>
        <model:ChartViewModel/>
    </chart:SfPyramidChart.DataContext>

    <chart:SfPyramidChart.Legend>
        <chart:ChartLegend/>
    </chart:SfPyramidChart.Legend>
</chart:SfPyramidChart>
using Syncfusion.UI.Xaml.Charts;

// Configure additional chart elements
public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        SfPyramidChart chart = new SfPyramidChart();

        ChartViewModel viewModel = new ChartViewModel();
        
        chart.DataContext = viewModel;
        
        chart.SetBinding(SfPyramidChart.ItemsSourceProperty, new Binding() 
        { 
            Path = new PropertyPath("Data")     
        });
        
        chart.XBindingPath = "FoodName";
        chart.YBindingPath = "Calories";
        chart.Header = "The Food Comparison Pyramid";
        chart.Legend = new ChartLegend();
        chart.EnableTooltip = true;
        chart.ShowDataLabels = true;

        this.Content = chart;
    }
}

Getting started for WinUI Chart

NOTE

Download demo application from GitHub.

NOTE

You can also explore our WinUI Pyramid Chart example that shows how to easily configure the chart with built-in support for creating stunning visual effects.