Articles in this section
Category / Section

How to create a .NET MAUI Spline Chart (SfCartesianChart)?

5 mins read

A .NET MAUI Chart provides a visual representation of data, suitable for scenarios involving both linear and rotational motions. This section explains how to create a beautiful .NET MAUI Spline Chart using the SfCartesianChart.

 

Register the handler

Ensure Syncfusion.Maui.Core NuGet package is installed, as it is a dependency for all Syncfusion® controls in .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


Define a simple data model that represents a data point for the .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 the ViewModel to the SfCartesianChart.


Note:

Add the namespace of the ViewModel class to your XAML page if you prefer to set the 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 a SplineSeries to the 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.


Note:

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

Chart, line in .NET MAUI Chart


Download the complete sample on GitHub.

Conclusion

I hope you enjoyed learning how to create a .NET MAUI Spline Chart (SfCartesianChart).

You can refer to our .NET MAUI Chart feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI Chart Documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Chart and other .NET MAUI components.

Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied