~/cartesianChart/legends.md

Legends

A legend is a visual element that displays a list with the name, stroke and fills of the series in a chart:

legends

You can place a legend at Top, Bottom, Left, Right or Hidden positions, notice the Hidden position will disable legends in a chart, default value is Hidden.

Customize default legends

You can quickly change the position, the font, the text size or the background color:

using Eto.Forms;
using LiveChartsCore.SkiaSharpView.Eto;
using ViewModelsSamples.Axes.Multiple;

namespace EtoFormsSample.Axes.Multiple;

public class View : Panel
{
    private readonly CartesianChart cartesianChart;

    public View()
    {
        var viewModel = new ViewModel();

        cartesianChart = new CartesianChart
        {
            Series = viewModel.Series,
            YAxes = viewModel.YAxes,
            LegendPosition = LiveChartsCore.Measure.LegendPosition.Left,
            LegendTextPaint = viewModel.LegendTextPaint,
            LegendBackgroundPaint = viewModel.LedgendBackgroundPaint,
            LegendTextSize = 16
        };

        Content = cartesianChart;
    }
}

View model

[ObservableObject]
public partial class ViewModel
{
    public ISeries[] Series { get; set; } = { ... };
    public Axis[] YAxes { get; set; } = { ... };

    public SolidColorPaint LegendTextPaint { get; set; } = // mark
        new SolidColorPaint // mark
        { // mark
            Color = new SKColor(50, 50, 50), // mark
            SKTypeface = SKTypeface.FromFamilyName("Courier New") // mark
        }; // mark

    public SolidColorPaint LedgendBackgroundPaint { get; set; } = // mark
        new SolidColorPaint(new SKColor(240, 240, 240)); // mark
}

custom

Tooltip control from scratch

You can also create your own legend, the recommended way is to use the LiveCharts API (example bellow) but you can use anything as tooltip as soon as it implements the IChartLegend<T> interface. At the following example we build a custom control to render legends in our charts using the LiveCharts API.

CustomLegend.cs

using System.Linq;
using LiveChartsCore;
using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.Measure;
using LiveChartsCore.Painting;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView.Drawing.Layouts;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;

namespace ViewModelsSamples.General.TemplatedLegends;

public class CustomLegend : IChartLegend
{
    private readonly Container _container;
    private readonly StackLayout _stackLayout;
    private bool _isInCanvas = false;
    private DrawablesTask? _drawableTask;

    public CustomLegend()
    {
        _container = new Container
        {
            Content = _stackLayout = new()
            {
                Padding = new Padding(15, 4),
                HorizontalAlignment = Align.Start,
                VerticalAlignment = Align.Middle
            }
        };
    }

    public void Draw(Chart chart)
    {
        var legendPosition = chart.GetLegendPosition();

        _container.X = legendPosition.X;
        _container.Y = legendPosition.Y;

        if (!_isInCanvas)
        {
            _drawableTask = chart.Canvas.AddGeometry(_container);
            _drawableTask.ZIndex = 10099;
            _isInCanvas = true;
        }

        if (chart.LegendPosition == LegendPosition.Hidden && _drawableTask is not null)
        {
            chart.Canvas.RemovePaintTask(_drawableTask);
            _isInCanvas = false;
            _drawableTask = null;
        }
    }

    public LvcSize Measure(Chart chart)
    {
        BuildLayout(chart);

        return _container.Measure();
    }

    public void Hide(Chart chart)
    {
        if (_drawableTask is not null)
        {
            chart.Canvas.RemovePaintTask(_drawableTask);
            _isInCanvas = false;
            _drawableTask = null;
        }
    }

    private void BuildLayout(Chart chart)
    {
        _stackLayout.Orientation = chart.LegendPosition is LegendPosition.Left or LegendPosition.Right
            ? ContainerOrientation.Vertical
            : ContainerOrientation.Horizontal;

        if (_stackLayout.Orientation == ContainerOrientation.Horizontal)
        {
            _stackLayout.MaxWidth = chart.ControlSize.Width;
            _stackLayout.MaxHeight = double.MaxValue;
        }
        else
        {
            _stackLayout.MaxWidth = double.MaxValue;
            _stackLayout.MaxHeight = chart.ControlSize.Height;
        }

        foreach (var visual in _stackLayout.Children.ToArray())
            _ = _stackLayout.Children.Remove(visual);

        foreach (var series in chart.Series.Where(x => x.IsVisibleAtLegend))
        {
            var sl = new StackLayout
            {
                Padding = new Padding(12, 6),
                VerticalAlignment = Align.Middle,
                HorizontalAlignment = Align.Middle,
                Children =
                {
                    new RectangleGeometry
                    {
                        Fill = (series as IStrokedAndFilled)?.Fill?.CloneTask(),
                        Stroke = new SolidColorPaint(new SKColor(30, 30, 30), 3),
                        Width = 20,
                        Height = 50
                    },
                    new LabelGeometry
                    {
                        Text = series.Name ?? string.Empty,
                        Paint = new SolidColorPaint(new SKColor(30, 30, 30)),
                        TextSize = 20,
                        Padding = new Padding(8, 2, 0, 2),
                        MaxWidth = (float)LiveCharts.DefaultSettings.MaxTooltipsAndLegendsLabelsWidth,
                        VerticalAlign = Align.Start,
                        HorizontalAlign = Align.Start
                    }
                }
            };

            _stackLayout.Children.Add(sl);
        }
    }
}

View

using Eto.Forms;
using LiveChartsCore.SkiaSharpView.Eto;
using ViewModelsSamples.General.TemplatedLegends;

namespace EtoFormsSample.General.TemplatedLegends;

public class View : Panel
{
    private readonly CartesianChart cartesianChart;

    public View()
    {
        var viewModel = new ViewModel();

        cartesianChart = new CartesianChart
        {
            Series = viewModel.Series,
            LegendPosition = LiveChartsCore.Measure.LegendPosition.Right,
            Legend = new CustomLegend()
        };

        Content = cartesianChart;
    }
}

custom tooltip