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

Cannot use rich text in legends anymore #2838

Closed
jonaswickman opened this issue Apr 3, 2023 · 5 comments · Fixed by #2902
Closed

Cannot use rich text in legends anymore #2838

jonaswickman opened this issue Apr 3, 2023 · 5 comments · Fixed by #2902

Comments

@jonaswickman
Copy link
Contributor

I used to be able to put rich text in legends, but this no longer seems to work.

fig = Figure()
ax = Axis(fig[1,1])
l1 = lines!( 0..2π , sin )
Legend( fig[1,2] , [l1] , [rich("sin")])
fig

produces the following error message:

ERROR: MethodError: no method matching initialize_block!(::Legend, ::Vector{Lines{Tuple{Vector{Point{2, Float32}}}}}, ::Vector{Makie.RichText})
Closest candidates are:
  initialize_block!(::Legend, ::Observable{Vector{Tuple{Union{Nothing, AbstractString}, Vector{LegendEntry}}}}) at ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks/legend.jl:1
Stacktrace:
 [1] _block(::Type{Legend}, ::Figure, ::Vector{Lines{Tuple{Vector{Point{2, Float32}}}}}, ::Vararg{Any}; bbox::Nothing, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:397
 [2] _block(::Type{Legend}, ::Figure, ::Vector{Lines{Tuple{Vector{Point{2, Float32}}}}}, ::Vararg{Any})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:295
 [3] _block(::Type{Legend}, ::GridPosition, ::Vector{Lines{Tuple{Vector{Point{2, Float32}}}}}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:291
 [4] _block(::Type{Legend}, ::GridPosition, ::Vector{Lines{Tuple{Vector{Point{2, Float32}}}}}, ::Vararg{Any})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:284
 [5] Legend(::GridPosition, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:276
 [6] Legend(::GridPosition, ::Vararg{Any})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:275
 [7] top-level scope
   @ ~/Files/Documents/Dropbox/Projects/Resource Producer Preadtor/Allometric Model/Julia/Data/DataComparison/make_figure_model_enrichment_scaling.jl:289

Here is my julia versioninfo and Makie versions:

Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 1 on 12 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 
[ee78f7c6] Makie v0.19.4
[e9467ef8] GLMakie v0.8.4
[13f3f980] CairoMakie v0.10.4
@jkrumbiegel
Copy link
Collaborator

Ah unintended consequence of removing AbstractString inheritance from RichText, Legend would have to have its input signature widened then.

@jonaswickman
Copy link
Contributor Author

I don't know if it's the same problem, but it's also not possible to pass rich text to tick labels in Axis3 (though Axis is fine):

fig = Figure()
ax = Axis3(
    fig[1,1] ,
    xticks = ([1,2,3],[rich("1"),rich("2"),rich("3")])
)
lines!( [1,2,3] , [1,2,3] , [1,2,3] )
fig 

produces the error:

ERROR: TypeError: in typeassert, expected Vector{Tuple{String, Point{2, Float32}}}, got a value of type Vector{Tuple{Makie.RichText, Point{2, Float32}}}
Stacktrace:
 [1] (::Makie.var"#1467#1481"{Scene})(pxa::GeometryBasics.HyperRectangle{2, Int64}, pv::StaticArraysCore.SMatrix{4, 4, Float32, 16}, ticksegs::Vector{Tuple{Point{3, Float32}, Point3{Float64}}}, ticklabs::Vector{Makie.RichText}, pad::Int64)
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks/axis3d.jl:534
 [2] map(::Makie.var"#1467#1481"{Scene}, ::Scene, ::Observable{GeometryBasics.HyperRectangle{2, Int64}}, ::Observable{StaticArraysCore.SMatrix{4, 4, Float32, 16}}, ::Vararg{Any}; ignore_equal_values::Bool)
   @ Makie ~/.julia/packages/Makie/Iqcri/src/scenes.jl:185
 [3] map
   @ ~/.julia/packages/Makie/Iqcri/src/scenes.jl:182 [inlined]
 [4] add_ticks_and_ticklabels!(topscene::Scene, scene::Scene, ax::Axis3, dim::Int64, limits::Observable{GeometryBasics.HyperRectangle{3, Float32}}, ticknode::Observable{Tuple{Vector{Int64}, Vector{Makie.RichText}}}, miv::Observable{Bool}, min1::Observable{Bool}, min2::Observable{Bool}, azimuth::Observable{Any})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks/axis3d.jl:518
 [5] initialize_block!(ax::Axis3)
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks/axis3d.jl:73
 [6] _block(::Type{Axis3}, ::Figure; bbox::Nothing, kwargs::Base.Pairs{Symbol, Tuple{Vector{Int64}, Vector{Makie.RichText}}, Tuple{Symbol}, NamedTuple{(:xticks,), Tuple{Tuple{Vector{Int64}, Vector{Makie.RichText}}}}})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:397
 [7] _block(::Type{Axis3}, ::GridPosition; kwargs::Base.Pairs{Symbol, Tuple{Vector{Int64}, Vector{Makie.RichText}}, Tuple{Symbol}, NamedTuple{(:xticks,), Tuple{Tuple{Vector{Int64}, Vector{Makie.RichText}}}}})
   @ Makie ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:291
 [8] #_#1098
   @ ~/.julia/packages/Makie/Iqcri/src/makielayout/blocks.jl:276 [inlined]
 [9] top-level scope
   @ ~/Files/Documents/Dropbox/Projects/Resource Producer Preadtor/Allometric Model/Julia/Data/DataComparison/scratches_makie_errors.jl:14

@asinghvi17
Copy link
Member

I believe that the Axis3 issue is either solved in a PR, or on master.

@SimonDanisch
Copy link
Member

It's not fixed on master, what PR is supposed to fix this?

@jonaswickman
Copy link
Contributor Author

My understanding of Julia package development is way to limited to try to make a PR, but as a temporary fix, anyone who is having trouble with this can define and run (after loading Makie):

function Makie.Legend(fig_or_scene,
    contents::AbstractArray,
    labels::AbstractArray{<:Makie.Optional{Makie.RichText}},
    title::Makie.Optional{<:Makie.RichText} = nothing;
    kwargs...)

    if length(contents) != length(labels)
        error("Number of elements not equal: $(length(contents)) content elements and $(length(labels)) labels.")
    end

    entrygroups = Observable{Vector{Makie.EntryGroup}}([])
    legend = Legend(fig_or_scene, entrygroups; kwargs...)
    entries = [LegendEntry(label, content, legend) for (content, label) in zip(contents, labels)]
    entrygroups[] = [(title, entries)]
    legend
end

function Makie.LegendEntry(label::Makie.Optional{Makie.RichText}, contentelements::AbstractArray, legend; kwargs...)
    attrs = Attributes(label = label)

    kwargattrs = Attributes(kwargs)
    merge!(attrs, kwargattrs)

    elems = vcat(Makie.legendelements.(contentelements, Ref(legend))...)
    Makie.LegendEntry(elems, attrs)
end

function Makie.LegendEntry(label::Makie.Optional{Makie.RichText}, contentelement, legend; kwargs...)
    attrs = Attributes(label = label)

    kwargattrs = Attributes(kwargs)
    merge!(attrs, kwargattrs)

    elems = Makie.legendelements(contentelement, legend)
    Makie.LegendEntry(elems, attrs)
end

To make it work, you have to make all text rich text though, so it's not ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants