dreamRs / billboarder

:bar_chart: R Htmlwidget for billboard.js
https://dreamrs.github.io/billboarder/
Other
174 stars 21 forks source link

Hover Possible? #24

Closed rsangole closed 3 years ago

rsangole commented 3 years ago

Like you have in your example for click

output$click <- renderPrint({
    cat("# input$mybbchart_click$category", "\n")
    input$mybbchart_click$category
  })

I couldn't get "hover" to work using something like input$mybbchart_hover$category. Is this something that's possible? Which Shiny events are currently supported?

pvictor commented 3 years ago

Yes event for hover exist, in JS library it's actually called "over" (https://naver.github.io/billboard.js/release/latest/doc/Options.html#.onover) so I used the same name, the syntax for retrieving input is input$<ID>_over, here's an example:

library(shiny)
library(billboarder)

ui <- fluidPage(
  tags$h1("Shiny events"),
  fluidRow(
    column(
      width = 6,
      billboarderOutput(outputId = "mybb1")
    ),
    column(
      width = 6,
      verbatimTextOutput(outputId = "click1"),
      verbatimTextOutput(outputId = "over1")
    )
  )
)

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

  output$mybb1 <- renderBillboarder({
    mtcars$model <- rownames(mtcars)
    billboarder() %>%
      bb_barchart(data = mtcars[1:5, ], mapping = bbaes(x = model, y = mpg))
  })

  output$click1 <- renderPrint({
    input$mybb1_click
  })
  output$over1 <- renderPrint({
    input$mybb1_over
  })

}

shinyApp(ui = ui, server = server)

PS: there's a little with category and hover, if you're experiencing some issues please re-install from Github.

Victor

rsangole commented 3 years ago

Thanks @pvictor . This worked perfectly.

Followup Q:

Is it possible to detect a click on a barchart when the user isn't ON the bar? My use case is highly skewed data like this:

image

You can see where the user's mouse is currently placed. It's intuitive for the user to simply click right where the mouse is... It's not intuitive for the user to have to move the mouse OVER the bar and then click. This is causing some UX frustration.

Any ideas?

Edit

If the user was forced to click on the bar, the legend blocks the view, making the UX more frustrating...

image

Edit 2

One idea I had was to move the tooltip to the bottom like in this example, but I don't know how to implement this in R + Shiny.

pvictor commented 3 years ago

Hello,

As far as I know it's not possible to retrieve click information without clicking the data object...

Here's an example for fixed tooltip if you want to try (you have to use some HTML to format content):

library(shiny)
library(billboarder)
data("economics_long", package = "ggplot2")

ui <- fluidPage(
  tags$h2("Fixed tooltip"),
  fluidRow(
    column(
      width = 8,
      billboarderOutput("mychart")
    ),
    column(
      width = 4,
      tags$div(id = "mytooltip")
    )
  )
)

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

  output$mychart <- renderBillboarder({
    billboarder(data = economics_long) %>% 
      bb_linechart(
        mapping = bbaes(x = date, y = value01, group = variable)
      ) %>% 
      bb_x_axis(tick = list(fit = FALSE)) %>% 
      bb_tooltip(
        init = list(
          show = TRUE
        ),
        doNotHide = TRUE,
        contents = list(
          bindto = "#mytooltip",
          template = "<ul><li>Index<br>{=TITLE}</li>{{<li class={=CLASS_TOOLTIP_NAME}><span>{=VALUE}</span><br><span style=color:{=COLOR}>{=NAME}</span></li>}}</ul>"
        )
      )
  })

}

shinyApp(ui, server)
rsangole commented 3 years ago

Thanks @pvictor. This was very helpful. Perhaps you could include this full example in bb_tooltip, for folks like me who don't know html & css as much?