JohnCoene / echarts4r

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

Reordering legend data (with workaround) #591

Open Mkranj opened 7 months ago

Mkranj commented 7 months ago

Hi, I'm plotting a chart with multiple bars and need to stack them in one order, and display the legend in another. I thought I could rearrange the legend data by supplying the data argument, but that actually creates a separate data object in the JSON.

I've found a workaround by modifying the data in the saved object, and then it gives the expected result. Here's a reproducible example:

library(echarts4r)

dataset <- data.frame(
  categories = letters[1:5],
  smaller = 1:5,
  larger = 21:25
)

my_chart <- e_charts(dataset, x = categories) %>%
  e_bar(serie = larger, name = "Big") %>%
  e_bar(serie = smaller, name = "Small") %>%
  e_legend(data = list("Small", "Big"))

# This one creates two "data"s
my_chart
my_chart %>% e_inspect()

# This reorganises as wanted
my_chart$x$opts$legend$data <- list("Small", "Big")

my_chart

Is there a way to reorganise the data by providing the list directly to e_legend, instead of modifying a chart stored as an object?