Skip to content

Commit

Permalink
more bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Nov 22, 2022
1 parent 91ed03d commit 23f64aa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
26 changes: 16 additions & 10 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ function optimal_ticks_and_labels(ticks, alims, scale, formatter)
# or DateTime) is chosen based on the time span between amin and amax
# rather than on the input format
# TODO: maybe: non-trivial scale (:ln, :log2, :log10) for date/datetime
if ticks === nothing && scale === :identity
noop = scale === :identity
if ticks === nothing && noop
if formatter == RecipesPipeline.dateformatter
# optimize_datetime_ticks returns ticks and labels(!) based on
# integers/floats corresponding to the DateTime type. Thus, the axes
Expand All @@ -159,7 +160,7 @@ function optimal_ticks_and_labels(ticks, alims, scale, formatter)
sf(amax);
k_min = scale _logScales ? 2 : 4, # minimum number of ticks
k_max = 8, # maximum number of ticks
scale = scale,
scale,
) |> first
elseif typeof(ticks) <: Int
optimize_ticks(
Expand All @@ -171,12 +172,16 @@ function optimal_ticks_and_labels(ticks, alims, scale, formatter)
# `strict_span = false` rewards cases where the span of the
# chosen ticks is not too much bigger than amin - amax:
strict_span = false,
scale = scale,
scale,
) |> first
else
map(sf, filter(t -> amin t amax, ticks))
end
unscaled_ticks = map(RecipesPipeline.inverse_scale_func(scale), scaled_ticks)
unscaled_ticks = if noop
scaled_ticks
else
map(RecipesPipeline.inverse_scale_func(scale), scaled_ticks)
end

labels::Vector{String} = if any(isfinite, unscaled_ticks)
if formatter in (:auto, :plain, :scientific, :engineering)
Expand Down Expand Up @@ -520,9 +525,12 @@ function scale_lims(from, to, factor)
mid .+ (-span, span) .* factor
end

_scale_lims(::Val{true}, ::Function, ::Function, from, to, factor) = scale_lims(from, to, factor)
_scale_lims(::Val{false}, f::Function, invf::Function, from, to, factor) = invf.(scale_lims(f(from), f(to), factor))

function scale_lims(from, to, factor, scale)
f, invf = RecipesPipeline.scale_func(scale), RecipesPipeline.inverse_scale_func(scale)
invf.(scale_lims(f(from), f(to), factor))
f, invf, noop = scale_inverse_scale_func(scale)
_scale_lims(Val(noop), f, invf, from, to, factor)
end

"""
Expand Down Expand Up @@ -770,8 +778,7 @@ function add_major_or_minor_segments_2d(
)
ticks === nothing && return
if cond
f = RecipesPipeline.scale_func(oax[:scale])
invf = RecipesPipeline.inverse_scale_func(oax[:scale])
f, invf = scale_inverse_scale_func(oax[:scale])
tick_start, tick_stop = if sp[:framestyle] === :origin
oamin, oamax = oamM
t = invf(f(0) + factor * (f(oamax) - f(oamin)))
Expand Down Expand Up @@ -904,8 +911,7 @@ function add_major_or_minor_segments_3d(
)
ticks === nothing && return
if cond
f = RecipesPipeline.scale_func(nax[:scale])
invf = RecipesPipeline.inverse_scale_func(nax[:scale])
f, invf = scale_inverse_scale_func(nax[:scale])
tick_start, tick_stop = if sp[:framestyle] === :origin
namin, namax = namM
t = invf(f(0) + factor * (f(namax) - f(namin)))
Expand Down
82 changes: 40 additions & 42 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ replaceAlias!(plotattributes::AKW, k::Symbol, aliases::Dict{Symbol,Symbol}) =
replaceAliases!(plotattributes::AKW, aliases::Dict{Symbol,Symbol}) =
foreach(k -> replaceAlias!(plotattributes, k, aliases), collect(keys(plotattributes)))

scale_inverse_scale_func(scale::Symbol) =
RecipesPipeline.scale_func(scale), RecipesPipeline.inverse_scale_func(scale), scale === :identity

function _heatmap_edges(v::AVec, isedges::Bool, ispolar::Bool)
length(v) == 1 && return v[1] .+ [ispolar ? max(-v[1], -0.5) : -0.5, 0.5]
isedges && return v
Expand All @@ -234,7 +237,7 @@ _heatmap_edges(::Val{true}, v::AVec, ::Symbol, isedges::Bool, ispolar::Bool) =
_heatmap_edges(v, isedges, ispolar)

function _heatmap_edges(::Val{false}, v::AVec, scale::Symbol, isedges::Bool, ispolar::Bool)
f, invf = RecipesPipeline.scale_func(scale), RecipesPipeline.inverse_scale_func(scale)
f, invf = scale_inverse_scale_func(scale)
map(invf, _heatmap_edges(map(f, v), isedges, ispolar))
end

Expand Down Expand Up @@ -942,45 +945,42 @@ function convert_sci_unicode(label::AbstractString)
label
end

_straightline_data(xl, yl, x, y, expansion_factor) =
if (n = length(x)) == 2
__straightline_data(xl, yl, x, y, expansion_factor)
else
k, r = divrem(n, 3)
@assert r == 0 "Misformed data. `straightline_data` either accepts vectors of length 2 or 3k. The provided series has length $n"
xdata, ydata = fill(NaN, n), fill(NaN, n)
for i in 1:k
inds = (3i - 2):(3i - 1)
xdata[inds], ydata[inds] =
__straightline_data(xl, yl, x[inds], y[inds], expansion_factor)
end
xdata, ydata
end

_straightline_data(::Val{true}, ::Function, ::Function, ::Function, ::Function, args...) = _straightline_data(args...)

function _straightline_data(::Val{false}, xf::Function, xinvf::Function, yf::Function, yinvf::Function, xl, yl, x, y, expansion_factor)
xdata, ydata = _straightline_data(xf.(xl), yf.(yl), xf.(x), yf.(y), expansion_factor)
xinvf.(xdata), yinvf.(ydata)
end

function straightline_data(series, expansion_factor = 1)
sp = series[:subplot]
xl, yl = isvertical(series) ? (xlims(sp), ylims(sp)) : (ylims(sp), xlims(sp))

# handle axes scales
xscale = sp[:xaxis][:scale]
xf = RecipesPipeline.scale_func(xscale)
xinvf = RecipesPipeline.inverse_scale_func(xscale)
xf, xinvf, xnoop = scale_inverse_scale_func(xscale)
yscale = sp[:yaxis][:scale]
yf = RecipesPipeline.scale_func(yscale)
yinvf = RecipesPipeline.inverse_scale_func(yscale)

xl, yl = xf.(xl), yf.(yl)
x, y = xf.(series[:x]), yf.(series[:y])
n = length(x)
yf, yinvf, ynoop = scale_inverse_scale_func(yscale)

xdata, ydata = if n == 2
straightline_data(xl, yl, x, y, expansion_factor)
else
k, r = divrem(n, 3)
if r == 0
xdata, ydata = fill(NaN, n), fill(NaN, n)
for i in 1:k
inds = (3 * i - 2):(3 * i - 1)
xdata[inds], ydata[inds] =
straightline_data(xl, yl, x[inds], y[inds], expansion_factor)
end
xdata, ydata
else
error(
"Misformed data. `straightline_data` either accepts vectors of length 2 or 3k. The provided series has length $n",
)
end
end

xinvf.(xdata), yinvf.(ydata)
_straightline_data(Val(xnoop && ynoop), xf, xinvf, yf, yinvf, xl, yl, series[:x], series[:y], expansion_factor)
end

function straightline_data(xl, yl, x, y, expansion_factor = 1)
function __straightline_data(xl, yl, x, y, expansion_factor)
x_vals, y_vals = if y[1] == y[2]
if x[1] == x[2]
error("Two identical points cannot be used to describe a straight line.")
Expand Down Expand Up @@ -1009,7 +1009,7 @@ function straightline_data(xl, yl, x, y, expansion_factor = 1)
x_vals, y_vals
end

function _shape_data!(::Val{false}, x, xl, xf::Function, xinvf::Function, expansion_factor)
function _shape_data!(::Val{false}, xf::Function, xinvf::Function, x, xl, expansion_factor)
@inbounds for i in eachindex(x)
if x[i] == -Inf
x[i] = xinvf(xf(xl[1]) - expansion_factor * (xf(xl[2]) - xf(xl[1])))
Expand All @@ -1020,7 +1020,7 @@ function _shape_data!(::Val{false}, x, xl, xf::Function, xinvf::Function, expans
x
end

function _shape_data!(::Val{true}, x, xl, ::Function, ::Function, expansion_factor)
function _shape_data!(::Val{true}, ::Function, ::Function, x, xl, expansion_factor)
@inbounds for i in eachindex(x)
if x[i] == -Inf
x[i] = xl[1] - expansion_factor * (xl[2] - xl[1])
Expand All @@ -1037,27 +1037,25 @@ function shape_data(series, expansion_factor = 1)

# handle axes scales
xscale = sp[:xaxis][:scale]
xf, xinvf =
RecipesPipeline.scale_func(xscale), RecipesPipeline.inverse_scale_func(xscale)
xf, xinvf, xnoop = scale_inverse_scale_func(xscale)
yscale = sp[:yaxis][:scale]
yf, yinvf =
RecipesPipeline.scale_func(yscale), RecipesPipeline.inverse_scale_func(yscale)
yf, yinvf, ynoop = scale_inverse_scale_func(yscale)

(
_shape_data!(
Val(xscale === :identity),
copy(series[:x]),
xl,
Val(xnoop),
xf,
xinvf,
copy(series[:x]),
xl,
expansion_factor,
),
_shape_data!(
Val(yscale === :identity),
copy(series[:y]),
yl,
Val(ynoop),
yf,
yinvf,
copy(series[:y]),
yl,
expansion_factor,
),
)
Expand Down

0 comments on commit 23f64aa

Please sign in to comment.