Themes

A theme sets the default look of every chart in the app: the color palette, the paints and sizes of series and axes, the tooltip and legend, and the hover effects. The theme only fills the properties you did not set: a property set in code or XAML keeps its value, the theme never replaces it. When no theme is registered, LiveCharts adds the default theme, which follows the light or dark mode of the app.

Every member is listed in the API explorer: Theme, ThemesExtensions (the default theme) and LiveChartsthemeExtensions (the theme rules).

Light and dark

The default theme has a light and a dark variant. The dark one uses a lighter palette, a black background and light text:

image

Where the UI framework reports the theme of the app, the chart follows it and switches when it changes:

  • Avalonia: the ActualThemeVariant of the application.
  • WinUI and Uno: the ActualTheme of the chart control, so a RequestedTheme set on the app or on a parent element applies to the chart.
  • MAUI: the RequestedTheme of the application, which is the UserAppTheme when the app sets one, or the theme of the system.
  • WPF, WinForms, Blazor and Eto: these frameworks do not report a theme, the chart is always light unless you force the dark variant.

Force light or dark

When the UI framework reports a theme, the recommended way to get a dark chart is to set the theme of the app, so the charts match the rest of the UI. Otherwise, register the variant you want when the app starts:

LiveCharts.Configure(config => config
    .AddDarkTheme()); // or .AddLightTheme() // mark

AddDarkTheme() and AddLightTheme() are shortcuts for AddDefaultTheme(requestedTheme: LvcThemeKind.Dark) and AddDefaultTheme(requestedTheme: LvcThemeKind.Light). LvcThemeKind.Unknown, the default, follows the app.

Customize the default theme

AddDefaultTheme, AddDarkTheme and AddLightTheme take a function that adds your own rules to the default theme. Your rules run after the default ones, so they override them. This sample changes the palette, the animations, the corner radius of the bars and the separators of the axes:

using System;
using LiveChartsCore;
using LiveChartsCore.Kernel;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.Themes;
using SkiaSharp;

namespace ViewModelsSamples;

public static class LiveChartsThemeExtensions
{
    // now in the AddMyCustomTheme function, we use the default theme // mark
    // but we also override some properties, like the animations speed, easing function and colors // mark
    public static LiveChartsSettings AddMyCustomTheme(this LiveChartsSettings settings) =>
        settings.AddDefaultTheme(theme => theme
            .OnInitialized(() =>
            {
                // the OnInitialized method is called when the // mark
                // theme is applied to a chart for the first time // mark
                // here we can define colors, palettes, etc. // mark
                theme.Colors = [
                    new(66, 165, 245),
                    new(30, 136, 229),
                    new(21, 101, 192)
                ];
            })
            .HasRuleForChart(chart =>
            {
                // animations are configured on the chart view itself; // mark
                // a rule here applies them to every chart unless the user sets them explicitly // mark
                chart.AnimationsSpeed = TimeSpan.FromSeconds(1);
                chart.EasingFunction = EasingFunctions.BounceOut;
            })
            .HasRuleForBarSeries(series =>
            {
                // this method is called when styling a column or row series // mark
                // here we define the corner radius // mark
                series.Rx = 10;
                series.Ry = 10;
            })
            .HasRuleForAxes(axis =>
            {
                if (axis is not ICartesianAxis cartesianAxis) return;

                // Define the color based on the system and app theme // mark
                // this only works on WinUI, Avalonia, Uno and Maui // mark
                // because these frameworks provide a way to detect the system theme // mark
                SKColor lineColor = theme.IsDark
                    ? new(90, 90, 90)
                    : new(235, 235, 235);

                axis.SeparatorsPaint = new SolidColorPaint(lineColor, 2);
                axis.SeparatorsPaint = new SolidColorPaint(lineColor, 1);
            })

        // for more information about the theme configuration, please see the // mark
        // source code of the default theme: // mark
        // https://github.com/Live-Charts/LiveCharts2/blob/f904fac8988c4792d2570e8245736e33479e1238/src/skiasharp/LiveChartsCore.SkiaSharp/ThemesExtensions.cs

        );
}

Then register it when the app starts:

LiveCharts.Configure(config => config.AddMyCustomTheme());

The theme API:

  • OnInitialized runs the first time a chart uses the theme, and again every time the app switches between light and dark. Set the values that are not rules there, such as theme.Colors, the palette.
  • HasRuleForAxes, HasRuleForAnySeries and one rule per series type (HasRuleForLineSeries, HasRuleForBarSeries, HasRuleForPieSeries, HasRuleForHeatSeries and so on) run for each axis or series when a chart draws it the first time, and again when the app switches between light and dark. HasPostRuleForAnySeries runs after the rules of the series type, when the series already has its paints.
  • HasRuleForChart styles the chart control itself, see the next section.
  • theme.IsDark tells a rule which variant is active.

Each series takes the color of the palette at its position in the chart's series collection; when there are more series than colors, the palette starts again. ColorPalletes has some palettes ready to use, such as ColorPalletes.MaterialDesign500, the one of the light variant, and ColorPalletes.FluentDesign.

The source code of the default theme is a complete example of every rule: ThemesExtensions.cs.

Chart properties

Besides series and axes, a theme can set the properties of the chart control (the IChartView), for example the legend and tooltip paints and sizes, the animations speed or the draw margin. Use HasRuleForChart; the rule runs on every platform, so you do not need a XAML style per platform.

LiveCharts.Configure(config => config
    .AddDefaultTheme(theme => theme
        .HasRuleForChart(chart =>
        {
            chart.LegendTextSize = 14;
            chart.TooltipTextSize = 14;
            chart.AnimationsSpeed = TimeSpan.FromMilliseconds(500);
        })));

As with series and axes, a value you set on the chart in code or XAML, or bind, wins over the rule.

Your own theme

HasTheme registers a theme built from scratch, with none of the default rules. Such a theme must define the default tooltip and legend and a palette. Anything else it does not set keeps the default value of the property: for example, an axis with no LabelsPaint draws no labels.

LiveCharts.Configure(config => config
    .HasTheme(theme => theme // mark
        .HasDefaultTooltip(() => new SKDefaultTooltip()) // mark
        .HasDefaultLegend(() => new SKDefaultLegend()) // mark
        .OnInitialized(() => theme.Colors = ColorPalletes.FluentDesign) // mark
        .HasRuleForLineSeries(series =>
        {
            // configure the line series here...
        })));

The theme registered in LiveCharts.Configure applies to every chart. To give one chart a different theme, set its ChartTheme property.

~/overview/1.12.themes.md