daattali / timevis

📅 Create interactive timeline visualizations in R
http://daattali.com/shiny/timevis-demo/
Other
652 stars 157 forks source link

Timevis data returned doesn't work well #66

Closed IkerrSC closed 5 years ago

IkerrSC commented 5 years ago

I need to take the data returned from timevis, but these data are returned with one hour less then the data entered. I have tried changing the R time zone, but that has not solved the problem. here's a simple example of what I'm trying to do:

    ui<-fluidPage(

      mainPanel(
      timevisOutput("gantt"),
      tableOutput("return"),
      actionButton("btn","btn")
      )
    )

    server <- function(input, output, session) {

      data <- data.frame(
        id      = 1:4,
        content = c("Item one"  , "Item two"  ,"Ranged item", "Item four"),
        start   = c("2016-01-10 00:00:00", "2016-01-11", "2016-01-20", "2016-02-14-15:00:00"),
        end     = c(NA          ,           NA, "2016-02-04", NA),
        group   =c(1,1,2,2)
      )
      #genero el timevis
      output$gantt<-renderTimevis({

        timevis(data= data,
               groups = data.frame(id = 1:4, content = c(" 1", " 2", " 3", " 4")),
               options = list(editable = list(add=FALSE, remove=TRUE, updateTime= TRUE, 
                updateGroup=TRUE, overrideItems=TRUE),  align = "center"))

      })

      observeEvent(input$btn,{
        output$return<-renderTable(
              print(input$gantt_data)
        )
      })
    }
    shinyApp(ui, server)

These are the data returned

       id     content                    start group                      end
    1  1    Item one 2016-01-09T23:00:00.000Z     1                     <NA>
    2  2    Item two 2016-01-10T23:00:00.000Z     1                     <NA>
    3  3 Ranged item 2016-01-19T23:00:00.000Z     2 2016-02-03T23:00:00.000Z
    4  4   Item four 2016-02-14T14:00:00.000Z     2                     <NA>
JesusPF1981 commented 5 years ago

I have observe the same. Also in the timevis demo website appears this bug: https://daattali.com/shiny/timevis-demo/

When you move an Item, the time that appears in timeline and the time that appears on table are different. I attach a descriptive capture:

imagen

daattali commented 5 years ago

Thanks for the report.

I've found the source of the problem. R automatically converts the time stamps to UTC , while the time shown on the widget is local time.

The solution would be to allow the user to set a preferred timezone, and if a preferred timezone is given then all start and end values should be converted to the given timezone.

I'm not an expert in dealing with timezone issues, but this line of code is the best I could come up with to resolve this:

timevis_tz <- "America/Los_Angeles"
test_time <- "2016-01-10T06:00:00.000Z"
lubridate::with_tz(as.POSIXct(test_time), timevis_tz)

If anyone wants to fix this issue and submit a PR, I'd be happy to review and merge.

daattali commented 5 years ago

After looking into it some more, I don't think this is something that can or should be fixed. Timevis correctly returns the dates in a UTC timezone format, and that's ok. The end user can convert it to any other timezone if they wish to do so.

I just updated the app at https://daattali.com/shiny/timevis-demo/ using https://github.com/daattali/timevis/commit/cdfa832046df224924ec4a6e344fd2a39ae5ced6#diff-2ee94ad3267319f15acf63babaec8bf3 - there's unfortunately still going to be some discrepancy in that app for you if you're not in the same timezone as the app's server (EST), that's because the times in the widget are initialized on the client side, in javascript, so it will show up at 00:00 in the widget, and it will be treated as midnight in your timezone. But when R receives the data, as I said above, the timezone information is lost, it only receives the time based on UTC and it doesn't know what timezone you are in, so it converts it to the timezone the server is in, which is EST.

JesusPF1981 commented 5 years ago

Looking the documentation on vis.js I have seen that you can set your timezone by using the options. For example,if you want to convert the timezone to UTC, you must write on vis.js this:

   var options = {
       moment: function(date) {
       return vis.moment(date).utc();
     }
   };

But this I dont know how to implement it. I have tried with:

 timevis(data,options=list(moment=function(date){..}) 

but this givee me an error.

Also I have trying to add this code on the timevis.js, but also not work.

I think that with this part of code, but well implemented, should work fine.

daattali commented 5 years ago

If I'm not mistaken, using moment in visjs is an addition that happened after the version that is used in timevis. Every once in a while I test out the newest version of the javascript library to see if I can update timevis, but I keep finding bugs with it so I'm still on an old version that doesn't use moment. I might be wrong, but that's what I seem to remember.

JesusPF1981 commented 5 years ago

I think that moment is used on the version that you are using right now...

daattali commented 5 years ago

@jpara3 yes you're right, moment is indeed available, I'm not sure why I thought it wasn't! Allowing timezone changes can be a separate issue if you want to open one