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

Allow hiding legend or colorbar via show = false #547

Merged
merged 2 commits into from
Sep 2, 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 `bar_labels` to `BarPlot`'s aesthetic mapping [#544](https://github.com/MakieOrg/AlgebraOfGraphics.jl/pull/544).
- Added ability to hide legend or colorbar by passing, e.g., `legend = (; show = false)` to `draw` [#547](https://github.com/MakieOrg/AlgebraOfGraphics.jl/pull/547).

## v0.8.5 - 2024-08-27

Expand Down
19 changes: 14 additions & 5 deletions src/draw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ In practice, `d` will often be a [`AlgebraOfGraphics.Layer`](@ref) or
[`AlgebraOfGraphics.Layers`](@ref).
Scale options can be passed as an optional second argument.
The output can be customized by passing named tuples or dictionaries with settings via the `axis`, `figure`, `facet`, `legend` or `colorbar` keywords.
Legend and colorbar are drawn automatically. For finer control, use [`draw!`](@ref),
Legend and colorbar are drawn automatically unless `show = false` is passed to the keyword
arguments of either `legend` or `colorbar`.
For finer control, use [`draw!`](@ref),
[`legend!`](@ref), and [`colorbar!`](@ref) independently.
"""
function draw(d::AbstractDrawable, scales::Scales = scales();
Expand All @@ -57,15 +60,21 @@ function draw(d::AbstractDrawable, scales::Scales = scales();
return _draw(d, scales; axis, figure, facet, legend, colorbar)
end

_remove_show_kw(pairs) = filter(((key, value),) -> key !== :show, pairs)

function _draw(d::AbstractDrawable, scales::Scales;
axis, figure, facet, legend, colorbar)

return update(Figure(; pairs(figure)...)) do f
grid = plot!(f, d, scales; axis)
fg = FigureGrid(f, grid)
facet!(fg; facet)
colorbar!(fg; pairs(colorbar)...)
legend!(fg; pairs(legend)...)
if get(colorbar, :show, true)
colorbar!(fg; _remove_show_kw(pairs(colorbar))...)
end
if get(legend, :show, true)
legend!(fg; _remove_show_kw(pairs(legend))...)
end
resize_to_layout!(fg)
return fg
end
Expand All @@ -77,13 +86,13 @@ function draw(scales::Scales = scales(); kwargs...)
end

"""
draw!(fig, d::AbstractDrawable, scales::Scales = scales(); [axis, facet, legend, colorbar])
draw!(fig, d::AbstractDrawable, scales::Scales = scales(); [axis, facet])
Draw a [`AlgebraOfGraphics.AbstractDrawable`](@ref) object `d` on `fig`.
In practice, `d` will often be a [`AlgebraOfGraphics.Layer`](@ref) or
[`AlgebraOfGraphics.Layers`](@ref).
`fig` can be a figure, a position in a layout, or an axis if `d` has no facet specification.
The output can be customized by passing named tuples or dictionaries with settings via the `axis`, `facet`, `legend` or `colorbar` keywords.
The output can be customized by passing named tuples or dictionaries with settings via the `axis` or `facet` keywords.
"""
function draw!(fig, d::AbstractDrawable, scales::Scales = scales();
axis=NamedTuple(), facet=NamedTuple(), palette=nothing)
Expand Down
11 changes: 9 additions & 2 deletions src/guides/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ function legend!(fg::FigureGrid; position=:right,
end

"""
legend!(figpos, grid; kwargs...)
legend!(figpos, grid; order = nothing, kwargs...)
Compute legend for `grid` (which should be the output of [`draw!`](@ref)) and draw it in
position `figpos`. Attributes allowed in `kwargs` are the same as `MakieLayout.Legend`.
position `figpos`. All `kwargs` are forwarded to Makie's `Legend` constructor.
The order of scales represented in the legend can be changed with the `order` keyword.
By default, legend sections are ordered the same as they appear in the plot specification.
Assuming three scales `Color`, `MarkerSize` and `custom` exist in a spec, you can pass a vector
to reorder them like `[:MarkerSize, :custom, :Color]`, or merge multiple entries together with
a nested vector like `[[:MarkerSize, :custom], :Color]`, or give merged sections a title with the
pair syntax `[[:MarkerSize, :custom] => "Merged group", :Color]`.
"""
function legend!(figpos, grid; order = nothing, kwargs...)
legend = compute_legend(grid; order)
Expand Down
30 changes: 30 additions & 0 deletions test/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,33 @@ end
@test length(leg_els) == 1
@test el_labels[] == last.(categories)
end

@testset "hidden legend" begin
df = (;
x = repeat(1:3, 3),
y = abs.(sin.(1:9)),
z=["a", "a", "a", "b", "b", "b", "c", "c", "c"]
)
for show in [true, false]
fg = draw(
data(df) * mapping(:x, :y; stack=:z, color=:z) * visual(BarPlot),
legend = (; show)
)
@test any(x -> x isa Legend, fg.figure.content) == show
end
end

@testset "hidden colorbar" begin
df = (;
x = 1:3,
y = 4:6,
z = 7:9
)
for show in [true, false]
fg = draw(
data(df) * mapping(:x, :y; color=:z) * visual(BarPlot),
colorbar = (; show)
)
@test any(x -> x isa Colorbar, fg.figure.content) == show
end
end
Loading