JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
587 stars 82 forks source link

How I can show the labels on echarts4r plot but just every 2 or 3 bars? #516

Closed Jorge-hercas closed 10 months ago

Jorge-hercas commented 1 year ago

Hi and thanks for reading me. Im working on a echarts plot in R and I want to show the label of the vars but only in every 2 or 3 bars, because i don't want it to look too saturated with information

I haven't found a working argument to e_labels(), could you help me? please

My code is the following:

library(echarts4r)

mtcars |> 
  tibble::rownames_to_column("model") |> 
  e_charts(model) |> 
  e_bar(cyl) |> 
  e_legend(F) |> 
  e_labels()
Jorge-hercas commented 1 year ago

I tried using a JS function and it works, but im not sure how I can make the label only shows the value of "cyl". My code is:

mtcars |> 
  tibble::rownames_to_column("model") |> 
  e_charts(model)|> 
  e_bar(cyl) |> 
  e_labels(formatter = htmlwidgets::JS("function(params) {
    var total = params.data.length;
    var step = 3;
    if ((params.dataIndex + 1) % step !== 0) {
      return '';
    }else{
      return params.value;
    }
}
"), rotate = 90, position = "inside")
rdatasculptor commented 1 year ago

@Jorge-hercas My approach would be not using formatter but doing it like this with the help of e_add_nested():

library(echarts4r)
library(dplyr)

df <- mtcars |> 
  tibble::rownames_to_column("model") |> arrange(model)
df$show <- rep(c(TRUE, FALSE, FALSE), 
               length.out = nrow(df))

df |> 
  e_charts(model) |> 
  e_bar(cyl, label = list(position = "insideTop")) |> 
  e_add_nested("label", show) |>
  e_legend(F)

Does the output look like the chart you were looking for?

munoztd0 commented 10 months ago

Closing because of inactivity