The WinUI Spline Chart is similar to a line chart, but the difference between them is that instead of connecting the data points with straight lines, the data points are connected by smooth Bezier curves. This section explains how to create the WinUI Spline Chart.
The user guide Documentation helps you to acquire more knowledge on charts and their features. You can also refer to the Feature Tour site to get an overview of all the features in a chart.
Create a simple project using the instructions given in the Getting Started with your first WinUI app documentation.
Add Syncfusion.Chart.WinUI NuGet to the project and import the SfCartesianChart namespace as follows.
[XAML]
xmlns:chart="using:Syncfusion.UI.Xaml.Charts"
[C#]
using Syncfusion.UI.Xaml.Charts;
Initialize an empty chart with the 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 XAxes
CategoryAxis xAxis = new CategoryAxis();
chart.XAxes.Add.(xAxis);
//Initializing YAxes
NumericalAxis yAxis = new NumericalAxis();
chart.YAxes.Add.(yAxis);
this.Content = chart;
Initialize a data model that represents a data point for a Spline Chart. [C#]
public class Model
{
public string Year { get; set; }
public double Counts { get; set; }
public Model(string name, double count)
{
Year = name;
Counts = count;
}
}
Create the ViewModel
class with the Data Collection
property using the above model and initialize a list of objects as shown in the following code sample.
[C#]
public class ViewModel
{
public ObservableCollection<Model> Data { get; set; }
public ViewModel()
{
Data = new ObservableCollection<Model>()
{
new Model("1925", 415),
new Model("1926", 408),
new Model("1927", 415),
new Model("1928", 350),
new Model("1929", 375),
new Model("1930", 500),
new Model("1931", 390),
new Model("1932", 450),
};
}
}
Set the ViewModel
instance as the DataContext
of your window; this is done to bind properties of the ViewModel
to the chart.
Note: Add the namespace of the
ViewModel
class to your XAML page, if you prefer to set theDataContext
in XAML.
[XAML]
xmlns:viewModel="using:CartesianChartDesktop"
. . .
<chart:SfCartesianChart>
<chart:SfCartesianChart.DataContext>
<viewModel:ViewModel/>
</chart:SfCartesianChart.DataContext>
</chart:SfCartesianChart>
[C#]
SfCartesianChart chart = new SfCartesianChart();
chart.DataContext = new ViewModel();
Populate the chart with the data.
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 in the following code sample.
Note: Need to set XBindingPath and YBindingPath properties so that series will fetch values from the respective properties in the data model to plot the series.
[XAML]
<chart:SfCartesianChart>
<chart:SfCartesianChart.DataContext>
<viewModel:ViewModel/>
</chart:SfCartesianChart.DataContext>
. . .
<chart:SfCartesianChart.Series>
<chart:SplineSeries ItemsSource="{Binding Data}"
XBindingPath="Year"
YBindingPath="Counts"
ShowDataLabels="True" >
</chart:SplineSeries>
</chart:SfCartesianChart.Series>
</chart:SfCartesianChart>
[C#]
SfCartesianChart chart = new SfCartesianChart();
chart.DataContext = new ViewModel();
. . .
SplineSeries series = new SplineSeries();
series.SetBinding(SplineSeries.ItemsSourceProperty, new Binding() { Path = new PropertyPath("Data") });
series.XBindingPath = " Year";
series.YBindingPath = " Counts";
series.ShowDataLabels = true;
chart.Series.Add(series);
this.Content = chart;
KB article - How to create a WinUI Spline Chart (SfCartesianChart)?