From 034a1af649ca29efb1d293b85de7c711c629f421 Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Wed, 29 Mar 2017 23:20:30 +0200 Subject: [PATCH] restore bar_width behavior for date x axis --- src/axes.jl | 21 +++++++++++++++++++-- src/recipes.jl | 6 +++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/axes.jl b/src/axes.jl index d3893330..7af2d7c5 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -158,9 +158,26 @@ function optimal_ticks_and_labels(axis::Axis, ticks = nothing) # If the axis input was a Date or DateTime use a special logic to find # "round" Date(Time)s as ticks + # This bypasses the rest of optimal_ticks_and_labels, because + # optimize_datetime_ticks returns ticks AND labels: the label format (Date + # 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 axis[:formatter] in (dateformatter, datetimeformatter) && ticks == nothing && scale == :identity - return optimize_datetime_ticks(amin, amax; k_min = 2, k_max = 4) + if ticks == nothing && scale == :identity + if axis[:formatter] == dateformatter + # optimize_datetime_ticks returns ticks and labels(!) based on + # integers/floats corresponding to the DateTime type. Thus, the axes + # limits, which resulted from converting the Date type to integers, + # are converted to 'DateTime integers' (actually floats) before + # being passed to optimize_datetime_ticks. + # (convert(Int, convert(DateTime, convert(Date, i))) == 87600000*i) + ticks, labels = optimize_datetime_ticks(864e5 * amin, 864e5 * amax; + k_min = 2, k_max = 4) + # Now the ticks are converted back to floats corresponding to Dates. + return ticks / 864e5, labels + elseif axis[:formatter] == datetimeformatter + return optimize_datetime_ticks(amin, amax; k_min = 2, k_max = 4) + end end # get a list of well-laid-out ticks diff --git a/src/recipes.jl b/src/recipes.jl index a4f576a3..3f63ea40 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -792,11 +792,11 @@ abline!(args...; kw...) = abline!(current(), args...; kw...) # ------------------------------------------------- # Dates -dateformatter(dt) = string(convert(Date, convert(DateTime, dt))) +dateformatter(dt) = string(convert(Date, dt)) datetimeformatter(dt) = string(convert(DateTime, dt)) -@recipe f(::Type{Date}, dt::Date) = (dt -> convert(Int, convert(DateTime, dt)), dateformatter) -@recipe f(::Type{DateTime}, dt::DateTime) = (dt -> convert(Int,dt), datetimeformatter) +@recipe f(::Type{Date}, dt::Date) = (dt -> convert(Int, dt), dateformatter) +@recipe f(::Type{DateTime}, dt::DateTime) = (dt -> convert(Int, dt), datetimeformatter) # ------------------------------------------------- # Complex Numbers