|
1 | 1 | # How-to-create-a-.NET-MAUI-Spline-Chart |
2 | 2 | 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 | + |
| 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 | +``` |
0 commit comments