UnicodePlots: support :annotations (#3804)

This commit is contained in:
t-bltg 2021-09-14 18:46:46 +02:00 committed by GitHub
parent 0a9d30f7ac
commit 6cf01229bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 10 deletions

View File

@ -910,6 +910,7 @@ const _gaston_scale = [:identity, :ln, :log2, :log10]
# unicodeplots
const _unicodeplots_attr = merge_with_base_supported([
:annotations,
:label,
:legend,
:seriescolor,

View File

@ -57,10 +57,30 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
o = addUnicodeSeries!(sp, o, series, sp[:legend] != :none, xlim, ylim)
end
for ann in sp[:annotations]
x, y, val = locate_annotation(sp, ann...)
o = UnicodePlots.annotate!(
o, x, y, val.str;
color = up_color(val.font.color), halign = val.font.halign, valign = val.font.valign
)
end
push!(plt.o, o) # save the object
end
end
function up_color(col)
if typeof(col) <: UnicodePlots.UserColorType
color = col
elseif typeof(col) <: RGBA
col = convert(ARGB32, col)
color = map(Int, (red(col).i, green(col).i, blue(col).i))
else
color = :auto
end
color
end
# add a single series
function addUnicodeSeries!(
sp::Subplot{UnicodePlotsBackend},
@ -111,17 +131,16 @@ function addUnicodeSeries!(
for (n, segment) in enumerate(series_segments(series, st; check = true))
i, rng = segment.attr_index, segment.range
lc = get_linecolor(series, i)
if typeof(lc) <: UnicodePlots.UserColorType
color = lc
elseif typeof(lc) <: RGBA
lc = convert(ARGB32, lc)
color = map(Int, (red(lc).i, green(lc).i, blue(lc).i))
else
color = :auto
up = func(up, x[rng], y[rng]; color = up_color(lc), name = n == 1 ? label : "")
end
up = func(up, x[rng], y[rng]; color = color, name = n == 1 ? label : "")
for (xi, yi, str, fnt) in EachAnn(series[:series_annotations], x, y)
up = UnicodePlots.annotate!(
up, xi, yi, str;
color = up_color(fnt.color), halign = fnt.halign, valign = fnt.valign
)
end
return up
end