Skip to content

Commit 8c1ae94

Browse files
Merge pull request #1 from saravananmadhesh/Add_demo_for_pie_chart
Add demo for pie chart
2 parents 0fc288b + 6d8c7e3 commit 8c1ae94

31 files changed

+889
-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);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31710.8
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PieChartDemo", "PieChartDemo\PieChartDemo.csproj", "{01293D4A-08A3-4D47-8660-BD22D508F5C7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{01293D4A-08A3-4D47-8660-BD22D508F5C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{01293D4A-08A3-4D47-8660-BD22D508F5C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{01293D4A-08A3-4D47-8660-BD22D508F5C7}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{01293D4A-08A3-4D47-8660-BD22D508F5C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{01293D4A-08A3-4D47-8660-BD22D508F5C7}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{01293D4A-08A3-4D47-8660-BD22D508F5C7}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {2D1F2E5D-D9DC-40E0-A728-5F9752C7E8C0}
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
3+
xmlns:windows="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;assembly=Microsoft.Maui.Controls"
4+
xmlns:local="clr-namespace:MauiApp2"
5+
x:Class="MauiApp2.App"
6+
windows:Application.ImageDirectory="Assets">
7+
<Application.Resources>
8+
<ResourceDictionary>
9+
10+
<Color x:Key="PrimaryColor">#512bdf</Color>
11+
<Color x:Key="SecondaryColor">White</Color>
12+
13+
<Style TargetType="Label">
14+
<Setter Property="TextColor" Value="{DynamicResource PrimaryColor}" />
15+
<Setter Property="FontFamily" Value="OpenSansRegular" />
16+
</Style>
17+
18+
<Style TargetType="Button">
19+
<Setter Property="TextColor" Value="{DynamicResource SecondaryColor}" />
20+
<Setter Property="FontFamily" Value="OpenSansRegular" />
21+
<Setter Property="BackgroundColor" Value="{DynamicResource PrimaryColor}" />
22+
<Setter Property="Padding" Value="14,10" />
23+
</Style>
24+
25+
</ResourceDictionary>
26+
</Application.Resources>
27+
</Application>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Charts;
2+
using Microsoft.Maui;
3+
using Microsoft.Maui.Controls;
4+
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
5+
using Microsoft.Maui.Graphics;
6+
using Application = Microsoft.Maui.Controls.Application;
7+
using TabbedPage = Microsoft.Maui.Controls.TabbedPage;
8+
9+
namespace MauiApp2
10+
{
11+
public partial class App : Application
12+
{
13+
public App()
14+
{
15+
InitializeComponent();
16+
17+
TabbedPage tab = new TabbedPage() { BarTextColor = Colors.White };
18+
tab.Children.Add(new MainPage() { Title = "Using xaml" });
19+
tab.Children.Add(new PieDemo() { Title = "Using C#" });
20+
MainPage = tab;
21+
}
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
3+
x:Class="Charts.MainPage"
4+
xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
5+
xmlns:viewModel ="clr-namespace:Charts.ViewModel"
6+
BackgroundColor="{DynamicResource PageBackgroundColor}">
7+
8+
<Grid HorizontalOptions="FillAndExpand" Padding="20" BackgroundColor="White" VerticalOptions="FillAndExpand">
9+
<chart:SfCircularChart>
10+
<chart:SfCircularChart.BindingContext>
11+
<viewModel:ViewModel/>
12+
</chart:SfCircularChart.BindingContext>
13+
<chart:SfCircularChart.Legend>
14+
<chart:ChartLegend Placement="Right" />
15+
</chart:SfCircularChart.Legend>
16+
<chart:SfCircularChart.Title>
17+
<Label Text="Rural population of various countries" FontSize="Large" Margin="5,10,5,10" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand"></Label>
18+
</chart:SfCircularChart.Title>
19+
<chart:SfCircularChart.Series>
20+
<chart:PieSeries ItemsSource="{Binding Data}" ShowDataLabels="True" Palette="Custom" CustomBrushes="{Binding CustomBrushes}"
21+
XBindingPath="Country"
22+
YBindingPath="Counts">
23+
<chart:PieSeries.DataLabelSettings>
24+
<chart:CircularDataLabelSettings>
25+
<chart:CircularDataLabelSettings.LabelStyle>
26+
<chart:ChartDataLabelStyle LabelFormat="0'%"/>
27+
</chart:CircularDataLabelSettings.LabelStyle>
28+
</chart:CircularDataLabelSettings>
29+
</chart:PieSeries.DataLabelSettings>
30+
</chart:PieSeries>
31+
</chart:SfCircularChart.Series>
32+
</chart:SfCircularChart>
33+
</Grid>
34+
</ContentPage>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Maui.Controls;
4+
using Syncfusion.Maui.Charts;
5+
namespace Charts
6+
{
7+
public partial class MainPage : ContentPage
8+
{
9+
public MainPage()
10+
{
11+
InitializeComponent();
12+
}
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Maui;
2+
using Microsoft.Maui.Controls.Compatibility;
3+
using Microsoft.Maui.Controls.Hosting;
4+
using Microsoft.Maui.Hosting;
5+
using Syncfusion.Maui.Core.Hosting;
6+
using Syncfusion.Maui.Graphics.Internals;
7+
8+
namespace MauiApp2
9+
{
10+
public static class MauiProgram
11+
{
12+
public static MauiApp CreateMauiApp()
13+
{
14+
var builder = MauiApp.CreateBuilder();
15+
builder
16+
.UseMauiApp<App>()
17+
.ConfigureSyncfusionCore()
18+
.ConfigureFonts(fonts =>
19+
{
20+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
21+
});
22+
23+
return builder.Build();
24+
}
25+
}
26+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0-ios;net6.0-android;net6.0-maccatalyst</TargetFrameworks>
5+
<!--<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);net6.0-windows10.0.19041</TargetFrameworks>-->
6+
<OutputType>Exe</OutputType>
7+
<RootNamespace>MauiApp2</RootNamespace>
8+
<UseMaui>true</UseMaui>
9+
<SingleProject>true</SingleProject>
10+
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
11+
12+
<!-- Display name -->
13+
<ApplicationTitle>MauiApp2</ApplicationTitle>
14+
15+
<!-- App Identifier -->
16+
<ApplicationId>com.companyname.MauiApp2</ApplicationId>
17+
18+
<!-- Versions -->
19+
<ApplicationVersion>1.0</ApplicationVersion>
20+
<AndroidVersionCode>1</AndroidVersionCode>
21+
22+
<!-- Required for C# Hot Reload -->
23+
<UseInterpreter Condition="'$(Configuration)' == 'Debug'">True</UseInterpreter>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<!-- App Icon -->
28+
<MauiImage Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" IsAppIcon="true" Color="#512BD4" />
29+
30+
<!-- Splash Screen -->
31+
<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" />
32+
33+
<!-- Images -->
34+
<MauiImage Include="Resources\Images\*" />
35+
36+
<!-- Custom Fonts -->
37+
<MauiFont Include="Resources\Fonts\*" />
38+
</ItemGroup>
39+
40+
<ItemGroup Condition="$(TargetFramework.Contains('-windows'))">
41+
<!-- Required - WinUI does not yet have buildTransitive for everything -->
42+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0-experimental1" />
43+
<PackageReference Include="Microsoft.WindowsAppSDK.Foundation" Version="1.0.0-experimental1" />
44+
<PackageReference Include="Microsoft.WindowsAppSDK.WinUI" Version="1.0.0-experimental1" />
45+
<PackageReference Include="Microsoft.WindowsAppSDK.InteractiveExperiences" Version="1.0.0-experimental1" NoWarn="NU1701" />
46+
</ItemGroup>
47+
48+
<ItemGroup>
49+
<PackageReference Include="Syncfusion.Maui.Charts" Version="19.3.43-preview" />
50+
</ItemGroup>
51+
52+
<!--<PropertyGroup Condition="$(TargetFramework.Contains('-windows'))">
53+
<OutputType>WinExe</OutputType>
54+
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
55+
</PropertyGroup>-->
56+
57+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<IsFirstTimeProjectOpen>False</IsFirstTimeProjectOpen>
5+
<ActiveDebugFramework>net6.0-android</ActiveDebugFramework>
6+
<ActiveDebugProfile>OnePlus A5000 (Android 8.1 - API 27)</ActiveDebugProfile>
7+
<SelectedDevice>OnePlus A5000</SelectedDevice>
8+
<DefaultDevice>kiler_2_q_10_0_-_api_29</DefaultDevice>
9+
</PropertyGroup>
10+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-ios|AnyCPU'">
11+
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
12+
</PropertyGroup>
13+
<PropertyGroup Condition="'$(TargetPlatformIdentifier)'=='iOS'">
14+
<RuntimeIdentifier>iossimulator-x64</RuntimeIdentifier>
15+
<PlatformTarget>x64</PlatformTarget>
16+
</PropertyGroup>
17+
</Project>

0 commit comments

Comments
 (0)