working on gadfly ticks

This commit is contained in:
Thomas Breloff 2015-09-29 09:49:58 -04:00
parent ffc05e971f
commit 07324ae892
4 changed files with 52 additions and 202 deletions

File diff suppressed because one or more lines are too long

View File

@ -273,7 +273,7 @@ function replaceType(vec, val)
push!(vec, val)
end
function addTicksGuide(gplt, ticks, isx::Bool)
function addGadflyTicksGuide(gplt, ticks, isx::Bool)
ticks == :auto && return
ttype = ticksType(ticks)
if ttype == :ticks
@ -283,22 +283,29 @@ function addTicksGuide(gplt, ticks, isx::Bool)
gtype = isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks
replaceType(gplt.guides, gtype(ticks = collect(ticks[1])))
# TODO add xtick_label function (given tick, return label??) to gplt.ascetics
# probably want to add to gplt.mapping!
# TODO add xtick_label function (given tick, return label??)
# Scale.x_discrete(; labels=nothing, levels=nothing, order=nothing)
filterGadflyScale(gplt, isx)
gfunc = isx ? Gadfly.Scale.x_discrete : Gadfly.Scale.y_discrete
labelmap = Dict(zip(ticks...))
labelfunc = val -> labelmap[val]
push!(gplt.scales, gfunc(levels = tickvals, labels = labelfunc))
else
error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks)
end
end
isContinuousScale(scale, isx::Bool) = isa(scale, Gadfly.Scale.ContinuousScale) && scale.vars[1] == (isx ? :x : :y)
# isContinuousScale(scale, isx::Bool) = isa(scale, Gadfly.Scale.ContinuousScale) && scale.vars[1] == (isx ? :x : :y)
filterGadflyScale(gplt, isx::Bool) = filter!(scale -> scale.vars[1] == (isx ? :x : :y), gplt.scales)
function addLimitsScale(gplt, lims, isx::Bool)
function addGadflyLimitsScale(gplt, lims, isx::Bool)
lims == :auto && return
ltype = limsType(lims)
if ltype == :limits
# remove any existing scales, then add a new one
filterGadflyScale(gplt, isx)
gfunc = isx ? Gadfly.Scale.x_continuous : Gadfly.Scale.y_continuous
filter!(scale -> !isContinuousScale(scale,isx), gplt.scales)
# filter!(scale -> !isContinuousScale(scale,isx), gplt.scales)
push!(gplt.scales, gfunc(minvalue = min(lims...), maxvalue = max(lims...)))
else
error("Invalid input for $(isx ? "xlims" : "ylims"): ", lims)
@ -338,10 +345,10 @@ function updateGadflyGuides(gplt, d::Dict)
haskey(d, :title) && findGuideAndSet(gplt, Gadfly.Guide.title, d[:title])
haskey(d, :xlabel) && findGuideAndSet(gplt, Gadfly.Guide.xlabel, d[:xlabel])
haskey(d, :ylabel) && findGuideAndSet(gplt, Gadfly.Guide.ylabel, d[:ylabel])
haskey(d, :xlims) && addLimitsScale(gplt, d[:xlims], true)
haskey(d, :ylims) && addLimitsScale(gplt, d[:ylims], false)
haskey(d, :xticks) && addTicksGuide(gplt, d[:xticks], true)
haskey(d, :yticks) && addTicksGuide(gplt, d[:yticks], false)
haskey(d, :xlims) && addGadflyLimitsScale(gplt, d[:xlims], true)
haskey(d, :ylims) && addGadflyLimitsScale(gplt, d[:ylims], false)
haskey(d, :xticks) && addGadflyTicksGuide(gplt, d[:xticks], true)
haskey(d, :yticks) && addGadflyTicksGuide(gplt, d[:yticks], false)
end
function updatePlotItems(plt::Plot{GadflyPackage}, d::Dict)

View File

@ -266,8 +266,9 @@ function plot!(pkg::PyPlotPackage, plt::Plot; kw...)
end
# -----------------------------------------------------------------
function addLimitsScale(gplt, lims, isx::Bool)
function addPyPlotLims(lims, isx::Bool)
lims == :auto && return
ltype = limsType(lims)
if ltype == :limits
@ -277,7 +278,7 @@ function addLimitsScale(gplt, lims, isx::Bool)
end
end
function addTicksGuide(gplt, ticks, isx::Bool)
function addPyPlotTicks(ticks, isx::Bool)
ticks == :auto && return
ttype = ticksType(ticks)
if ttype == :ticks
@ -305,16 +306,16 @@ function updatePlotItems(plt::Plot{PyPlotPackage}, d::Dict)
end
# limits and ticks
haskey(d, :xlims) && addLimitsScale(fig, d[:xlims], true)
haskey(d, :ylims) && addLimitsScale(fig, d[:ylims], false)
haskey(d, :xticks) && addTicksGuide(fig, d[:xticks], true)
haskey(d, :yticks) && addTicksGuide(fig, d[:yticks], false)
haskey(d, :xlims) && addPyPlotLims(d[:xlims], true)
haskey(d, :ylims) && addPyPlotLims(d[:ylims], false)
haskey(d, :xticks) && addPyPlotTicks(d[:xticks], true)
haskey(d, :yticks) && addPyPlotTicks(d[:yticks], false)
end
# -------------------------------
# -----------------------------------------------------------------
function createPyPlotAnnotationObject(plt::Plot{PyPlotPackage}, x, y, val::AbstractString)
ax = getLeftAxis(plt.o[1])

View File

@ -104,7 +104,7 @@ function plot!(plt::Plot, args...; kw...)
setTicksFromStringVector(d, di, :x, :xticks)
setTicksFromStringVector(d, di, :y, :yticks)
@show di[:x] di[:y]
# @show di[:x] di[:y]
# println("Plotting: ", di)
plot!(plt.backend, plt; di...)
@ -130,21 +130,23 @@ end
# --------------------------------------------------------------------
# if x or y are a vector of strings, we should create a list of unique strings,
# and map x/y to be the index of the string... then set the x/y tick labels
function setTicksFromStringVector(d::Dict, di::Dict, sym::Symbol, ticksym::Symbol)
# if the x or y values are strings, set ticks to the unique values, and x/y to the indices of the ticks
@show sym di
# @show sym di
v = di[sym]
@show v
# @show v
isa(v, AbstractArray) || return
T = eltype(v)
@show T
# @show T
if T <: AbstractString || (!isempty(T.types) && all(x -> x <: AbstractString, T.types))
@show sym ticksym di[sym]
# @show sym ticksym di[sym]
ticks = unique(di[sym])
@show ticks
# @show ticks
di[sym] = Int[findnext(ticks, v, 1) for v in di[sym]]
if !haskey(d, ticksym) || d[ticksym] == :auto