Skip to content

Commit

Permalink
annotate!: relative position independant from axis scale (#4297)
Browse files Browse the repository at this point in the history
* relative position now independant from the axis scale

* Format and math enhancement

* Fix unecessary braces

Co-authored-by: Brian Sinquin <brian.sinquin@gmail.com>
  • Loading branch information
curio-sitas and brian-sinquin authored Jul 31, 2022
1 parent d987c72 commit 6d537c6
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions src/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,19 @@ function process_annotation(
anns
end

_relative_position(xmin, xmax, pos::Length{:pct}) = xmin + pos.value * (xmax - xmin)
function _relative_position(xmin, xmax, pos::Length{:pct}, scale::Symbol)

# !TODO Add more scales in the future (asinh, sqrt) ?
if scale === :log || scale === :ln
exp(log(xmin) + pos.value * log(xmax / xmin))
elseif scale === :log10
exp10(log10(xmin) + pos.value * log10(xmax / xmin))
elseif scale === :log2
exp2(log2(xmin) + pos.value * log2(xmax / xmin))
else # :identity (linear scale)
xmin + pos.value * (xmax - xmin)
end
end

# Give each annotation coordinates based on specified position
function locate_annotation(
Expand All @@ -662,23 +674,51 @@ function locate_annotation(
)
x, y = position_multiplier[pos]
(
_relative_position(axis_limits(sp, :x)..., x),
_relative_position(axis_limits(sp, :y)..., y),
_relative_position(
axis_limits(sp, :x)...,
x,
sp[get_attr_symbol(:x, :axis)][:scale],
),
_relative_position(
axis_limits(sp, :y)...,
y,
sp[get_attr_symbol(:y, :axis)][:scale],
),
label,
)
end
locate_annotation(sp::Subplot, x, y, label::PlotText) = (x, y, label)
locate_annotation(sp::Subplot, x, y, z, label::PlotText) = (x, y, z, label)

locate_annotation(sp::Subplot, rel::NTuple{2,<:Number}, label::PlotText) = (
_relative_position(axis_limits(sp, :x)..., rel[1] * Plots.pct),
_relative_position(axis_limits(sp, :y)..., rel[2] * Plots.pct),
_relative_position(
axis_limits(sp, :x)...,
rel[1] * Plots.pct,
sp[get_attr_symbol(:x, :axis)][:scale],
),
_relative_position(
axis_limits(sp, :y)...,
rel[2] * Plots.pct,
sp[get_attr_symbol(:y, :axis)][:scale],
),
label,
)
locate_annotation(sp::Subplot, rel::NTuple{3,<:Number}, label::PlotText) = (
_relative_position(axis_limits(sp, :x)..., rel[1] * Plots.pct),
_relative_position(axis_limits(sp, :y)..., rel[2] * Plots.pct),
_relative_position(axis_limits(sp, :z)..., rel[3] * Plots.pct),
_relative_position(
axis_limits(sp, :x)...,
rel[1] * Plots.pct,
sp[get_attr_symbol(:x, :axis)][:scale],
),
_relative_position(
axis_limits(sp, :y)...,
rel[2] * Plots.pct,
sp[get_attr_symbol(:y, :axis)][:scale],
),
_relative_position(
axis_limits(sp, :z)...,
rel[3] * Plots.pct,
sp[get_attr_symbol(:z, :axis)][:scale],
),
label,
)
# -----------------------------------------------------------------------
Expand Down

0 comments on commit 6d537c6

Please sign in to comment.