allow passing tuple to series_annotations

This commit is contained in:
t-bltg 2021-08-02 20:55:08 +02:00
parent 2df85eb0fd
commit f331bfa010
4 changed files with 26 additions and 29 deletions

View File

@ -996,9 +996,9 @@ function processFontArg!(plotattributes::AKW, fontname::Symbol, arg)
elseif arg == :center elseif arg == :center
plotattributes[Symbol(fontname, :halign)] = :hcenter plotattributes[Symbol(fontname, :halign)] = :hcenter
plotattributes[Symbol(fontname, :valign)] = :vcenter plotattributes[Symbol(fontname, :valign)] = :vcenter
elseif arg in (:hcenter, :left, :right) elseif arg _haligns
plotattributes[Symbol(fontname, :halign)] = arg plotattributes[Symbol(fontname, :halign)] = arg
elseif arg in (:vcenter, :top, :bottom) elseif arg _valigns
plotattributes[Symbol(fontname, :valign)] = arg plotattributes[Symbol(fontname, :valign)] = arg
elseif T <: Colorant elseif T <: Colorant
plotattributes[Symbol(fontname, :color)] = arg plotattributes[Symbol(fontname, :color)] = arg

View File

@ -70,6 +70,7 @@ function text_size(lablen::Int, sz::Number, rot::Number = 0)
width, height width, height
end end
text_size(lab::AbstractString, sz::Number, rot::Number = 0) = text_size(length(lab), sz, rot) text_size(lab::AbstractString, sz::Number, rot::Number = 0) = text_size(length(lab), sz, rot)
text_size(lab::PlotText, sz::Number, rot::Number = 0) = text_size(length(lab.str), sz, rot)
# account for the size/length/rotation of tick labels # account for the size/length/rotation of tick labels
function tick_padding(sp::Subplot, axis::Axis) function tick_padding(sp::Subplot, axis::Axis)

View File

@ -475,10 +475,15 @@ mutable struct SeriesAnnotations
scalefactor::Tuple scalefactor::Tuple
end end
_text_label(lab::Tuple, font) = text(lab[1], font, lab[2:end]...)
_text_label(lab::PlotText, font) = lab
_text_label(lab, font) = text(lab, font)
series_annotations(anns::AMat) = map(series_annotations, anns)
series_annotations(scalar) = series_annotations([scalar]) series_annotations(scalar) = series_annotations([scalar])
function series_annotations(anns::AMat) series_annotations(anns::SeriesAnnotations) = anns
map(series_annotations, anns) series_annotations(::Nothing) = nothing
end
function series_annotations(strs::AbstractVector, args...) function series_annotations(strs::AbstractVector, args...)
fnt = font() fnt = font()
shp = nothing shp = nothing
@ -503,10 +508,8 @@ function series_annotations(strs::AbstractVector, args...)
# scale!(s, scalefactor, scalefactor, (0,0)) # scale!(s, scalefactor, scalefactor, (0,0))
# end # end
# end # end
SeriesAnnotations(strs, fnt, shp, scalefactor) SeriesAnnotations([_text_label(s, fnt) for s strs], fnt, shp, scalefactor)
end end
series_annotations(anns::SeriesAnnotations) = anns
series_annotations(::Nothing) = nothing
function series_annotations_shapes!(series::Series, scaletype::Symbol = :pixels) function series_annotations_shapes!(series::Series, scaletype::Symbol = :pixels)
anns = series[:series_annotations] anns = series[:series_annotations]
@ -574,11 +577,11 @@ function Base.iterate(ea::EachAnn, i = 1)
end end
# ----------------------------------------------------------------------- # -----------------------------------------------------------------------
annotations(::Nothing) = []
annotations(anns::AVec) = anns
annotations(anns::AMat) = map(annotations, anns) annotations(anns::AMat) = map(annotations, anns)
annotations(anns) = Any[anns]
annotations(sa::SeriesAnnotations) = sa annotations(sa::SeriesAnnotations) = sa
annotations(anns::AVec) = anns
annotations(anns) = Any[anns]
annotations(::Nothing) = []
_annotationfont(sp::Subplot) = Plots.font(; _annotationfont(sp::Subplot) = Plots.font(;
family=sp[:annotationfontfamily], family=sp[:annotationfontfamily],
@ -589,12 +592,9 @@ _annotationfont(sp::Subplot) = Plots.font(;
color=sp[:annotationcolor], color=sp[:annotationcolor],
) )
_annotation(sp, font, lab, pos...; alphabet="abcdefghijklmnopqrstuvwxyz") = ( _annotation(sp::Subplot, font, lab, pos...; alphabet="abcdefghijklmnopqrstuvwxyz") = (
if lab == :auto pos...,
(pos..., text("($(alphabet[sp[:subplot_index]]))", font)) lab == :auto ? text("($(alphabet[sp[:subplot_index]]))", font) : _text_label(lab, font)
else
(pos..., isa(lab, PlotText) ? lab : isa(lab, Tuple) ? text(lab[1], font, lab[2:end]...) : text(lab, font))
end
) )
# Expand arrays of coordinates, positions and labels into induvidual annotations # Expand arrays of coordinates, positions and labels into induvidual annotations
@ -623,8 +623,6 @@ function process_annotation(sp::Subplot, positions::Union{AVec{Symbol},Symbol,Tu
anns anns
end end
process_any_label(lab, font=Font()) = lab isa Tuple ? text(lab...) : text(lab, font)
_relative_position(xmin, xmax, pos::Length{:pct}) = xmin + pos.value * (xmax - xmin) _relative_position(xmin, xmax, pos::Length{:pct}) = xmin + pos.value * (xmax - xmin)
# Give each annotation coordinates based on specified position # Give each annotation coordinates based on specified position

View File

@ -116,7 +116,7 @@ end
end end
@testset "Series Annotations" begin @testset "Series Annotations" begin
square = Shape([(0, 0), (1, 0), (1, 1), (0, 1)]) square = Shape([(0., 0.), (1., 0.), (1., 1.), (0., 1.)])
@test_logs (:warn, "Unused SeriesAnnotations arg: triangle (Symbol)") begin @test_logs (:warn, "Unused SeriesAnnotations arg: triangle (Symbol)") begin
p = plot( p = plot(
[1, 2, 3], [1, 2, 3],
@ -130,7 +130,7 @@ end
), ),
) )
sa = p.series_list[1].plotattributes[:series_annotations] sa = p.series_list[1].plotattributes[:series_annotations]
@test sa.strs == ["a"] @test only(sa.strs).str == "a"
@test sa.font.family == "courier" @test sa.font.family == "courier"
@test sa.baseshape == square @test sa.baseshape == square
@test sa.scalefactor == (1, 4) @test sa.scalefactor == (1, 4)
@ -141,21 +141,19 @@ end
layout = (5, 1), layout = (5, 1),
ylims = (-1.1, 1.1), ylims = (-1.1, 1.1),
xlims = (0, 5), xlims = (0, 5),
series_annotations = permutedims([["1/1"],["1/2"],["1/3"],["1/4"],["1/5"]]), series_annotations = permutedims([["1/1"], ["1/2"], ["1/3"], ["1/4"], ["1/5"]]),
) )
@test spl.series_list[1].plotattributes[:series_annotations].strs == ["1/1"] for i 1:5
@test spl.series_list[2].plotattributes[:series_annotations].strs == ["1/2"] @test only(spl.series_list[i].plotattributes[:series_annotations].strs).str == "1/$i"
@test spl.series_list[3].plotattributes[:series_annotations].strs == ["1/3"] end
@test spl.series_list[4].plotattributes[:series_annotations].strs == ["1/4"]
@test spl.series_list[5].plotattributes[:series_annotations].strs == ["1/5"]
p = plot([1, 2], annotations=(1.5, 2, text("foo", :left))) p = plot([1, 2], annotations=(1.5, 2, text("foo", :left)))
x, y, txt = p.subplots[end][:annotations][end] x, y, txt = only(p.subplots[end][:annotations])
@test (x, y) == (1.5, 2) @test (x, y) == (1.5, 2)
@test txt.str == "foo" @test txt.str == "foo"
p = plot([1, 2], annotations=((.1, .5), :auto)) p = plot([1, 2], annotations=((.1, .5), :auto))
pos, txt = p.subplots[end][:annotations][end] pos, txt = only(p.subplots[end][:annotations])
@test pos == (.1, .5) @test pos == (.1, .5)
@test txt.str == "(a)" @test txt.str == "(a)"
end end