From 07e8ac24e93de4f1f87a336d4f880f0d908b328e Mon Sep 17 00:00:00 2001 From: t-bltg Date: Sun, 7 Jan 2024 12:12:52 +0100 Subject: [PATCH 1/3] fix legend colorrange --- .../src/tests/figures_and_makielayout.jl | 16 +++++++++++++ src/makielayout/blocks/legend.jl | 23 +++++++++++++++---- src/makielayout/defaultattributes.jl | 2 +- src/makielayout/types.jl | 2 ++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/ReferenceTests/src/tests/figures_and_makielayout.jl b/ReferenceTests/src/tests/figures_and_makielayout.jl index 6c6f46a2013..ed53d5e9cfb 100644 --- a/ReferenceTests/src/tests/figures_and_makielayout.jl +++ b/ReferenceTests/src/tests/figures_and_makielayout.jl @@ -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) diff --git a/src/makielayout/blocks/legend.jl b/src/makielayout/blocks/legend.jl index 8d9310f8ba4..1e01e7f2b7a 100644 --- a/src/makielayout/blocks/legend.jl +++ b/src/makielayout/blocks/legend.jl @@ -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) @@ -404,9 +408,13 @@ 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 @@ -414,6 +422,7 @@ 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), @@ -425,6 +434,7 @@ 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), )] @@ -432,7 +442,10 @@ 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 diff --git a/src/makielayout/defaultattributes.jl b/src/makielayout/defaultattributes.jl index 12603db1821..6dc7f200a88 100644 --- a/src/makielayout/defaultattributes.jl +++ b/src/makielayout/defaultattributes.jl @@ -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 diff --git a/src/makielayout/types.jl b/src/makielayout/types.jl index aa4c4d458ab..e7e26a287da 100644 --- a/src/makielayout/types.jl +++ b/src/makielayout/types.jl @@ -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" From fd7848f66d03b7476da523a944eb7149d587c821 Mon Sep 17 00:00:00 2001 From: t-bltg Date: Sun, 7 Jan 2024 12:28:30 +0100 Subject: [PATCH 2/3] fix conflict --- src/makielayout/blocks/legend.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/makielayout/blocks/legend.jl b/src/makielayout/blocks/legend.jl index 1e01e7f2b7a..79ac5f62649 100644 --- a/src/makielayout/blocks/legend.jl +++ b/src/makielayout/blocks/legend.jl @@ -402,27 +402,27 @@ 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 -extract_colorrange(@nospecialize(plot)) = +extract_legend_colorrange(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), + color = extract_legend_color(plot, legend.linecolor), + colorrange = extract_legend_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), + color = extract_legend_color(plot, legend.markercolor), + colorrange = extract_legend_colorrange(plot), marker = choose_scalar(plot.marker, legend.marker), markersize = choose_scalar(plot.markersize, legend.markersize), strokewidth = choose_scalar(plot.strokewidth, legend.markerstrokewidth), @@ -431,10 +431,10 @@ 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 = extract_colorrange(plot), + colorrange = extract_legend_colorrange(plot), strokecolor = choose_scalar(plot.strokecolor, legend.polystrokecolor), strokewidth = choose_scalar(plot.strokewidth, legend.polystrokewidth), )] @@ -444,7 +444,7 @@ 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), - colorrange = extract_colorrange(plot), + colorrange = extract_legend_colorrange(plot), polystrokecolor = :transparent, polystrokewidth = 0)] end From 54b1eb0160e4a31d3a598ba2194dfe75bae71590 Mon Sep 17 00:00:00 2001 From: t-bltg Date: Sun, 7 Jan 2024 17:00:26 +0100 Subject: [PATCH 3/3] fix colormap - add more ref imgs --- .../src/tests/figures_and_makielayout.jl | 24 ++++++++-- src/makielayout/blocks/legend.jl | 44 ++++++++++++------- src/makielayout/defaultattributes.jl | 2 +- src/makielayout/types.jl | 2 + 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/ReferenceTests/src/tests/figures_and_makielayout.jl b/ReferenceTests/src/tests/figures_and_makielayout.jl index ed53d5e9cfb..7b51e95e928 100644 --- a/ReferenceTests/src/tests/figures_and_makielayout.jl +++ b/ReferenceTests/src/tests/figures_and_makielayout.jl @@ -133,18 +133,34 @@ end end end -@reference_test "Legend colorrange" begin +@reference_test "Legend lines 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) + 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 colorrange" begin +@reference_test "Axislegend lines 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) + 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 diff --git a/src/makielayout/blocks/legend.jl b/src/makielayout/blocks/legend.jl index 79ac5f62649..b92afeb124c 100644 --- a/src/makielayout/blocks/legend.jl +++ b/src/makielayout/blocks/legend.jl @@ -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) @@ -287,8 +292,9 @@ function legendelement_plots!(scene, element::LineElement, bbox::Observable{Rect scene, points; linewidth = attrs.linewidth, linestyle = attrs.linestyle, - color = attrs.linecolor, colorrange = attrs.colorrange, + colormap = attrs.colormap, + color = attrs.linecolor, inspectable = false )] end @@ -298,10 +304,15 @@ function legendelement_plots!(scene, element::PolyElement, bbox::Observable{Rect 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) @@ -408,13 +419,11 @@ function extract_legend_color(@nospecialize(plot), color_default) return choose_scalar(color, color_default) end -extract_legend_colorrange(plot) = - haskey(plot, :colorrange) ? plot.colorrange : nothing - function legendelements(plot::Union{Lines, LineSegments}, legend) LegendElement[LineElement( color = extract_legend_color(plot, legend.linecolor), - colorrange = extract_legend_colorrange(plot), + colorrange = plot.colorrange, + colormap = plot.colormap, linestyle = choose_scalar(plot.linestyle, legend.linestyle), linewidth = choose_scalar(plot.linewidth, legend.linewidth))] end @@ -422,7 +431,8 @@ end function legendelements(plot::Scatter, legend) LegendElement[MarkerElement( color = extract_legend_color(plot, legend.markercolor), - colorrange = extract_legend_colorrange(plot), + 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), @@ -434,7 +444,8 @@ function legendelements(plot::Union{Poly, Violin, BoxPlot, CrossBar, Density}, l color = extract_legend_color(plot, legend.polycolor) LegendElement[PolyElement( color = color, - colorrange = extract_legend_colorrange(plot), + colorrange = plot.colorrange, + colormap = plot.colormap, strokecolor = choose_scalar(plot.strokecolor, legend.polystrokecolor), strokewidth = choose_scalar(plot.strokewidth, legend.polystrokewidth), )] @@ -444,7 +455,8 @@ 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), - colorrange = extract_legend_colorrange(plot), + colorrange = plot.colorrange, + colormap = plot.colormap, polystrokecolor = :transparent, polystrokewidth = 0)] end diff --git a/src/makielayout/defaultattributes.jl b/src/makielayout/defaultattributes.jl index 6dc7f200a88..0eabe167dae 100644 --- a/src/makielayout/defaultattributes.jl +++ b/src/makielayout/defaultattributes.jl @@ -76,7 +76,7 @@ end function attributenames(::Type{LegendEntry}) (:label, :labelsize, :labelfont, :labelcolor, :labelhalign, :labelvalign, :patchsize, :patchstrokecolor, :patchstrokewidth, :patchcolor, - :linepoints, :linewidth, :linecolor, :linestyle, :colorrange, + :linepoints, :linewidth, :linecolor, :linestyle, :colorrange, :colormap, :markerpoints, :markersize, :markerstrokewidth, :markercolor, :markerstrokecolor, :polypoints, :polystrokewidth, :polycolor, :polystrokecolor) end diff --git a/src/makielayout/types.jl b/src/makielayout/types.jl index e7e26a287da..91b9a746ed0 100644 --- a/src/makielayout/types.jl +++ b/src/makielayout/types.jl @@ -1215,6 +1215,8 @@ const EntryGroup = Tuple{Any, Vector{LegendEntry}} 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"