Skip to content

Commit 6d8c7e3

Browse files
Update README.md
1 parent a9e9df4 commit 6d8c7e3

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

README.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,126 @@
1-
# How-to-create-a-Pie-Chart-in-.NET-MAUI
1+
# How-to-create-a-Pie-Chart-in-.NET-MAUI
2+
3+
A .NET MAUI Pie charts visual representation of percentages at a certain point in time and it can be used to show percentages of a whole. This section explains how to create a beautiful .NET MAUI Pie Charts.
4+
5+
## Register the handler.
6+
Syncfusion.Maui.Core nuget is a dependent package for all Syncfusion controls of .NET MAUI. In the MauiProgram.cs file, register the handler for Syncfusion core. For more details refer this link.
7+
8+
## Initialize Chart
9+
Import the SfCircularChart namespace as shown below.
10+
11+
[XAML]
12+
13+
xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
14+
15+
[C#]
16+
17+
using Syncfusion.Maui.Charts;
18+
Initialize an empty chart and set as content,
19+
20+
[XAML]
21+
22+
<chart:SfCircularChart>
23+
. . .
24+
</chart:SfCircularChart>
25+
26+
[C#]
27+
28+
SfCircularChart chart = new SfCircularChart();
29+
. . .
30+
this.Content = chart;
31+
32+
## Initialize view model
33+
34+
Now, let define a simple data model that represents a data point for .NET MAUI Pie Chart.
35+
36+
public class Model
37+
{
38+
public string Country{ get; set; }
39+
40+
public double Counts { get; set; }
41+
42+
public Model(string name , double count)
43+
{
44+
Country= name;
45+
Counts = count;
46+
}
47+
}
48+
49+
Create a view model class and initialize a list of objects as shown below,
50+
51+
public class ViewModel
52+
{
53+
public ObservableCollection<Model> Data { get; set; }
54+
55+
public ViewModel()
56+
{
57+
Data = new ObservableCollection<Model>()
58+
{
59+
new Model("Algeria", 28),
60+
new Model("Australia", 14),
61+
new Model("Bolivia", 31),
62+
new Model("Cambodia", 77),
63+
new Model("Canada", 19),
64+
};
65+
}
66+
}
67+
68+
Set the ViewModel instance as the BindingContext of chart; this is done to bind properties of ViewModel to SfCircularChart.
69+
70+
Add namespace of ViewModel class in your XAML page if you prefer to set BindingContext in XAML.
71+
72+
[XAML]
73+
74+
xmlns:viewModel ="clr-namespace:MauiApp"
75+
. . .
76+
<chart:SfCircularChart>
77+
<chart:SfCircularChart.BindingContext>
78+
<viewModel:ViewModel/>
79+
</chart:SfCircularChart.BindingContext>
80+
</chart:SfCircularChart>
81+
82+
[C#]
83+
84+
SfCircularChart chart = new SfCircularChart();
85+
chart.BindingContext = new ViewModel();
86+
87+
## How to populate data in .NET MAUI Pie Charts
88+
89+
As we are going to visualize the comparison of annual population of various countries in the data model, add PieSeries to SfCircularChart.Series property, and then bind the Data property of the above ViewModel to the PieSeries.ItemsSource property as shown below.
90+
91+
Need to set XBindingPath and YBindingPath properties, so that series would fetch values from the respective properties in the data model to plot the series.
92+
93+
[XAML]
94+
95+
<chart:SfCircularChart>
96+
97+
<chart:SfCircularChart.BindingContext>
98+
<viewModel:ViewModel/>
99+
</chart:SfCircularChart.BindingContext>
100+
101+
<chart:SfCircularChart.Series>
102+
<chart:PieSeries ItemsSource="{Binding Data}"
103+
XBindingPath="Country"
104+
YBindingPath="Counts"
105+
ShowDataLabels="True">
106+
</chart:PieSeries>
107+
</chart:SfCircularChart.Series>
108+
109+
</chart:SfCircularChart>
110+
111+
[C#]
112+
113+
SfCircularChart chart = new SfCircularChart ();
114+
115+
chart.BindingContext = new ViewModel();
116+
. . .
117+
var binding = new Binding() { Path = "Data" };
118+
var series = new PieSeries()
119+
{
120+
XBindingPath = " Country",
121+
YBindingPath = "Counts",
122+
ShowDataLabels = true
123+
};
124+
125+
series.SetBinding(ChartSeries.ItemsSourceProperty, binding);
126+
chart.Series.Add(series);

0 commit comments

Comments
 (0)