How to create a Column Chart in .NET MAUI?
A .NET MAUI Column Chart is a visual representation of changing data that is created with a high level of user interactive. This section explains how to create a beautiful .NET MAUI Column Charts.
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 Column Chart.
Public class Model
{
public string Country { get; set; }
public double Counts { get; set; }
public Model(string name , double count)
{
Country = name;
Counts = count;
}
}
Create a ViewModel class and initialize a list of objects as shown in the following code sample.
public class ViewModel
{
public ObservableCollection<Model> Data { get; set; }
public ViewModel()
{
Data = new ObservableCollection<Model>()
{
new Model("Korea",39),
new Model("India",20),
new Model("Africa", 61),
new Model("China",65),
new Model("France",45),
};
}
}
Set the ViewModel instance as the BindingContext of the chart; this is done to bind properties of ViewModel to SfCartesianChart.
Add the namespace of ViewModel class to your XAML page, if you prefer to set BindingContext in XAML.
[XAML]
xmlns:viewModel ="clr-namespace:MauiApp"
. . .
<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 Column Charts
As we are going to visualize the comparison of annual internet users count in the data model, add ColumnSeries to SfCartesianChart.Series property, and then bind the Data property of the above ViewModel to the ColumnSeries ItemsSource property as shown in the following code sample.
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.BindingContext>
<viewModel:ViewModel/>
</chart:SfCartesianChart.BindingContext>
. . .
<chart:SfCartesianChart.Series>
<chart:ColumnSeries ItemsSource="{Binding Data}"
XBindingPath="Country"
YBindingPath="Counts" ShowDataLabels="True"/>
</chart:SfCartesianChart.Series>
</chart:SfCartesianChart>
[C#]
SfCartesianChart chart = new SfCartesianChart();
chart.BindingContext = new ViewModel();
. . .
var binding = new Binding() { Path = "Data" };
var columnSeries = new ColumnSeries()
{
XBindingPath = "Country ",
YBindingPath = "Counts",
ShowDataLabels = true
};
columnSeries.SetBinding(ChartSeries.ItemsSourceProperty, binding);
chart.Series.Add(columnSeries);
Output
Hope you enjoyed learning about the quick getting started with .NET MAUI Column Charts. You can explore the runnable demo from this Github location.
Conclusion
I hope you enjoyed learning about how to create a Column Chart in .NET MAUI.
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!