向工具提示饼图添加额外变量 echarts4r

L S*_*ets 5 javascript r echarts

我正在使用创建一个圆环图echarts4r。现在我正在尝试添加自定义工具提示,并且我能够复制此处给出的示例Echarts4r :使用工具提示中总计的百分比创建堆叠面积图和此处在工具提示 echarts4r 中显示额外变量。但是,我不太明白这是如何扩展到饼图的。我想要一个带有工具提示的饼图,显示总数和相对百分比

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()
Run Code Online (Sandbox Code Playgroud)

这是我迄今为止最好的镜头

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])   }  "))
Run Code Online (Sandbox Code Playgroud)

在散点图示例中,附加值使用附加值bind =,但这不适用于饼图。

nor*_*rie 5

你不能用吗params.percent

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.percent)  +'%' }  "))
Run Code Online (Sandbox Code Playgroud)

您还可以使用 Javascript模板文字来整理一些内容。

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.percent}%`
                                        }  "))
Run Code Online (Sandbox Code Playgroud)