How to Configure the Syncfusion Theme for .NET MAUI Chart(SfCartesianChart)?
In this article, we will learn how to configure and customize the .NET MAUI Syncfusion theme for the SfCartesianChart. Syncfusion themes enable you to apply colors uniformly to .NET MAUI Cartesian Charts, providing a consistent look and feel for your applications.
Let’s take a look at the following sections:
- How to Set the Theme for the Application
- Dynamically Changing Themes
- Overriding the Default Theme
How to Set the Theme for the Application
By default, Syncfusion provides support for both light and dark themes through the inclusion of a SyncfusionThemeResourceDictionary.
Initialize and assign the desired theme in your application, modify the App.xaml file as follows:
<Application xmlns:syncTheme="clr-namespace:Syncfusion.Maui.Themes;assembly=Syncfusion.Maui.Core">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<syncTheme:SyncfusionThemeResourceDictionary VisualTheme="MaterialLight"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Light Theme
Dark Theme
Dynamically Changing Themes
Syncfusion allows for dynamic theme switching within your application. This can be achieved by updating the VisualTheme property at runtime. Here is an example in C#:
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
if (mergedDictionaries != null)
{
var theme = mergedDictionaries.OfType<SyncfusionThemeResourceDictionary>().FirstOrDefault();
if (theme != null)
{
if (state == false)
{
theme.VisualTheme = SfVisuals.MaterialLight;
}
else
{
theme.VisualTheme = SfVisuals.MaterialDark
}
}
}
Overriding the Default Theme
The theme resource dictionary contains a set of keys that are mapped to styles in control style dictionaries. The default appearance of themes can be customized by overriding these key values. You can find the keys for the Cartesian Chart here: Cartesian Chart Theme Keys.
For example, here the major grid lines and tooltip background values are customized by overriding theme keys.
<Application xmlns:syncTheme="clr-namespace:Syncfusion.Maui.Themes;assembly=Syncfusion.Maui.Core">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<syncTheme:SyncfusionThemeResourceDictionary VisualTheme="MaterialLight"/>
<ResourceDictionary>
<Color x:Key="SfCartesianChartMajorGridLineStroke">Lime</Color>
<Color x:Key="SfCartesianChartTooltipBackground">Pink</Color>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
For more information, please refer to the Syncfusion .NET MAUI Cartesian Chart Theme Demo
Conclusion
I hope you enjoyed learning how to configure and customize themes in your .NET MAUI applications using Syncfusion’s SfCartesianChart control.
You can refer to our .NET MAUI CartesianChart 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 components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trail to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!