Currently, I am in the process of creating a doughnut chart with echarts4r
. As I delve into adding a custom tooltip to enhance the user experience, I have successfully referenced examples from Stack Overflow on stacked area charts (Echarts4r : Create stacked area chart with percentage from total in tooltip) and extra variables in tooltips for echarts4r (Displaying extra variables in tooltips echarts4r). However, adapting these concepts to a pie chart is proving to be a bit challenging. My goal is to implement a pie chart that displays both the total values and their relative percentages in the tooltip.
library(tidyverse)
library(echarts4r)
My_df <- data.frame(n = c(1, 4, 10),
x = c("A", "B", " C")) %>%
mutate(percent = round(n/sum(n), 2) )
My_df %>%
e_charts(x) %>%
e_pie(n, radius = c("50%", "70%")) %>%
e_tooltip()
Here's my progress so far:
My_df %>%
e_charts(x) %>%
e_pie(n, radius = c("50%", "70%")) %>%
e_tooltip(formatter = htmlwidgets::JS("
function(params){
return('<strong>' + params.name +
'</strong><br />total: ' + params.value +
'<br />percent: ' + params.value[1]) } "))
While the scatterplot examples utilize bind =
to include additional values, this approach doesn't seem to work smoothly with the pie chart.