3

I'm trying to make plot with extra variables in tooltips, but can't understand how to make it.
For example I want to display qsec, cyl and hp columns
Tutorial from API didn't help me
Example :

library(dplyr)
library(echarts4r)
mtcars %>%  
  tibble::rownames_to_column("model") %>% 
  e_charts(wt) %>% 
  e_scatter(mpg,bind=model) %>%
  e_tooltip(formatter = htmlwidgets::JS("
                                        function(params){
                                        return('<strong>' + params.name + 
                                        '</strong><br />wt: ' + params.value[0] + 
                                        '<br />mpg: ' +  params.value[1] +
                                        '<br />qsec: ' +  this.qsec )   }  "))

expected result something like this:
https://github.com/jbkunst/highcharter/issues/54

jyjek
  • 2,627
  • 11
  • 23

2 Answers2

9

A bit hacky, but you can pass in a string with the data you wish to display as the data name, then parse it inside the function. For example,

mtcars %>%  
  tibble::rownames_to_column("model") %>%
  mutate(model = paste(model, qsec, sep = ",")) %>%
  e_charts(wt) %>% 
  e_scatter(mpg, bind = model) %>%
  e_tooltip(formatter = htmlwidgets::JS("
                                        function(params){
                                          var vals = params.name.split(',')
                                          return('<strong>' + vals[0] + 
                                          '</strong><br />wt: ' + params.value[0] + 
                                          '<br />mpg: ' +  params.value[1]) +
                                          '<br />qsec: ' + vals[1]}  "))

which gives you

enter image description here

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
2

Does this give the intended effect?

library(dplyr)
library(echarts4r)
mtcars %>%  
  tibble::rownames_to_column("model") %>% 
  e_charts(wt) %>% 
  e_scatter(mpg, qsec, bind=model) %>% # pass qsec as size
  e_tooltip(formatter = htmlwidgets::JS("
                                        function(params){
                                        return('<strong>' + params.name + 
                                        '</strong><br />wt: ' + params.value[0] + 
                                        '<br />mpg: ' +  params.value[1] +
                                        '<br />qsec: ' +  params.value[2] )   }  ")) # size = third value
JohnCoene
  • 2,107
  • 1
  • 14
  • 31