Skip to content

Make Plot.Legend settable and Legend class derivable #4571

@onur-akaydin

Description

@onur-akaydin

Suggestion: Would you consider making the Plot.Legend property settable and the Legend class inheritable, by making some methods virtual (e.g. GetItems method)? I am trying to create a custom "LegendWithOrderControl" class that can sort the legends with any logic I want, but Plot.Legend is not mutable.

public class LegendWithOrderControl : Legend
{
    private Func<LegendItem[], LegendItem[]>? _orderingFunction;

    public LegendWithOrderControl(Plot plot) : base(plot)
    {
    }

    /// <summary>
    /// Sets the function used to determine the order of legend items
    /// </summary>
    /// <param name="orderingFunction">A function that takes an array of LegendItems and returns them in the desired order</param>
    public void SetOrderingFunction(Func<LegendItem[], LegendItem[]> orderingFunction)
    {
        _orderingFunction = orderingFunction;
    }

    /// <summary>
    /// Clears the custom ordering function, reverting to default ordering
    /// </summary>
    public void ClearOrderingFunction()
    {
        _orderingFunction = null;
    }

    /// <summary>
    /// Override of the base GetItems method to apply custom ordering if specified
    /// </summary>
    public override LegendItem[] GetItems()
    {
        // Get items from base implementation
        LegendItem[] items = base.GetItems();

        // Apply custom ordering if a function is set
        if (_orderingFunction != null)
        {
            return _orderingFunction(items);
        }

        return items;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions