Search code examples
rechartsecharts4r

show Label as percentage in echarts4r


df1 <- data.frame(Date = c(1,2,3),
                  Reach = c(12,15,20),
                  Cost = c(0.98,0.00056,0.89))

df1 %>% 
  e_chart(Date) %>% 
  e_bar(Reach, label = list(show = FALSE)) %>% 
  e_line(Cost, y_index = 1, label = list(show = TRUE), scales = list(x = list(label = scales::percent_format(scale = 1)))) %>%
  e_tooltip() %>% 
  e_title("Actual v CPC")

so I tried this code to show a bar and line charts using echarts4r and then i want to show the e_line label but not the e_bar and it's already working with this code, but then because i want to show the e_line label with percentage i use scales or {d}% in e_line formatter but it still dont work. I also tried using e_labels but i cant hide the bar label


Solution

  • One option would be to use a small JS formatter function like so:

    library(echarts4r)
    library(magrittr)
    
    df1 %>%
      e_chart(Date) %>%
      e_bar(Reach, label = list(show = FALSE)) %>%
      e_line(Cost, y_index = 1, 
             label = list(
               show = TRUE, 
               formatter = htmlwidgets::JS(
                 "(params) => params.value[1] + ' %';")
        )) %>%
      e_tooltip() %>%
      e_title("Actual v CPC")
    

    enter image description here