daattali / timevis

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

Add custom button like a reset zoom #125

Closed roman-tremmel closed 1 year ago

roman-tremmel commented 1 year ago

I wonder wether it is possible to add custom buttons next to the + and - button?

In leaflet one can use sth like

library(leaflet)
addEasyButton(easyButton(
      icon = "fa-globe", title = "Reset Zoom",
      onClick = JS("function(btn, map){ map.setView([0, 45], 2);}")))
daattali commented 1 year ago

That's not supported unfortunately. The underlying javascript library (https://visjs.github.io/vis-timeline/docs/timeline/index.html) doesn't have any support for this. If you would like you can do it yourself though with just a little bit of CSS, for example:

library(shiny)

ui <- fluidPage(
  div(
    style = "position: relative",
    timevisOutput("test"),
    actionButton("btn", "btn", style = "position: absolute; left: 5px; top: 5px")
  )
)

server <- function(input, output, session) {
  output$test <- renderTimevis({
      timevis()
  })
}

shinyApp(ui, server)
roman-tremmel commented 1 year ago

OK, that was easy. Thank you very much for pointing out the solution.

library(shiny)

ui <- fluidPage(
  div(
    style = "position: relative",
    timevisOutput("test"),
    actionButton("btn_zoom", "Resize", icon=icon("maximize"), 
                 style = "position: absolute; right: 70px; top: 5px; padding: 4px; background-color: #ffffff; border-color: #cccccc;"))
  )
)

server <- function(input, output, session) {
  output$test <- renderTimevis({
    timevis()
  })

  observeEvent(input$btn_zoom, {
    timevis::setWindow("test",start = Sys.Date()-10, end=Sys.Date()+10)
  })
}

shinyApp(ui, server)

image

daattali commented 1 year ago

Thanks for following up!