Getting Started with .NET MAUI Spark Charts (SparkChart)

25 Sep 202514 minutes to read

This section explains how to populate the spark chart with data, configure the chart type, enable markers and data labels, and customize its appearance. It also covers the essential aspects for getting started with the spark chart.

Prerequisites

Before proceeding, ensure that the following are set up:

  1. Install .NET 8 SDK or later.
  2. Set up a .NET MAUI environment with Visual Studio 2022 (v17.8 or later).

Step 1: Create a new .NET MAUI project

  1. Go to File > New > Project and choose the .NET MAUI App template.
  2. Name the project and choose a location. Click Next.
  3. Select the .NET framework version and click Create.

Step 2: Install the Syncfusion® .NET MAUI Toolkit Package

  1. In Solution Explorer, right-click the project and choose Manage NuGet Packages.
  2. Search for Syncfusion.Maui.Toolkit and install the latest version.
  3. Ensure the necessary dependencies are installed correctly, and the project is restored.

Step 3: Register the handler

In the MauiProgram.cs file, register the handler for Syncfusion® Toolkit.

using Syncfusion.Maui.Toolkit.Hosting;

    public static class MauiProgram
    {
	    public static MauiApp CreateMauiApp()
	    {
	        var builder = MauiApp.CreateBuilder();
		    builder
			    .ConfigureSyncfusionToolkit()
			    .UseMauiApp<App>()
			    .ConfigureFonts(fonts =>
			    {
				    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			    });

		    return builder.Build();
	    }
    }

Step 4: Add .NET MAUI Spark Charts

  1. To initialize the control, import the Syncfusion.Maui.Toolkit.SparkCharts namespace into your code.
  2. Initialize an instance of the SfSparkLineChart control.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sparkchart="clr-namespace:Syncfusion.Maui.Toolkit.SparkCharts;assembly=Syncfusion.Maui.Toolkit"
             x:Class="GettingStarted.MainPage">

        <sparkchart:SfSparkLineChart/>

</ContentPage>
using Syncfusion.Maui.Toolkit.SparkCharts; 

. . .

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        SfSparkLineChart sparkchart = new SfSparkLineChart(); 
        this.Content = sparkchart; 
    }
}

Prerequisites

Before proceeding, ensure that the following are set up:

  1. Install .NET 8 SDK or later is installed.
  2. Set up a .NET MAUI environment with Visual Studio Code.
  3. Ensure that the .NET MAUI extension is installed and configured as described here

Step 1: Create a new .NET MAUI project

  1. Open the command palette by pressing Ctrl+Shift+P and type .NET:New Project and enter.
  2. Choose the .NET MAUI App template.
  3. Select the project location, type the project name and press Enter.
  4. Then choose Create project.

Step 2: Install the Syncfusion® .NET MAUI Toolkit Package

  1. Press Ctrl + ` (backtick) to open the integrated terminal in Visual Studio Code.
  2. Ensure you’re in the project root directory where your .csproj file is located.
  3. Run the command dotnet add package Syncfusion.Maui.Toolkit to install the Syncfusion® .NET MAUI Toolkit NuGet package.
  4. To ensure all dependencies are installed, run dotnet restore.

Step 3: Register the handler

In the MauiProgram.cs file, register the handler for Syncfusion® Toolkit.

using Syncfusion.Maui.Toolkit.Hosting;

    public static class MauiProgram
    {
	    public static MauiApp CreateMauiApp()
	    {
	        var builder = MauiApp.CreateBuilder();
		    builder
			    .ConfigureSyncfusionToolkit()
			    .UseMauiApp<App>()
			    .ConfigureFonts(fonts =>
			    {
				    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			    });

		    return builder.Build();
	    }
    }

Step 4: Add .NET MAUI Spark Charts

  1. To initialize the control, import the Syncfusion.Maui.Toolkit.SparkCharts namespace.
  2. Initialize SfSparkLineChart.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sparkchart="clr-namespace:Syncfusion.Maui.Toolkit.SparkCharts;assembly=Syncfusion.Maui.Toolkit"
             x:Class="GettingStarted.MainPage">

        <sparkchart:SfSparkLineChart/>

</ContentPage>
using Syncfusion.Maui.Toolkit.SparkCharts; 

. . .

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        SfSparkLineChart sparkchart = new SfSparkLineChart(); 
        this.Content = sparkchart; 
    }
}

Prerequisites

Before proceeding, ensure the following are set up:

  1. Ensure you have the latest version of JetBrains Rider.
  2. Install .NET 8 SDK or later is installed.
  3. Make sure the MAUI workloads are installed and configured as described here.

Step 1: Create a new .NET MAUI Project

  1. Go to File > New Solution, Select .NET (C#) and choose the .NET MAUI App template.
  2. Enter the Project Name, Solution Name, and Location.
  3. Select the .NET framework version and click Create.

Step 2: Install the Syncfusion® MAUI Toolkit NuGet Package

  1. In Solution Explorer, right-click the project and choose Manage NuGet Packages.
  2. Search for Syncfusion.Maui.Toolkit and install the latest version.
  3. Ensure the necessary dependencies are installed correctly, and the project is restored. If not, Open the Terminal in Rider and manually run: dotnet restore

Step 3: Register the handler

In the MauiProgram.cs file, register the handler for Syncfusion® core.

using Syncfusion.Maui.Toolkit.Hosting;

    public static class MauiProgram
    {
	    public static MauiApp CreateMauiApp()
	    {
	        var builder = MauiApp.CreateBuilder();
		    builder
			    .ConfigureSyncfusionToolkit()
			    .UseMauiApp<App>()
			    .ConfigureFonts(fonts =>
			    {
				    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			    });

		    return builder.Build();
	    }
    }

Step 4: Add .NET MAUI Spark Charts

  1. To initialize the control, import the Syncfusion.Maui.Toolkit.SparkCharts namespace.
  2. Initialize SfSparkLineChart.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sparkchart="clr-namespace:Syncfusion.Maui.Toolkit.SparkCharts;assembly=Syncfusion.Maui.Toolkit"
             x:Class="GettingStarted.MainPage">

        <sparkchart:SfSparkLineChart/>

</ContentPage>
using Syncfusion.Maui.Toolkit.SparkCharts;

. . .

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        SfSparkLineChart sparkchart = new SfSparkLineChart();
        this.Content = sparkchart;
    }
}

Initialize view model

Define a simple data model to represent a data point in the chart:

public class SparkDataModel
{
    public double Value { get; set; }
}

Next, create a SparkChartViewModel class that holds a list of SparkDataModel objects as follows.

public class SparkChartViewModel
{
    public List<SparkDataModel> Data { get; set; }

    public SparkChartViewModel()
    {
        Data = new List<SparkDataModel>()
        {
            new SparkDataModel(){ Value = 5000},
            new SparkDataModel(){ Value = 9000},
            new SparkDataModel(){ Value = 5000},
            new SparkDataModel(){ Value = 0},
            new SparkDataModel(){ Value = 3000},
            new SparkDataModel(){ Value = -4000},
            new SparkDataModel(){ Value = 5000},
            new SparkDataModel(){ Value = 0},
            new SparkDataModel(){ Value = 9000},
            new SparkDataModel(){ Value = -9000},
        };
    }
}

Create a SparkChartViewModel instance and set it as the spark chart’s BindingContext. This enables property binding from the SparkChartViewModel class.

NOTE

Add the namespace of the SparkChartViewModel 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"
             xmlns:sparkchart="clr-namespace:Syncfusion.Maui.Toolkit.SparkCharts;assembly=Syncfusion.Maui.Toolkit"
             xmlns:model="clr-namespace:GettingStarted"
             x:Class="GettingStarted.MainPage">
    <sparkchart:SfSparkLineChart>
        <sparkchart:SfSparkLineChart.BindingContext>
            <model:SparkChartViewModel/>
        </sparkchart:SfSparkLineChart.BindingContext>
    </sparkchart:SfSparkLineChart>
</ContentPage>
SfSparkLineChart sparkchart = new SfSparkLineChart();
SparkChartViewModel viewModel = new SparkChartViewModel();
sparkchart.BindingContext = viewModel;

Populate chart with data

Binding Data to the spark chart ItemsSource property from its BindingContext to create our own spark chart.

<sparkchart:SfSparkLineChart ItemsSource="{Binding Data}" 
                    YBindingPath="Value">
. . .
</sparkchart:SfSparkLineChart>
SfSparkLineChart sparkchart = new SfSparkLineChart()
{
    ItemsSource = new SparkChartViewModel().Data,
    YBindingPath = "Value",
};

this.Content = sparkchart;

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"
             xmlns:sparkchart="clr-namespace:Syncfusion.Maui.Toolkit.SparkCharts;assembly=Syncfusion.Maui.Toolkit"
             xmlns:model="clr-namespace:GettingStarted"
             x:Class="GettingStarted.MainPage">

    <sparkchart:SfSparkLineChart ItemsSource="{Binding Data}" 
            YBindingPath="Value">
        <sparkchart:SfSparkLineChart.BindingContext>
            <model:SparkChartViewModel/>
        </sparkchart:SfSparkLineChart.BindingContext>

    </sparkchart:SfSparkLineChart>

</ContentPage>
using Syncfusion.Maui.Toolkit.SparkCharts;

. . .

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        SparkChartViewModel viewModel = new SparkChartViewModel();
        SfSparkLineChart sparkchart = new SfSparkLineChart()
        {
            ItemsSource = viewModel.Data,
            YBindingPath = "Value",
            BindingContext = viewModel;
        };
        this.Content = sparkchart;
    }
}

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

Spark Line Chart in MAUI Spark Chart

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