Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying extra variables in tooltips echarts4r

Tags:

r

echarts

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

like image 510
jyjek Avatar asked Oct 25 '25 22:10

jyjek


1 Answers

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

like image 191
Weihuang Wong Avatar answered Oct 27 '25 15:10

Weihuang Wong