Getting Started with .NET MAUI Chart

14 Mar 202321 minutes to read

This section explains how to populate the Cartesian chart with data, a title, data labels, a legend, and tooltips, as well as the essential aspects for getting started with the chart.

To get start quickly with our .NET MAUI Cartesian Chart, you can check the below video.

Creating an application with .NET MAUI chart

  1. Create a new .NET MAUI application in Visual Studio.
  2. Syncfusion .NET MAUI components are available in nuget.org. To add SfCartesianChart to your project, open the NuGet package manager in Visual Studio, search for Syncfusion.Maui.Charts and then install it.
  3. To initialize the control, import the Chart namespace.
  4. Initialize SfCartesianChart.
<ContentPage
    . . .    
    xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts">
    <Grid>
        <chart:SfCartesianChart/>
    </Grid>
</ContentPage>
using Syncfusion.Maui.Charts;
namespace ChartGettingStarted
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();           
            SfCartesianChart chart = new SfCartesianChart(); 
        }
    }   
}

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.

  • C#
  • using Microsoft.Maui;
    using Microsoft.Maui.Hosting;
    using Microsoft.Maui.Controls.Compatibility;
    using Microsoft.Maui.Controls.Hosting;
    using Microsoft.Maui.Controls.Xaml;
    using Syncfusion.Maui.Core.Hosting;
    
    namespace ChartGettingStarted
    {
        public static class MauiProgram
        {
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                .UseMauiApp<App>()
                .ConfigureSyncfusionCore()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });
    
                return builder.Build();
            }
        }
    }

    Initialize view model

    Now, let us define a simple data model that represents a data point in the chart.

    public class Person   
    {   
        public string Name { get; set; }
        public double Height { get; set; }
    }

    Next, create a view model class and initialize a list of Person objects as follows.

    public class ViewModel  
    {
        public List<Person> Data { get; set; }      
    
        public ViewModel()       
        {
            Data = new List<Person>()
            {
                new Person { Name = "David", Height = 170 },
                new Person { Name = "Michael", Height = 96 },
                new Person { Name = "Steve", Height = 65 },
                new Person { Name = "Joel", Height = 182 },
                new Person { Name = "Bob", Height = 134 }
            }; 
        }
     }

    Set the ViewModel instance as the BindingContext of your page to bind ViewModel properties to the chart.

    NOTE

    Add namespace of ViewModel class to your XAML Page, if you prefer to set BindingContext in XAML.

    <ContentPage
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="ChartGettingStarted.MainPage"
        xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
        xmlns:model="clr-namespace:ChartGettingStarted">
    
        <ContentPage.BindingContext>
            <model:ViewModel></model:ViewModel>
        </ContentPage.BindingContext>
    </ContentPage>
    this.BindingContext = new ViewModel();

    Initialize Chart axis

    ChartAxis is used to locate the data points inside the chart area. The XAxes and YAxes collection of the chart is used to initialize the axis for the chart.

    <chart:SfCartesianChart>                            
        <chart:SfCartesianChart.XAxes>
            <chart:CategoryAxis/>
        </chart:SfCartesianChart.XAxes>
        <chart:SfCartesianChart.YAxes>
            <chart:NumericalAxis/>
        </chart:SfCartesianChart.YAxes>                       
    </chart:SfCartesianChart>
    SfCartesianChart chart = new SfCartesianChart();
        CategoryAxis primaryAxis = new CategoryAxis();
        chart.XAxes.Add(primaryAxis);
        NumericalAxis secondaryAxis = new NumericalAxis();
        chart.YAxes.Add(secondaryAxis);

    Run the project and check if you get following output to make sure you have configured your project properly to add a chart.

    Initializing axis for .NET MAUI Chart

    Populate Chart with data

    As we are going to visualize the comparison of heights in the data model, add ColumnSeries to Series property of chart, and then bind the Data property of the above ViewModel to the ColumnSeries.ItemsSource as follows.

    NOTE

    The Cartesian chart has Series as its default content.

    NOTE

    You need to set XBindingPath and YBindingPath
    properties so that chart will fetch values from the respective properties in the data model to plot the series.

    <chart:SfCartesianChart>
        <chart:SfCartesianChart.XAxes>
            <chart:CategoryAxis>
                <chart:CategoryAxis.Title>
                    <chart:ChartAxisTitle Text="Name" />
                </chart:CategoryAxis.Title>
            </chart:CategoryAxis>
        </chart:SfCartesianChart.XAxes>
        <chart:SfCartesianChart.YAxes>
            <chart:NumericalAxis>
                <chart:NumericalAxis.Title>
                    <chart:ChartAxisTitle Text="Height(in cm)" />
                </chart:NumericalAxis.Title>
            </chart:NumericalAxis>
        </chart:SfCartesianChart.YAxes>
    
        <chart:ColumnSeries ItemsSource="{Binding Data}" 
                            XBindingPath="Name" 
                            YBindingPath="Height" />
    </chart:SfCartesianChart>
    SfCartesianChart chart = new SfCartesianChart();
    
    // Initializing primary axis
    CategoryAxis primaryAxis = new CategoryAxis();
    primaryAxis.Title = new ChartAxisTitle
    {
        Text = "Name",
    };
    chart.XAxes.Add(primaryAxis);
    
    //Initializing secondary Axis
    NumericalAxis secondaryAxis = new NumericalAxis();
    secondaryAxis.Title = new ChartAxisTitle
    {
        Text= "Height(in cm)",
    };
    chart.YAxes.Add(secondaryAxis);
    
    //Initialize the two series for SfChart
    ColumnSeries series = new ColumnSeries();
    series.Label = "Height";
    series.ShowDataLabels = true;
    series.ItemsSource = (new ViewModel()).Data;
    series.XBindingPath = "Name";
    series.YBindingPath = "Height";
    
    //Adding Series to the Chart Series Collection
    chart.Series.Add(series);

    Add a title

    The title of the chart provide quick information to the user about the data being plotted in the chart. The Title property is used to set title for the chart as follows.

    <Grid>
        <chart:SfCartesianChart>
            <chart:SfCartesianChart.Title>
                <Label Text="Height Comparison" />
            </chart:SfCartesianChart.Title> 
        </chart:SfCartesianChart>
    </Grid>
    SfCartesianChart chart = new SfCartesianChart();
    chart.Title = new Label
    {
        Text = "Height Comparison"
    };

    Enable the data labels

    The ShowDataLabels property of series can be used to enable the data labels to improve the readability of the chart. The label visibility is set to False by default.

    <chart:SfCartesianChart>
        . . . 
        <chart:ColumnSeries ShowDataLabels="True">
        </chart:ColumnSeries>
    </chart:SfCartesianChart>
    SfCartesianChart chart = new SfCartesianChart()
    . . .
    ColumnSeries series = new ColumnSeries();
    series.ShowDataLabels = true;
    chart.Series.Add(series);

    Enable a legend

    The legend provides information about the data point displayed in the chart. The Legend property of the chart was used to enable it.

    <chart:SfCartesianChart >
        . . .
        <chart:SfCartesianChart.Legend>
            <chart:ChartLegend/>
        </chart:SfCartesianChart.Legend>
        . . .
    </chart:SfCartesianChart>
    SfCartesianChart chart = new SfCartesianChart();
        chart.Legend = new ChartLegend ();

    NOTE

    Additionally, set label for each series using the Label property of chart series, which will be displayed in corresponding legend.

    <chart:SfCartesianChart>
        . . .
        <chart:ColumnSeries Label="Height"
                        ItemsSource="{Binding Data}"
                        XBindingPath="Name" 
                        YBindingPath="Height">
        </chart:ColumnSeries>
    </chart:SfCartesianChart>
    ColumnSeries series = new ColumnSeries (); 
    series.ItemsSource = (new ViewModel()).Data;
    series.XBindingPath = "Name"; 
    series.YBindingPath = "Height"; 
    series.Label = "Height";

    Enable tooltip

    Tooltips are used to show information about the segment, when a user hovers over a segment. Enable tooltip by setting series EnableTooltip property to true.

    <chart:SfCartesianChart>
        ...
        <chart:ColumnSeries EnableTooltip="True"
    						ItemsSource="{Binding Data}"
    						XBindingPath="Name"
    						YBindingPath="Height"/>
        ...
    </chart:SfCartesianChart>
    ColumnSeries series = new ColumnSeries();
    series.ItemsSource = (new ViewModel()).Data;
    series.XBindingPath = "Name";          
    series.YBindingPath = "Height";
    series.EnableTooltip = true;

    The following code example gives you the complete code of above configurations.

    <ContentPage
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="ChartGettingStarted.MainPage"
        xmlns:chart="clr-namespace:Syncfusion.Maui.Charts;assembly=Syncfusion.Maui.Charts"
        xmlns:model="clr-namespace:ChartGettingStarted">
    
        <ContentPage.BindingContext>
        <model:ViewModel></model:ViewModel>
        </ContentPage.BindingContext>
    
        <ContentPage.Content>
            <Grid>
                <chart:SfCartesianChart>
                    <chart:SfCartesianChart.Title>
                        <Label Text="Height Comparison"/>
                    </chart:SfCartesianChart.Title>
    
                    <chart:SfCartesianChart.Legend>
                        <chart:ChartLegend/>
                    </chart:SfCartesianChart.Legend>
        
                    <chart:SfCartesianChart.XAxes>
                        <chart:CategoryAxis>
                            <chart:CategoryAxis.Title>
                                <chart:ChartAxisTitle Text="Name"/>
                            </chart:CategoryAxis.Title>
                        </chart:CategoryAxis>
                    </chart:SfCartesianChart.XAxes>
    
                    <chart:SfCartesianChart.YAxes>
                        <chart:NumericalAxis>
                            <chart:NumericalAxis.Title>
                                <chart:ChartAxisTitle Text="Height(in cm)"/>
                            </chart:NumericalAxis.Title>
                        </chart:NumericalAxis>
                    </chart:SfCartesianChart.YAxes>
    
                <!--Initialize the series for chart-->
                <chart:ColumnSeries Label="Height" 
                        EnableTooltip="True"
                        ShowDataLabels="True"
                        ItemsSource="{Binding Data}"
                        XBindingPath="Name" 
                        YBindingPath="Height">
                    <chart:ColumnSeries.DataLabelSettings>
                        <chart:CartesianDataLabelSettings LabelPlacement="Inner"/>
                        </chart:ColumnSeries.DataLabelSettings>
                </chart:ColumnSeries>
                </chart:SfCartesianChart>
            </Grid>
        </ContentPage.Content>
    </ContentPage>
    using Syncfusion.Maui.Charts;
    namespace ChartGettingStarted
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();            
                SfCartesianChart chart = new SfCartesianChart();
    
                chart.Title = new Label
                {
                    Text = "Height Comparison"
                };
    
                // Initializing primary axis
                CategoryAxis primaryAxis = new CategoryAxis();
                primaryAxis.Title = new ChartAxisTitle
                {
                    Text = "Name",
                };
                chart.XAxes.Add(primaryAxis);
    
                //Initializing secondary Axis
                NumericalAxis secondaryAxis = new NumericalAxis();
                secondaryAxis.Title = new ChartAxisTitle
                {
                    Text= "Height(in cm)",
                };
                chart.YAxes.Add(secondaryAxis);
    
                //Initialize the two series for SfChart
                ColumnSeries series = new ColumnSeries()
                {
                    Label = "Height",
                    ShowDataLabels = true,
                    ItemsSource = (new ViewModel()).Data,
                    XBindingPath = "Name",
                    YBindingPath = "Height",
                    DataLabelSettings = new CartesianDataLabelSettings
                    {
                        LabelPlacement = DataLabelPlacement.Inner
                    }              
                };  
    
                //Adding Series to the Chart Series Collection
                chart.Series.Add(series);
                this.Content = chart;
            }
        }   
    }

    The following chart is created as a result of the previous codes.

    Getting started for .NET MAUI Chart

    You can find the complete getting started sample from this link.