working on Axis type

This commit is contained in:
Thomas Breloff 2016-05-13 14:42:11 -04:00
parent 98dc52f124
commit 2ba4ac6d20
4 changed files with 79 additions and 8 deletions

View File

@ -11,7 +11,7 @@ const _3dTypes = [:path3d, :scatter3d, :surface, :wireframe, :contour3d]
const _allTypes = vcat([
:none, :line, :path, :steppre, :steppost, :sticks, :scatter,
:heatmap, :hexbin, :hist, :hist2d, :hist3d, :density, :bar, :hline, :vline, :ohlc,
:contour, :pie, :shape, :image #, :boxplot, :violin, :quiver,
:contour, :pie, :shape, :image #, :boxplot, :violin, :quiver,
], _3dTypes)
@compat const _typeAliases = KW(
:n => :none,
@ -215,6 +215,9 @@ _plotDefaults[:annotation] = nothing # annotation tuple(s)... (
_plotDefaults[:overwrite_figure] = false
_plotDefaults[:polar] = false
_plotDefaults[:aspect_ratio] = :none # choose from :none or :equal
_plotDefaults[:xaxis] = xaxis()
_plotDefaults[:yaxis] = yaxis()
_plotDefaults[:zaxis] = zaxis()
# TODO: x/y scales

View File

@ -404,6 +404,7 @@ function discrete_value!(a::Axis, v)
cv = max(0.5, emax + 1.0)
expand_extrema!(a, cv)
a[:discrete_map][v] = cv
push!(a[:discrete_values], (cv, v))
end
cv
end
@ -417,6 +418,25 @@ Base.getindex(a::Axis, k::Symbol) = getindex(a.d, k)
Base.setindex!(a::Axis, v, ks::Symbol...) = setindex!(a.d, v, ks...)
Base.extrema(a::Axis) = a[:extrema]
# get discrete ticks, or not
function get_ticks(a::Axis)
ticks = a[:ticks]
dvals = a[:discrete_values]
if !isempty(dvals) && ticks == :auto
vals, labels = unzip(dvals)
else
ticks
end
end
const _axis_symbols = (:label, :lims, :ticks, :scale, :flip, :rotation)
const _axis_symbols_fonts_colors = (
:guidefont, :tickfont,
:foreground_color_axis,
:foreground_color_border,
:foreground_color_text,
:foreground_color_guide
)
# function processAxisArg(d::KW, letter::AbstractString, arg)
function Axis(letter::AbstractString, args...; kw...)
@ -437,16 +457,16 @@ function Axis(letter::AbstractString, args...; kw...)
# :foreground_color_guide => :match,
:extrema => (Inf, -Inf),
:discrete_map => Dict(), # map discrete values to continuous plot values
:discrete_values => [],
:use_minor => false,
:show => true, # show or hide the axis? (useful for linked subplots)
)
for sym in (:label, :lims, :ticks, :scale, :flip, :rotation)
for sym in _axis_symbols
k = symbol(letter * string(sym))
d[k] = default(k)
d[k] = _plotDefaults[k]
end
for k in (:guidefont, :tickfont, :foreground_color_axis, :foreground_color_border,
:foreground_color_text, :foreground_color_guide)
d[k] = default(k)
for k in _axis_symbols_fonts_colors
d[k] = _plotDefaults[k]
end
# first process args
@ -489,9 +509,12 @@ function Axis(letter::AbstractString, args...; kw...)
end
end
# then override for any keywords
# then override for any keywords... only those keywords that already exists in d
for (k,v) in kw
d[k] = v
sym = symbol(string(k)[2:end])
if haskey(d, sym)
d[sym] = v
end
end
Axis(d)

View File

@ -74,7 +74,46 @@ function plot!(plt::Plot, args...; kw...)
_plot!(plt, d, args...)
end
function strip_first_letter(s::Symbol)
str = string(s)
str[1:1], symbol(str[2:end])
end
# merge the KW d into the plot args
function _add_plotargs!(plt::Plot, d::KW)
# handle axis updates from a recipe
for letter in ("x","y","z")
# get the Axis object
axis = plt.plotargs[symbol(letter * "axis")]
# update xlabel, xscale, etc
for k in _axis_symbols
lk = symbol(letter * string(k))
if haskey(d, lk)
axis[k] = d[lk]
end
end
# update guidefont, etc
for k in _axis_symbols_fonts_colors
if haskey(d, k)
axis[k] = d[k]
end
end
# update extrema and discrete values
datasym = symbol(letter)
if haskey(d, datasym)
v = d[datasym]
if eltype(v) <: Number
expand_extrema!(axis, v)
else
d[datasym] = discrete_value!(axis, v)
end
end
end
for k in keys(_plotDefaults)
if haskey(d, k)
plt.plotargs[k] = pop!(d, k)

View File

@ -68,6 +68,8 @@ facts("UnicodePlots") do
@fact isa(plot(rand(10)), Plot) --> true
end
facts("Axes") do
axis = xaxis()
@fact typeof(axis) --> Axis
@ -75,6 +77,10 @@ facts("Axes") do
@fact Plots.discrete_value!(axis, :yo) --> 1.5
@fact extrema(axis) --> (0.5,1.5)
@fact axis[:discrete_map] --> Dict{Any,Any}(:yo => 1.5, "HI" => 0.5)
Plots.discrete_value!(axis, ["x$i" for i=1:5])
Plots.discrete_value!(axis, ["x$i" for i=0:2])
@fact extrema(axis) --> (0.5, 7.5)
end