5

I am trying to add some relevant hover information to the tooltip of a echarts4r plot. I want to make an boxplot that shows the user the name (or some other information) of the outliers. This is somewhat related to Add extra variables to tooltip pie chart echarts4r and Add extra variables to tooltip pie chart echarts4r, but those solutions do not work as bind does not work for e_boxplot.

This is what I have so far

library(echarts4r)

df <- data.frame(
    my_name = letters[1:11],
    x = c(1:10, 25),
    y = c(1:10, -6)
)

df |>
    e_charts() |>
    e_boxplot(y, outliers = TRUE) |>
    e_boxplot(x, outliers = TRUE) |>
    e_tooltip(
      formatter = htmlwidgets::JS("
        function(params)
        {
          return `<strong>${params.name}</strong>
            <br/>val: ${params.value[1]}
          <br/>name: ${params.my_name}`
        }
      "))
shafee
  • 15,566
  • 3
  • 19
  • 47
L Smeets
  • 888
  • 4
  • 17

1 Answers1

2

The only way I was able to get this to work was to do it manually. I've added a debugger statement to the formatter. You can print the echart and use the View in browser button: View in browser

Open up the Chrome Devtools console and roll over your scatter dot and the debugger will open in your callback function for you to explore the value object and get the desired information out of it for forming your tooltip. The object val will already have you y-axis value if you just want to work with that.

Hope this helps!

e <- df |>
        e_charts() |>
        e_boxplot(y, outliers = TRUE) |>
        e_boxplot(x, outliers = TRUE) |>
        e_tooltip(
          trigger = "item"
        )
      # Series 2 is the outlier scatter layer
      e$x$opts$series[[2]]$tooltip <- list(
        formatter = "(value) => {
           var val = value.value[1];
          debugger;
           // your subsequent changes
        }")