Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix legend for plotlist with multiple plots #4546

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`show plots within PlotSpecs as individual entries. [#4546](https://github.com/MakieOrg/Makie.jl/pull/4546)
- Implement S.Colorbar(plotspec) [#4520](https://github.com/MakieOrg/Makie.jl/pull/4520).

## [0.21.15] - 2024-10-25
Expand Down
8 changes: 7 additions & 1 deletion src/makielayout/blocks/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,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[]
Expand Down Expand Up @@ -713,6 +714,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)
Expand Down
17 changes: 17 additions & 0 deletions test/specapi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading