Skip to content

Commit

Permalink
dispatch processing of axis args on the plot object (#63)
Browse files Browse the repository at this point in the history
* dispatch processing of axis args on the plot object

* Update type_recipe.jl

* Update type_recipe.jl

* Update type_recipe.jl

* add to API

* Update api.jl
  • Loading branch information
BeastyBlacksmith authored Sep 8, 2020
1 parent 336263e commit 385d1d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
48 changes: 48 additions & 0 deletions RecipesPipeline/src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,54 @@ Returns `true` if `attr` is an axis attribute, i.e. it applies to `xattr`, `yatt
"""
is_axis_attribute(plt, attr) = false

# ### processing of axis args
# axis args before type recipes should still be mapped to all axes
"""
preprocess_axis_args!(plt, plotattributes)
Preprocessing of axis attributes.
Prepends the axis letter to axis attributes by default.
"""
function preprocess_axis_args!(plt, plotattributes)
for (k, v) in plotattributes
if is_axis_attribute(plt, k)
pop!(plotattributes, k)
for l in (:x, :y, :z)
lk = Symbol(l, k)
haskey(plotattributes, lk) || (plotattributes[lk] = v)
end
end
end
end

"""
preprocess_axis_args!(plt, plotattributes, letter)
This version additionally stores the letter name in `plotattributes[:letter]`.
"""
function preprocess_axis_args!(plt, plotattributes, letter)
plotattributes[:letter] = letter
preprocess_axis_args!(plt, plotattributes)
end

# axis args in type recipes should only be applied to the current axis
"""
postprocess_axis_args!(plt, plotattributes, letter)
Removes the `:letter` key from `plotattributes` and does the same prepending of the letters as `preprocess_axis_args!`.
"""
function postprocess_axis_args!(plt, plotattributes, letter)
pop!(plotattributes, :letter)
if letter in (:x, :y, :z)
for (k, v) in plotattributes
if is_axis_attribute(plt, k)
pop!(plotattributes, k)
lk = Symbol(letter, k)
haskey(plotattributes, lk) || (plotattributes[lk] = v)
end
end
end
end

# ## User recipes

Expand Down
44 changes: 6 additions & 38 deletions RecipesPipeline/src/type_recipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
Apply the type recipe with signature `(::Type{T}, ::T)`.
"""
function _apply_type_recipe(plotattributes, v, letter)
_preprocess_axis_args!(plotattributes, letter)
plt = plotattributes[:plot_object]
preprocess_axis_args!(plt, plotattributes, letter)
rdvec = RecipesBase.apply_recipe(plotattributes, typeof(v), v)
warn_on_recipe_aliases!(plotattributes[:plot_object], plotattributes, :type, typeof(v))
_postprocess_axis_args!(plotattributes, letter)
postprocess_axis_args!(plt, plotattributes, letter)
return rdvec[1].args[1]
end

Expand All @@ -25,7 +26,7 @@ end
# and one to format tick values.
function _apply_type_recipe(plotattributes, v::AbstractArray, letter)
plt = plotattributes[:plot_object]
_preprocess_axis_args!(plotattributes, letter)
preprocess_axis_args!(plt, plotattributes, letter)
# First we try to apply an array type recipe.
w = RecipesBase.apply_recipe(plotattributes, typeof(v), v)[1].args[1]
warn_on_recipe_aliases!(plt, plotattributes, :type, typeof(v))
Expand All @@ -35,15 +36,15 @@ function _apply_type_recipe(plotattributes, v::AbstractArray, letter)
x = first(skipmissing(v))
args = RecipesBase.apply_recipe(plotattributes, typeof(x), x)[1].args
warn_on_recipe_aliases!(plt, plotattributes, :type, typeof(x))
_postprocess_axis_args!(plotattributes, letter)
postprocess_axis_args!(plt, plotattributes, letter)
if length(args) == 2 && all(arg -> arg isa Function, args)
numfunc, formatter = args
return Formatted(map(numfunc, v), formatter)
else
return v
end
end
_postprocess_axis_args!(plotattributes, letter)
postprocess_axis_args!(plt, plotattributes, letter)
return w
end

Expand All @@ -69,36 +70,3 @@ _apply_type_recipe(
letter,
) = v
_apply_type_recipe(plotattributes, v::Nothing, letter) = v

# axis args before type recipes should still be mapped to all axes
function _preprocess_axis_args!(plotattributes)
plt = plotattributes[:plot_object]
for (k, v) in plotattributes
if is_axis_attribute(plt, k)
pop!(plotattributes, k)
for l in (:x, :y, :z)
lk = Symbol(l, k)
haskey(plotattributes, lk) || (plotattributes[lk] = v)
end
end
end
end
function _preprocess_axis_args!(plotattributes, letter)
plotattributes[:letter] = letter
_preprocess_axis_args!(plotattributes)
end

# axis args in type recipes should only be applied to the current axis
function _postprocess_axis_args!(plotattributes, letter)
plt = plotattributes[:plot_object]
pop!(plotattributes, :letter)
if letter in (:x, :y, :z)
for (k, v) in plotattributes
if is_axis_attribute(plt, k)
pop!(plotattributes, k)
lk = Symbol(letter, k)
haskey(plotattributes, lk) || (plotattributes[lk] = v)
end
end
end
end

0 comments on commit 385d1d1

Please sign in to comment.