Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: DateTime support #87

Closed
amellnik opened this issue Oct 12, 2016 · 7 comments
Closed

Feature request: DateTime support #87

amellnik opened this issue Oct 12, 2016 · 7 comments

Comments

@amellnik
Copy link

I think it's not a high priority for most of us since Dates work fine, but it would be nice to have. Right now DateTimes don't throw an error, but are treated as categorical strings.

@sglyon
Copy link
Member

sglyon commented Oct 13, 2016

Hey @amellnik I actually ran into this the other day.

The issue is that julia exports DateTime to string like this:

julia> now()
2016-10-13T07:20:58.166

whereas plotly.js expects the same, but without the T: 2016-10-13 07:20:58.166.

I asked the plotly.js guys if they could support the version with the `T, and they made it seem like it would be a bigger issue than "just allowing it". It's too bad because the version with the T is the official ISO 8601 specification.

There are two things I could think of on this side:

  1. Add JSON.lower(d::DateTime) that doesn't include the T. I don't really like this because we are extending someone else's method for someone else's type.
  2. Add a processing layer just before sending to JSON that would look for DateTime and convert it to the string we need. I've avoided this because I haven't wanted there to be data transformation between Julia and plotly.js.

If anyone has other ideas, I'd love to hear them!

@tbreloff
Copy link
Member

Not a solution of course, but you can plot DateTime through Plots. It uses
a type recipe which converts to numbers and adds a special tick formatter
to the axis. You'll need master until I tag a new version (today maybe?)

On Thursday, October 13, 2016, Spencer Lyon [email protected]
wrote:

Hey @amellnik https://github.com/amellnik I actually ran into this the
other day.

The issue is that julia exports DateTime to string like this:

julia> now()
2016-10-13T07:20:58.166

whereas plotly.js expects the same, but without the T: 2016-10-13
07:20:58.166.

I asked the plotly.js guys if they could support the version with the `T,
and they made it seem like it would be a bigger issue than "just allowing
it". It's too bad because the version with the T is the official ISO 8601
specification https://en.wikipedia.org/wiki/ISO_8601.

There are two things I could think of on this side:

  1. Add JSON.lower(d::DateTime) that doesn't include the T. I don't
    really like this because we are extending someone else's method for someone
    else's type.
  2. Add a processing layer just before sending to JSON that would look
    for DateTime and convert it to the string we need. I've avoided this
    because I haven't wanted there to be data transformation between Julia and
    plotly.js.

If anyone has other ideas, I'd love to hear them!


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#87 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA492lkwPylQgwMTPb8mbHpcn-UINVF1ks5qzhWggaJpZM4KU_qe
.

@sglyon sglyon closed this as completed Jun 23, 2017
@sglyon
Copy link
Member

sglyon commented Jun 23, 2017

I believe this is fixed in the latest plotly.js releases from upstream.

If someone finds otherwise please reopen or make a comment so I can reopen

@kafisatz
Copy link

kafisatz commented Oct 2, 2019

I currently get the error below when trying to use a datetime vector as x axis.
Is this supposed to work?

EDIT: I can see that string.(df.datetime) does the trick for the time being.

julia> aPlot=PlotlyJS.plot(df.datetime,df[!,varsOfInterest[varNum]],layout,size=plotsize,ticks = :native,dpi=200,show=true,xlabel="Time",ylabel=string(varsOfInterest[varNum]))
ERROR: MethodError: no method matching Plot(::Array{Dates.DateTime,1}, ::Array{Float64,1}, ::Layout{Dict{Symbol,Any}}; size=(2000, 800), ticks=:native, dpi=200, show=true, xlabel="Time", ylabel="TA [°C]")
Closest candidates are:
  Plot(::AbstractArray{T<:Union{Date, AbstractString, Number, Symbol},1}, ::AbstractArray{T,1} where T, ::Layout; kind, style, kwargs...) where T<:Union{Date, AbstractString, Number, Symbol} at C:\Users\bernhard.konig\.julia\packages\PlotlyBase\qL4g1\src\convenience_api.jl:20
  Plot(::AbstractArray{T<:Union{Date, AbstractString, Number, Symbol},1}, ::AbstractArray{T,1} where T) where T<:Union{Date, AbstractString, Number, Symbol} at C:\Users\bernhard.konig\.julia\packages\PlotlyBase\qL4g1\src\convenience_api.jl:20 got unsupported keyword arguments "size", "ticks", "dpi", "show", "xlabel", "ylabel"
  Plot(::AbstractArray{T<:Union{Date, AbstractString, Number, Symbol},1}, ::AbstractArray{T,2} where T, ::Layout; style, kwargs...) where T<:Union{Date, AbstractString, Number, Symbol} at C:\Users\bernhard.konig\.julia\packages\PlotlyBase\qL4g1\src\convenience_api.jl:25
  ...
Stacktrace:
 [1] #plot#6(::Dict{Any,Any}, ::Base.Iterators.Pairs{Symbol,Any,NTuple{6,Symbol},NamedTuple{(:size, :ticks, :dpi, :show, :xlabel, :ylabel),Tuple{Tuple{Int64,Int64},Symbol,Int64,Bool,String,String}}}, ::Function, ::Array{Dates.DateTime,1}, ::Vararg{Any,N} where N) at C:\Users\bernhard.konig\.julia\packages\PlotlyJS\I8Gki\src\display.jl:143
 [2] (::getfield(PlotlyJS, Symbol("#kw##plot")))(::NamedTuple{(:size, :ticks, :dpi, :show, :xlabel, :ylabel),Tuple{Tuple{Int64,Int64},Symbol,Int64,Bool,String,String}}, ::typeof(plot), ::Array{Dates.DateTime,1}, ::Vararg{Any,N} where N) at .\none:0
 [3] top-level scope at none:0

julia> 

julia> df.datetime
4560-element Array{Dates.DateTime,1}:
 2019-09-27T16:33:24
 2019-09-27T16:34:24
 2019-09-27T16:35:24
 2019-09-27T16:36:24
 2019-09-27T16:37:24
 2019-09-27T16:38:24
 2019-09-27T16:39:24

@sglyon
Copy link
Member

sglyon commented Oct 2, 2019

Hey @kafisatz I believe the issue comes from the definition of this internal type _Scalar: https://github.com/sglyon/PlotlyBase.jl/blob/e212bd8242121d98077ce7bfae3c76f16ee75b40/src/traces_layouts.jl#L59

If we were to add DateTime to that list I think the issue would be fixed.

@kafisatz are you willing to make this change, test this out using your data, and submit a pull request over in PlotlyBase (the repo where that union us defined) if it works?

@kafisatz
Copy link

kafisatz commented Oct 3, 2019

I can confirm that this change does the trick for my data.

@sglyon
Copy link
Member

sglyon commented Oct 3, 2019

Excellent! Thank you for making the pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants