dreamRs / billboarder

:bar_chart: R Htmlwidget for billboard.js

Home Page:https://dreamrs.github.io/billboarder/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Customizing the Tooltip / Adding to the Data object

elbamos opened this issue · comments

I'd like to customize the tooltip shown when a user hovers over a scatterplot point. To do this, I need to pull data out of the data object passed to format.contents. However, not all of the fields from my data.frame are getting into data. How can I add fields to data, or otherwise get the other columns from the data.frame into the right place?

Here is an example that should illustrate what I mean:

data.frame(
  x = rnorm(200), 
  y = rnorm(200), 
  group = sample(letters[1:3], size=200, replace=T), 
  label = sample(letters[4:26], size=200, replace=T) 
) %>%
  billboarder(data=.) %>%
  bb_aes(x = x, y = y, group=group, label=label) %>%
  bb_scatterplot() %>%
  bb_tooltip(grouped=FALSE, 
             contents = htmlwidgets::JS(
               "function(data, titleFormat, valueFormat, color) {console.log(data);return `<table><tr><td>${data[0].label}</td></tr><tr><td>${data[0].x}</td></tr><tr><td>${data[0].value}</td></tr></table>`;}"
             ))

As of now, the labels are all undefined, because data has group for both the id and name fields.

That's complicated... the additional data are not kept, it is not possible to refer to label. A solution will be to pass a custom JSON in the contents function and retrieve data by index and group. I'll investigate.