Skip to content

Commit

Permalink
fix legend colorrange
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Jan 7, 2024
1 parent 182c3fa commit 07e8ac2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
16 changes: 16 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,22 @@ end
end
end

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

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

@reference_test "LaTeXStrings in Axis3 plots" begin
xs = LinRange(-10, 10, 100)
ys = LinRange(0, 15, 100)
Expand Down
23 changes: 18 additions & 5 deletions src/makielayout/blocks/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,14 @@ 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,
color = attrs.linecolor,
colorrange = attrs.colorrange,
inspectable = false
)]
end

function legendelement_plots!(scene, element::PolyElement, bbox::Observable{Rect2f}, defaultattrs::Attributes)
Expand Down Expand Up @@ -404,16 +408,21 @@ function extract_color(@nospecialize(plot), color_default)
return choose_scalar(color, color_default)
end

extract_colorrange(@nospecialize(plot)) =
haskey(plot, :colorrange) ? plot.colorrange : nothing

function legendelements(plot::Union{Lines, LineSegments}, legend)
LegendElement[LineElement(
color = extract_color(plot, legend.linecolor),
colorrange = extract_colorrange(plot),
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),
colorrange = extract_colorrange(plot),
marker = choose_scalar(plot.marker, legend.marker),
markersize = choose_scalar(plot.markersize, legend.markersize),
strokewidth = choose_scalar(plot.strokewidth, legend.markerstrokewidth),
Expand All @@ -425,14 +434,18 @@ function legendelements(plot::Union{Poly, Violin, BoxPlot, CrossBar, Density}, l
color = extract_color(plot, legend.polycolor)
LegendElement[PolyElement(
color = color,
colorrange = extract_colorrange(plot),
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 = extract_colorrange(plot),
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,
:markerpoints, :markersize, :markerstrokewidth, :markercolor, :markerstrokecolor,
:polypoints, :polystrokewidth, :polycolor, :polystrokecolor)
end
Expand Down
2 changes: 2 additions & 0 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,8 @@ 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 default line style used for LineElements"
linestyle = :solid
"The default marker color for MarkerElements"
Expand Down

0 comments on commit 07e8ac2

Please sign in to comment.