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 colorrange #3536

Closed
wants to merge 3 commits into from
Closed
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
32 changes: 32 additions & 0 deletions ReferenceTests/src/tests/figures_and_makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ end
end
end

@reference_test "Legend lines colorrange" begin
f = Figure()
ax = Axis(f[1, 1])
foreach(i -> lines!(ax, i:i+1; colorrange = (1, 8), colormap = :cividis, color = i, label = string(i)), 1:8)
f[1, 2] = Legend(f, ax, "Legend")
f
end

@reference_test "Axislegend lines colorrange" begin
f = Figure()
ax = Axis(f[1, 1])
foreach(i -> lines!(ax, i:i+1; colorrange = (1, 8), colormap = :cividis, color = i, label = string(i)), 1:8)
axislegend(ax)
f
end

@reference_test "Axislegend scatter colorrange" begin
f = Figure()
ax = Axis(f[1, 1])
foreach(i -> scatter!(ax, i:i+1, i:i+1; colorrange = (1, 4), colormap = :cividis, color = i, label = string(i), marker = :hexagon), 1:4)
axislegend(ax)
f
end

@reference_test "Axislegend poly colorrange" begin
f = Figure()
ax = Axis(f[1, 1])
foreach(i -> poly!(ax, Rect(i, i, 0.5, 0.25); colorrange = (1, 4), colormap = :cividis, color = i, label = string(i)), 1:4)
axislegend(ax)
f
end

@reference_test "LaTeXStrings in Axis3 plots" begin
xs = LinRange(-10, 10, 100)
ys = LinRange(0, 15, 100)
Expand Down
59 changes: 42 additions & 17 deletions src/makielayout/blocks/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,17 @@ function legendelement_plots!(scene, element::MarkerElement, bbox::Observable{Re
attrs = element.attributes
fracpoints = attrs.markerpoints
points = lift((bb, fp) -> fractionpoint.(Ref(bb), fp), scene, bbox, fracpoints)
scat = scatter!(scene, points, color = attrs.markercolor, marker = attrs.marker,
return [scatter!(
scene, points;
colorrange = attrs.colorrange,
colormap = attrs.colormap,
color = attrs.markercolor,
marker = attrs.marker,
markersize = attrs.markersize,
strokewidth = attrs.markerstrokewidth,
strokecolor = attrs.markerstrokecolor, inspectable = false)

return [scat]
strokecolor = attrs.markerstrokecolor,
inspectable = false
)]
end

function legendelement_plots!(scene, element::LineElement, bbox::Observable{Rect2f}, defaultattrs::Attributes)
Expand All @@ -283,21 +288,31 @@ function legendelement_plots!(scene, element::LineElement, bbox::Observable{Rect

fracpoints = attrs.linepoints
points = lift((bb, fp) -> fractionpoint.(Ref(bb), fp), scene, bbox, fracpoints)
lin = lines!(scene, points, linewidth = attrs.linewidth, color = attrs.linecolor,
linestyle = attrs.linestyle, inspectable = false)

return [lin]
return [lines!(
scene, points;
linewidth = attrs.linewidth,
linestyle = attrs.linestyle,
colorrange = attrs.colorrange,
colormap = attrs.colormap,
color = attrs.linecolor,
inspectable = false
)]
end

function legendelement_plots!(scene, element::PolyElement, bbox::Observable{Rect2f}, defaultattrs::Attributes)
merge!(element.attributes, defaultattrs)
attrs = element.attributes
fracpoints = attrs.polypoints
points = lift((bb, fp) -> fractionpoint.(Ref(bb), fp), scene, bbox, fracpoints)
pol = poly!(scene, points, strokewidth = attrs.polystrokewidth, color = attrs.polycolor,
strokecolor = attrs.polystrokecolor, inspectable = false)

return [pol]
return [poly!(
scene, points;
strokewidth = attrs.polystrokewidth,
strokecolor = attrs.polystrokecolor,
colorrange = attrs.colorrange,
colormap = attrs.colormap,
color = attrs.polycolor,
inspectable = false
)]
end

function Base.getproperty(lentry::LegendEntry, s::Symbol)
Expand Down Expand Up @@ -398,22 +413,26 @@ end

choose_scalar(attr, default) = is_scalar_attribute(to_value(attr)) ? attr : default

function extract_color(@nospecialize(plot), color_default)
function extract_legend_color(@nospecialize(plot), color_default)
color = haskey(plot, :calculated_color) ? plot.calculated_color : plot.color
color[] isa ColorMapping && return color_default
return choose_scalar(color, color_default)
end

function legendelements(plot::Union{Lines, LineSegments}, legend)
LegendElement[LineElement(
color = extract_color(plot, legend.linecolor),
color = extract_legend_color(plot, legend.linecolor),
colorrange = plot.colorrange,
colormap = plot.colormap,
linestyle = choose_scalar(plot.linestyle, legend.linestyle),
linewidth = choose_scalar(plot.linewidth, legend.linewidth))]
end

function legendelements(plot::Scatter, legend)
LegendElement[MarkerElement(
color = extract_color(plot, legend.markercolor),
color = extract_legend_color(plot, legend.markercolor),
colorrange = plot.colorrange,
colormap = plot.colormap,
marker = choose_scalar(plot.marker, legend.marker),
markersize = choose_scalar(plot.markersize, legend.markersize),
strokewidth = choose_scalar(plot.strokewidth, legend.markerstrokewidth),
Expand All @@ -422,17 +441,23 @@ function legendelements(plot::Scatter, legend)
end

function legendelements(plot::Union{Poly, Violin, BoxPlot, CrossBar, Density}, legend)
color = extract_color(plot, legend.polycolor)
color = extract_legend_color(plot, legend.polycolor)
LegendElement[PolyElement(
color = color,
colorrange = plot.colorrange,
colormap = plot.colormap,
strokecolor = choose_scalar(plot.strokecolor, legend.polystrokecolor),
strokewidth = choose_scalar(plot.strokewidth, legend.polystrokewidth),
)]
end

function legendelements(plot::Band, legend)
# there seems to be no stroke for Band, so we set it invisible
LegendElement[PolyElement(polycolor = choose_scalar(plot.color, legend.polystrokecolor), polystrokecolor = :transparent, polystrokewidth = 0)]
LegendElement[PolyElement(
polycolor = choose_scalar(plot.color, legend.polystrokecolor),
colorrange = plot.colorrange,
colormap = plot.colormap,
polystrokecolor = :transparent, polystrokewidth = 0)]
end

# if there is no specific overload available, we go through the child plots and just stack
Expand Down
2 changes: 1 addition & 1 deletion src/makielayout/defaultattributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ end
function attributenames(::Type{LegendEntry})
(:label, :labelsize, :labelfont, :labelcolor, :labelhalign, :labelvalign,
:patchsize, :patchstrokecolor, :patchstrokewidth, :patchcolor,
:linepoints, :linewidth, :linecolor, :linestyle,
:linepoints, :linewidth, :linecolor, :linestyle, :colorrange, :colormap,
:markerpoints, :markersize, :markerstrokewidth, :markercolor, :markerstrokecolor,
:polypoints, :polystrokewidth, :polycolor, :polystrokecolor)
end
Expand Down
4 changes: 4 additions & 0 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,10 @@ const EntryGroup = Tuple{Any, Vector{LegendEntry}}
linewidth = theme(scene, :linewidth)
"The default line color used for LineElements"
linecolor = theme(scene, :linecolor)
"The range of values depicted in the LineElements."
colorrange = nothing
"The colormap that the LineElements uses."
colormap = @inherit(:colormap, :viridis)
"The default line style used for LineElements"
linestyle = :solid
"The default marker color for MarkerElements"
Expand Down