From d5cab6d4554196a7d60e4ef60cf1459f24a027d7 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 29 Oct 2024 16:33:55 -0700 Subject: [PATCH 1/3] Fix `legend` for plotlist with multiple plots --- src/makielayout/blocks/legend.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/makielayout/blocks/legend.jl b/src/makielayout/blocks/legend.jl index bf1a07226f3..8ce08ce9db9 100644 --- a/src/makielayout/blocks/legend.jl +++ b/src/makielayout/blocks/legend.jl @@ -664,7 +664,8 @@ end function get_labeled_plots(ax; merge::Bool, unique::Bool) lplots = filter(get_plots(ax)) do plot - haskey(plot.attributes, :label) + haskey(plot.attributes, :label) || + plot isa PlotList && any(x -> haskey(x.attributes, :label), plot.plots) end labels = map(lplots) do l l.label[] @@ -716,6 +717,11 @@ function get_labeled_plots(ax; merge::Bool, unique::Bool) end get_plots(p::AbstractPlot) = [p] +# NOTE: this is important, since we know that `get_plots` is only ever called on the toplevel, +# we can assume that any plotlist on the toplevel should be decomposed into individual plots. +# However, if the user passes a label argument with a legend override, what do we do? +get_plots(p::PlotList) = haskey(p.attributes, :label) && p.attributes[:label] isa Pair ? [p] : p.plots + get_plots(ax::Union{Axis, Axis3}) = get_plots(ax.scene) get_plots(lscene::LScene) = get_plots(lscene.scene) function get_plots(scene::Scene) From fa9a319f46faaec020f46c0735f86dc67b7d5c97 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 29 Oct 2024 16:39:54 -0700 Subject: [PATCH 2/3] Add a test --- test/specapi.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/specapi.jl b/test/specapi.jl index d948768d821..cea645640aa 100644 --- a/test/specapi.jl +++ b/test/specapi.jl @@ -170,3 +170,20 @@ end @test isempty(f.content) @test isempty(f.layout.content) end + +@testset "Legend construction" begin + f, ax, pl = plotlist([S.Scatter(1:4, 1:4; marker = :circle, label="A"), S.Scatter(1:6, 1:6; marker = :rect, label="B")]) + leg = axislegend(ax) + # Test that the legend has two scatter plots + @test count(x -> x isa Makie.Scatter, leg.scene.plots) == 2 + + # Test that the scatter plots have the correct markers + # This is too internal and fragile, so we won't actually test this + # @test leg.scene.plots[2].marker[] == :circle + # @test leg.scene.plots[3].marker[] == :rect + + # Test that the legend has the correct labels. + # Again, I consider this too fragile to work with! + # @test contents(contents(leg.grid)[1])[2].text[] == "A" + # @test contents(contents(leg.grid)[2])[4].text[] == "B" +end From d4f2cec30ab5ee1b53f78fc4229e81302d81d2c2 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 29 Oct 2024 17:22:28 -0700 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2b23539527..8af0e31edd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] - Added `subsup` and `left_subsup` functions that offer stacked sub- and superscripts for `rich` text which means this style can be used with arbitrary fonts and is not limited to fonts supported by MathTeXEngine.jl [#4489](https://github.com/MakieOrg/Makie.jl/pull/4489). +- Expand PlotList plots to expose their child plots to the legend interface, allowing `axislegend` to once again show plots within PlotSpecs as individual entries. [#4546](https://github.com/MakieOrg/Makie.jl/pull/4546) ## [0.21.15] - 2024-10-25