How to create a .NET MAUI Spline Chart (SfCartesianChart)?
A .NET MAUI Chart is a visual representation of data when both linear and rotational motion is desired. This section explains how to create a beautiful .NET MAUI Spline Chart.
Register the handler
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 to this link.
Initialize a Chart
Import the SfCartesianChart namespace as follows:
[XAML]
xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
[C#]
using Syncfusion.Maui.Charts;
Initialize an empty chart with XAxes and YAxes as shown in the following code sample:
[XAML]
<chart:SfCartesianChart> <chart:SfCartesianChart.XAxes > <chart:CategoryAxis/> </chart:SfCartesianChart.XAxes> <chart:SfCartesianChart.YAxes> <chart:NumericalAxis/> </chart:SfCartesianChart.YAxes> </chart:SfCartesianChart>
[C#]
SfCartesianChart chart = new SfCartesianChart(); //Initializing Primary Axis CategoryAxis primaryAxis = new CategoryAxis(); chart.XAxes.Add(primaryAxis); //Initializing Secondary Axis NumericalAxis secondaryAxis = new NumericalAxis(); chart.YAxes.Add(secondaryAxis); this.Content = chart;
Initialize ViewModel
Now, define a simple data model that represents a data point for .NET MAUI Spline Chart.
Public class Model { public string Day { get; set; } public double Degree { get; set; } public Model(string day , double degree) { Day = day; Degree = degree; } }
Create a ViewModel class and initialize a list of objects as shown in the following code sample:
public class ViewModel { public List<Model> Data { get; set; } public ViewModel() { Data = new List<Model>() { new Model ("Sun",80), new Model ("Mon",60 ), new Model ("Tue",80), new Model ("Wed",20), new Model ("Thu",70), new Model ("Fri",70), new Model ("Sat",70), }; } }
Set the ViewModel instance as the BindingContext of the chart; this is done to bind properties of ViewModel to SfCartesianChart.
Add the namespace of the ViewModel class to your XAML page, if you prefer to set BindingContext in XAML.
[XAML]
xmlns:viewModel ="clr-namespace: Syncfusion_Dot_NET_MAUI_Spline_Charts" . . . <chart:SfCartesianChart> <chart:SfCartesianChart.BindingContext> <viewModel:ViewModel/> </chart:SfCartesianChart.BindingContext> </chart:SfCartesianChart>
[C#]
SfCartesianChart chart = new SfCartesianChart(); chart.BindingContext = new ViewModel();
How to populate data in .NET MAUI Spline Charts
As we are going to visualize the comparison of weather reports 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 in the following code sample.
You need to set XBindingPath and YBindingPath properties so that the series will fetch values from the respective properties in the data model to plot the series.
[XAML]
<chart:SfCartesianChart> <chart:SfCartesianChart.BindingContext> <viewModel:ViewModel/> </chart:SfCartesianChart.BindingContext> . . . <chart:SfCartesianChart.Series> <chart:SplineSeries ItemsSource="{Binding Data}" XBindingPath="Day" YBindingPath="Degree" ShowDataLabels="True"/> </chart:SfCartesianChart.Series> </chart:SfCartesianChart>
[C#]
SfCartesianChart chart = new SfCartesianChart(); chart.BindingContext = new ViewModel(); . . . var binding = new Binding() { Path = "Data" }; var splineSeries = new SplineSeries() { XBindingPath = "Day", YBindingPath = "Degree", ShowDataLabels = true }; splineSeries.SetBinding(ChartSeries.ItemsSourceProperty, binding); chart.Series.Add(splineSeries);
Output
See also
Conclusion
I hope you enjoyed learning about How to create a .NET MAUI Spline Chart(SfCartesianChart).
You can refer to our .NET MAUI Chart’s feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI Chart Documentation to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI Chart and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!