Articles in this section

How to create a Column Chart in .NET MAUI?

A .NET MAUI Column Chart provides a visual representation of changing data and is designed with a high level of user interactivity. This section explains how to create a beautiful .NET MAUI Column Chart using the SfCartesianChart.


Register the handler

Ensure the Syncfusion.Maui.Core NuGet package is installed, as it is a dependency 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

Define a simple data model that represents a data point for the .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 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: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 user 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.


Note:

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: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

NET MAUI Column Charts


Download the complete sample on GitHub.


Conclusion

I hope you enjoyed learning how to create a Column Chart in .NET MAUI.

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)
Access denied
Access denied