Displaying Multiple Legends for OxyPlot in WPF with Two Y Axes

380 views Asked by At

I'm using OxyPlot in my WPF project to create a plot with two Y axes. I have different series assigned to either the left or the right Y axis. I would like to display two separate legends: one on the left with elements corresponding to the left Y axis, and another legend for the right Y axis.

I have already implemented the two Y axes and assigned the series correctly to each axis using the YAxisKey property. However, I haven't found a built-in way to split the legends in OxyPlot.

Is there a way to achieve this visual separation of legends? If so, could you please provide guidance or an example of how to implement it?

My current code for creating the axes and series assignment looks similar to the following:

    var leftAxis = new LinearAxis
    {
        Position = AxisPosition.Left,
        Key = "LeftAxis",
        Title = "Left Axis"
    };

    var rightAxis = new LinearAxis
    {
        Position = AxisPosition.Right,
        Key = "RightAxis",
        Title = "Right Axis"
    };

    plotModel.Axes.Add(leftAxis);
    plotModel.Axes.Add(rightAxis);

    var series0 = new LineSeries
    {
        YAxisKey = rightAxis.Key,
        Title = "Series0",
        Color = OxyColors.Blue,
    };

    var series1 = new LineSeries
    {
        YAxisKey = leftAxis.Key,
        Title = "Series0",
        Color = OxyColors.Red,
    };

I'm open to any suggestions or alternative approaches to visually split the legends based on the Y axes. Thank you in advance for your help!

1

There are 1 answers

0
Döharrrck On

You can add an arbitrary number of legends to a PlotModel. Each instance of Legend can get it's own key assigned. You can then set the LegendKey property on each series to control in which legend they are displayed.

public static PlotModel TwoAxesToLegends()
    {
        var model = new PlotModel();

        var leftAxis = new LinearAxis
        {
            Position = AxisPosition.Left,
            Key = "LeftAxis",
            Title = "Left Axis"
        };

        var rightAxis = new LinearAxis
        {
            Position = AxisPosition.Right,
            Key = "RightAxis",
            Title = "Right Axis"
        };

        model.Axes.Add(leftAxis);
        model.Axes.Add(rightAxis);

        var leftLegend = new Legend
        {
            LegendTitle = "Left Axis",
            LegendPlacement = LegendPlacement.Inside,
            LegendPosition = LegendPosition.LeftTop,
            LegendBackground = OxyColor.FromAColor(200, OxyColors.White),
            LegendBorder = OxyColors.Black,
            Key = "Legend Left",
        };

        var rightLegend = new Legend
        {
            LegendTitle = "Right Axis",
            LegendPlacement = LegendPlacement.Inside,
            LegendPosition = LegendPosition.RightTop,
            LegendBackground = OxyColor.FromAColor(200, OxyColors.White),
            LegendBorder = OxyColors.Black,
            Key = "Legend Right",
        };

        model.Legends.Add(leftLegend);
        model.Legends.Add(rightLegend);

        var leftSeries = new LineSeries
        {
            YAxisKey = rightAxis.Key,
            LegendKey = leftLegend.Key,
            Title = "Left Series",
            Color = OxyColors.Blue,
        };

        var rightSeries = new LineSeries
        {
            YAxisKey = leftAxis.Key,
            LegendKey = rightLegend.Key,
            Title = "Right Series",
            Color = OxyColors.Red,
        };

        model.Series.Add(leftSeries);
        model.Series.Add(rightSeries);

        return model;
    }