From 273996aa910ddfe4bae162125765f490da536a99 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Wed, 23 Sep 2015 00:14:32 -0400 Subject: [PATCH] working on ticks/lims --- src/args.jl | 8 +++-- src/backends/gadfly.jl | 69 +++++++++++++++++++++++++++++------------- src/utils.jl | 5 ++- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/args.jl b/src/args.jl index 909afbb9..77dcd87c 100644 --- a/src/args.jl +++ b/src/args.jl @@ -116,6 +116,8 @@ _plotDefaults[:yrightlabel] = "" _plotDefaults[:legend] = true _plotDefaults[:background_color] = colorant"white" _plotDefaults[:foreground_color] = :auto +_plotDefaults[:xlims] = :auto +_plotDefaults[:ylims] = :auto _plotDefaults[:xticks] = :auto _plotDefaults[:yticks] = :auto _plotDefaults[:size] = (800,600) @@ -198,8 +200,10 @@ const _keyAliases = Dict( :fgcolor => :foreground_color, :fg_color => :foreground_color, :foreground => :foreground_color, - :xlim => :xticks, - :ylim => :yticks, + :xlim => :xlims, + :ylim => :ylims, + :xtick => :xticks, + :ytick => :yticks, :windowsize => :size, :wsize => :size, :wtitle => :windowtitle, diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index c1efffce..254ba5f0 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -200,32 +200,57 @@ function addGadflySeries!(gplt, d::Dict, initargs::Dict) nothing end -function addTicksGuideOrScale(gplt, ticks, isx::Bool) +# function addTicksGuideOrScale(gplt, ticks, isx::Bool) +# ticks == :auto && return +# ttype = ticksType(ticks) +# if ttype == :limits +# minvalue = min(ticks...) +# maxvalue = max(ticks...) +# scale = (isx ? Gadfly.Scale.x_continuous : Gadfly.Scale.y_continuous)(minvalue=minvalue, maxvalue=maxvalue) +# push!(gplt.scales, scale) +# elseif ttype == :ticks +# # if isx +# # ticks = map(Compose.x_measure, sort(collect(ticks))) +# # push!(gplt.statistics, Gadfly.Stat.xticks(ticks=ticks)) +# # else +# # ticks = map(Compose.y_measure, sort(collect(ticks))) +# # push!(gplt.statistics, Gadfly.Stat.yticks(ticks=ticks)) +# # end +# ticks = sort(ticks) +# guide = (isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks)(ticks=ticks) +# push!(gplt.guides, guide) +# # stat = (isx ? Gadfly.Stat.xticks : Gadfly.Stat.yticks)(ticks=ticks) +# # push!(gplt.statistics, stat) +# else +# error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks) +# end +# end + +function addTicksGuide(gplt, ticks, isx::Bool) ticks == :auto && return ttype = ticksType(ticks) - if ttype == :limits - minvalue = min(ticks...) - maxvalue = max(ticks...) - scale = (isx ? Gadfly.Scale.x_continuous : Gadfly.Scale.y_continuous)(minvalue=minvalue, maxvalue=maxvalue) - push!(gplt.scales, scale) - elseif ttype == :ticks - if isx - ticks = map(Compose.x_measure, sort(collect(ticks))) - push!(gplt.statistics, Gadfly.Stat.xticks(ticks=ticks)) - else - ticks = map(Compose.y_measure, sort(collect(ticks))) - push!(gplt.statistics, Gadfly.Stat.yticks(ticks=ticks)) - end - # ticks = map(Composesort(ticks) - # # guide = (isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks)(ticks=ticks) - # # push!(gplt.guides, guide) - # stat = (isx ? Gadfly.Stat.xticks : Gadfly.Stat.yticks)(ticks=ticks) - # push!(gplt.statistics, stat) + if ttype == :ticks + gtype = isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks + filter!(x -> !isa(x, gtype), gplt.guides) + push!(gplt.guides, gtype(ticks = sort(collect(ticks)))) else error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks) end end +function addLimitsScale(gplt, lims, isx::Bool) + lims == :auto && return + ltype = limsType(lims) + if ltype == :limits + gtype = isx ? Gadfly.Scale.x_continuous : Gadfly.Scale.y_continuous + filter!(x -> !isa(x, gtype), gplt.scales) + push!(gplt.scales, gtype(minvalue = min(lims...), maxvalue = max(lims...))) + else + error("Invalid input for $(isx ? "xlims" : "ylims"): ", lims) + end +end + + # --------------------------------------------------------------------------- # create a blank Gadfly.Plot object @@ -258,8 +283,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, :xticks) && addTicksGuideOrScale(gplt, d[:xticks], true) - haskey(d, :yticks) && addTicksGuideOrScale(gplt, d[:yticks], true) + 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) end function updatePlotItems(plt::Plot{GadflyPackage}, d::Dict) diff --git a/src/utils.jl b/src/utils.jl index 2f7e620b..5d7478e6 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -128,10 +128,13 @@ function regressionXY(x, y) regx, regy end -ticksType{T<:Real,S<:Real}(ticks::Tuple{T,S}) = :limits +# ticksType{T<:Real,S<:Real}(ticks::Tuple{T,S}) = :limits ticksType{T<:Real}(ticks::AVec{T}) = :ticks ticksType(ticks) = :invalid +limsType{T<:Real,S<:Real}(lims::Tuple{T,S}) = :limits +limsType(lims) = :invalid + # Some conversion functions # note: I borrowed these conversion constants from Compose.jl's Measure