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:
- Install .NET 8 SDK or later.
- Set up a .NET MAUI environment with Visual Studio 2022 (v17.8 or later).
Step 1: Create a new .NET MAUI project
- Go to File > New > Project and choose the .NET MAUI App template.
- Name the project and choose a location. Click Next.
- Select the .NET framework version and click Create.
Step 2: Install the Syncfusion® .NET MAUI Toolkit Package
- In Solution Explorer, right-click the project and choose Manage NuGet Packages.
- Search for Syncfusion.Maui.Toolkit and install the latest version.
- 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
- To initialize the control, import the
Syncfusion.Maui.Toolkit.SparkCharts
namespace into your code. - 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:
- Install .NET 8 SDK or later is installed.
- Set up a .NET MAUI environment with Visual Studio Code.
- Ensure that the .NET MAUI extension is installed and configured as described here
Step 1: Create a new .NET MAUI project
- Open the command palette by pressing
Ctrl+Shift+P
and type .NET:New Project and enter. - Choose the .NET MAUI App template.
- Select the project location, type the project name and press Enter.
- Then choose Create project.
Step 2: Install the Syncfusion® .NET MAUI Toolkit Package
- Press Ctrl + ` (backtick) to open the integrated terminal in Visual Studio Code.
- Ensure you’re in the project root directory where your .csproj file is located.
- Run the command
dotnet add package Syncfusion.Maui.Toolkit
to install the Syncfusion® .NET MAUI Toolkit NuGet package. - 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
- To initialize the control, import the
Syncfusion.Maui.Toolkit.SparkCharts
namespace. - 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:
- Ensure you have the latest version of JetBrains Rider.
- Install .NET 8 SDK or later is installed.
- Make sure the MAUI workloads are installed and configured as described here.
Step 1: Create a new .NET MAUI Project
- Go to File > New Solution, Select .NET (C#) and choose the .NET MAUI App template.
- Enter the Project Name, Solution Name, and Location.
- Select the .NET framework version and click Create.
Step 2: Install the Syncfusion® MAUI Toolkit NuGet Package
- In Solution Explorer, right-click the project and choose Manage NuGet Packages.
- Search for Syncfusion.Maui.Toolkit and install the latest version.
- 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
- To initialize the control, import the
Syncfusion.Maui.Toolkit.SparkCharts
namespace. - 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 setBindingContext
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.
You can find the complete getting started sample from this link.