Skip to content

Commit ce7c228

Browse files
Merge pull request #1 from Saravanan-Madhesh/Sample_for_spline_chart
Sample created for the how to create spline chart
2 parents 0c6eaa8 + 991ad97 commit ce7c228

39 files changed

+1307
-0
lines changed

README.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,161 @@
11
# How-to-create-a-.NET-MAUI-Spline-Chart
22
The SplineSeries resembles line series, but instead of connecting the data points with line segments, the data points are connected by smooth bezier curves.
3+
4+
![.NET MAUI Spline Chart](https://user-images.githubusercontent.com/92786382/192692779-494315cf-efd0-4830-ab83-7c5bce64ad6f.png)
5+
6+
## Register the handler.
7+
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.
8+
9+
## Initialize Chart
10+
Import the SfCartesianChart namespace as shown below.
11+
12+
13+
**[XAML]**
14+
15+
```
16+
xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
17+
```
18+
19+
**[C#]**
20+
21+
```
22+
using Syncfusion.Maui.Charts;
23+
```
24+
Initialize an empty chart with XAxes and YAxes as shown in the following code sample.
25+
26+
**[XAML]**
27+
28+
```
29+
<chart:SfCartesianChart>
30+
31+
<chart:SfCartesianChart.XAxes >
32+
<chart:CategoryAxis/>
33+
</chart:SfCartesianChart.XAxes>
34+
35+
<chart:SfCartesianChart.YAxes>
36+
<chart:NumericalAxis/>
37+
</chart:SfCartesianChart.YAxes>
38+
39+
</chart:SfCartesianChart>
40+
41+
```
42+
**[C#]**
43+
44+
```
45+
SfCartesianChart chart = new SfCartesianChart();
46+
47+
//Initializing Primary Axis
48+
CategoryAxis primaryAxis = new CategoryAxis();
49+
50+
chart.XAxes.Add(primaryAxis);
51+
52+
//Initializing Secondary Axis
53+
NumericalAxis secondaryAxis = new NumericalAxis();
54+
55+
chart.YAxes.Add(secondaryAxis);
56+
57+
this.Content = chart;
58+
59+
```
60+
61+
## Initialize view model
62+
Now, let define a simple data model that represents a data point for .NET MAUI Spline Chart.
63+
64+
```
65+
public class Model
66+
{
67+
public string Name { get; set; }
68+
69+
public double Degree { get; set; }
70+
71+
public Model(string name , double degree)
72+
{
73+
Name = name;
74+
Degree = degree;
75+
}
76+
}
77+
```
78+
79+
Create a view model class and initialize a list of objects as shown below,
80+
81+
```
82+
public class ViewModel
83+
{
84+
public ObservableCollection<Model> Data { get; set; }
85+
86+
public ViewModel()
87+
{
88+
Data = new ObservableCollection<Model>()
89+
{
90+
new Model("Korea",39),
91+
new Model("India",20),
92+
new Model("Africa", 61),
93+
new Model("China",65),
94+
new Model("France",45),
95+
};
96+
}
97+
}
98+
```
99+
100+
Set the ViewModel instance as the BindingContext of chart; this is done to bind properties of ViewModel to SfCartesianChart.
101+
102+
> Note: Add namespace of ViewModel class in your XAML page if you prefer to set BindingContext in XAML.
103+
104+
**[XAML]**
105+
106+
```
107+
xmlns:viewModel ="clr-namespace:MauiApp"
108+
. . .
109+
<chart:SfCartesianChart>
110+
111+
<chart:SfCartesianChart.BindingContext>
112+
<viewModel:ViewModel/>
113+
</chart:SfCartesianChart.BindingContext>
114+
115+
</chart:SfCartesianChart>
116+
```
117+
**[C#]**
118+
119+
```
120+
SfCartesianChart chart = new SfCartesianChart();
121+
chart.BindingContext = new ViewModel();
122+
```
123+
124+
## How to populate data in .NET MAUI Spline Charts
125+
As we are going to visualize the comparison of annual rainfall in the data model, add SplineSeries to SfCartesianChart.Series property, and then bind the Data property of the above ViewModel to the SplineSeries.ItemsSource property as shown below.
126+
127+
> Note: 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.
128+
129+
**[XAML]**
130+
131+
```
132+
<chart:SfCartesianChart>
133+
<chart:SfCartesianChart.BindingContext>
134+
<viewModel:ViewModel/>
135+
</chart:SfCartesianChart.BindingContext>
136+
. . .
137+
<chart:SfCartesianChart.Series>
138+
<chart:SplineSeries ItemsSource="{Binding Data}"
139+
XBindingPath="Country"
140+
YBindingPath="Counts" ShowDataLabels="True"/>
141+
</chart:SfCartesianChart.Series>
142+
143+
</chart:SfCartesianChart>
144+
```
145+
**[C#]**
146+
147+
```
148+
SfCartesianChart chart = new SfCartesianChart();
149+
chart.BindingContext = new ViewModel();
150+
. . .
151+
var binding = new Binding() { Path = "Data" };
152+
var splineSeries = new SplineSeries()
153+
{
154+
XBindingPath = "Country ",
155+
YBindingPath = "Counts",
156+
ShowDataLabels = true
157+
};
158+
159+
splineSeries.SetBinding(ChartSeries.ItemsSourceProperty, binding);
160+
chart.Series.Add(splineSeries);
161+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31611.283
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Syncfusion_Dot_NET_MAUI_Spline_Charts", "Syncfusion_Dot_NET_MAUI_Spline_Charts\Syncfusion_Dot_NET_MAUI_Spline_Charts.csproj", "{EFD32A17-1F17-4C04-911E-3D65B207B6CD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
Release-Xml|Any CPU = Release-Xml|Any CPU
13+
EndGlobalSection
14+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
15+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
16+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
17+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
18+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Release|Any CPU.Deploy.0 = Release|Any CPU
21+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Release-Xml|Any CPU.ActiveCfg = Release|Any CPU
22+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Release-Xml|Any CPU.Build.0 = Release|Any CPU
23+
{EFD32A17-1F17-4C04-911E-3D65B207B6CD}.Release-Xml|Any CPU.Deploy.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {61F7FB11-1E47-470C-91E2-47F8143E1572}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:Syncfusion_Dot_NET_MAUI_Spline_Charts"
5+
x:Class="Syncfusion_Dot_NET_MAUI_Spline_Charts.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Syncfusion_Dot_NET_MAUI_Spline_Charts;
2+
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="Syncfusion_Dot_NET_MAUI_Spline_Charts.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:Syncfusion_Dot_NET_MAUI_Spline_Charts"
7+
Shell.FlyoutBehavior="Disabled">
8+
9+
<ShellContent
10+
Title="Home"
11+
ContentTemplate="{DataTemplate local:MainPage}"
12+
Route="MainPage" />
13+
14+
</Shell>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Syncfusion_Dot_NET_MAUI_Spline_Charts;
2+
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
5+
xmlns:model="clr-namespace:Syncfusion_Dot_NET_MAUI_Spline_Charts"
6+
x:Class="Syncfusion_Dot_NET_MAUI_Spline_Charts.MainPage">
7+
8+
<chart:SfCartesianChart>
9+
10+
<chart:SfCartesianChart.BindingContext>
11+
<model:ViewModel/>
12+
</chart:SfCartesianChart.BindingContext>
13+
14+
<chart:SfCartesianChart.Title>
15+
<Label Text="Weather Report" FontSize="20" HorizontalOptions="Center"/>
16+
</chart:SfCartesianChart.Title>
17+
18+
<chart:SfCartesianChart.XAxes>
19+
<chart:CategoryAxis ShowMajorGridLines="False" PlotOffsetStart="10" PlotOffsetEnd="30" />
20+
</chart:SfCartesianChart.XAxes>
21+
22+
<chart:SfCartesianChart.YAxes>
23+
<chart:NumericalAxis Interval="10" >
24+
<chart:NumericalAxis.LabelStyle>
25+
<chart:ChartAxisLabelStyle LabelFormat= "0.#&#186;C" />
26+
</chart:NumericalAxis.LabelStyle>
27+
<chart:NumericalAxis.AxisLineStyle>
28+
<chart:ChartLineStyle StrokeWidth="0"/>
29+
</chart:NumericalAxis.AxisLineStyle>
30+
</chart:NumericalAxis>
31+
</chart:SfCartesianChart.YAxes>
32+
33+
<chart:SplineSeries Label="Height"
34+
EnableTooltip="True"
35+
ShowDataLabels="True"
36+
ItemsSource="{Binding Data}"
37+
XBindingPath="Name"
38+
YBindingPath="Height">
39+
</chart:SplineSeries>
40+
</chart:SfCartesianChart>
41+
42+
</ContentPage>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Syncfusion_Dot_NET_MAUI_Spline_Charts;
2+
3+
public partial class MainPage : ContentPage
4+
{
5+
public MainPage()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Syncfusion.Maui.Core.Hosting;
2+
3+
namespace Syncfusion_Dot_NET_MAUI_Spline_Charts;
4+
5+
public static class MauiProgram
6+
{
7+
public static MauiApp CreateMauiApp()
8+
{
9+
var builder = MauiApp.CreateBuilder();
10+
builder
11+
.UseMauiApp<App>()
12+
.ConfigureSyncfusionCore()
13+
.ConfigureFonts(fonts =>
14+
{
15+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
16+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
17+
});
18+
19+
return builder.Build();
20+
}
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
</manifest>

0 commit comments

Comments
 (0)