From f2235abf08cca48209954c7378075e55efd3f7f2 Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Mon, 6 Mar 2017 23:52:59 +0100 Subject: [PATCH] Added logic to choose *round* DateTime tick positions --- src/axes.jl | 7 +++++++ src/recipes.jl | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/axes.jl b/src/axes.jl index b9840e7a..dd36f824 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -156,6 +156,13 @@ function optimal_ticks_and_labels(axis::Axis, ticks = nothing) scale = axis[:scale] sf = scalefunc(scale) + # If the axis input was a Date or DateTime use a special logic to find + # "round" Date(Time)s as ticks + # TODO: maybe: non-trivial scale (:ln, :log2, :log10) for date/datetime + if axis[:formatter] in (dateformatter, datetimeformatter) && scale == :identity + return optimize_datetime_ticks(amin, amax; k_min = 2, k_max = 4) + end + # get a list of well-laid-out ticks scaled_ticks = if ticks == nothing optimize_ticks( diff --git a/src/recipes.jl b/src/recipes.jl index 3fa7a46e..a4f576a3 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -792,8 +792,11 @@ abline!(args...; kw...) = abline!(current(), args...; kw...) # ------------------------------------------------- # Dates -@recipe f(::Type{Date}, dt::Date) = (dt -> convert(Int,dt), dt -> string(convert(Date,dt))) -@recipe f(::Type{DateTime}, dt::DateTime) = (dt -> convert(Int,dt), dt -> string(convert(DateTime,dt))) +dateformatter(dt) = string(convert(Date, convert(DateTime, 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) # ------------------------------------------------- # Complex Numbers