Search code examples
recharts4r

How to change area style with echarts4r::e_area()


I can change the line style and the item style, but I cannot seem to be able to pass arguments to areaStyle (see areaStyle).

For example:

library(echarts4r)
data.frame(x = seq.int(1, 5, 1),
           y = 10) %>% 
  e_chart(x = x) %>% 
  e_area(
    serie = y,
    areaStyle = list(opacity = 0),
    lineStyle = list(opacity = 0),
    itemStyle = list(opacity = 0)
  )

produces an area chart with no points and no line, but the area is still visible. How do I change the color, opacity, etc. of the area itself?


Solution

  • I had a look at the source code of e_area_ (which is called by e_area). and the issue is that e_area_ inits areaStyle as an empty list. See https://github.com/JohnCoene/echarts4r/blob/bf23891749cf42a40656fa87ff04ecb3627a9af5/R/add_.R#L263-L269 . And unfortunately this empty list doesn't gets updated when the user provides his own specs. Not sure whether this is a bug or whether this is intended. Perhaps you should file an issue.

    As a possible quick workaround here is "fixed" e_area2_ which updates the default empty list via modifyList:

    library(echarts4r)
    library(dplyr)
    
    e_area2_ <- function(e, serie, bind = NULL, name = NULL, legend = TRUE,
                         y_index = 0, x_index = 0, coord_system = "cartesian2d", ...) {
      .default <- list(areaStyle = list())
    
      args <- utils::modifyList(.default, list(...))
    
      if (missing(e)) {
        stop("must pass e", call. = FALSE)
      }
      if (missing(serie)) {
        stop("must pass serie", call. = FALSE)
      }
      for (i in seq_along(e$x$data)) {
        vector <- echarts4r:::.build_data2(
          e$x$data[[i]], e$x$mapping$x,
          serie
        )
        if (!is.null(bind)) {
          vector <- echarts4r:::.add_bind2(e, vector, bind, i = i)
        }
        l <- list(data = vector)
        if (coord_system == "cartesian2d") {
          if (y_index != 0) {
            e <- echarts4r:::.set_y_axis(e, serie, y_index, i)
          }
          if (x_index != 0) {
            e <- echarts4r:::.set_x_axis(e, x_index, i)
          }
          l$yAxisIndex <- y_index
          l$xAxisIndex <- x_index
        } else if (coord_system == "polar") {
          l$data <- as.list(unname(unlist(dplyr::select(
            e$x$data[[i]],
            serie
          ))))
        }
        if (!e$x$tl) {
          nm <- echarts4r:::.name_it(e, serie, name, i)
          args
          opts <- c(
            list(name = nm, type = "line", coordinateSystem = coord_system),
            args
          )
    
          l <- append(l, opts)
          if (isTRUE(legend)) {
            e$x$opts$legend$data <- append(
              e$x$opts$legend$data,
              list(nm)
            )
          }
          e$x$opts$series <- append(e$x$opts$series, list(l))
        } else {
          e$x$opts$options[[i]]$series <- append(
            e$x$opts$options[[i]]$series,
            list(l)
          )
        }
      }
      if (isTRUE(e$x$tl)) {
        if (is.null(name)) {
          name <- serie
        }
        series_opts <- c(
          list(
            name = name, type = "line", yAxisIndex = y_index,
            xAxisIndex = x_index, coordinateSystem = coord_system
          ),
          args
        )
        if (isTRUE(legend)) {
          e$x$opts$baseOption$legend$data <- append(
            e$x$opts$baseOption$legend$data,
            list(name)
          )
        }
        e$x$opts$baseOption$series <- append(
          e$x$opts$baseOption$series,
          list(series_opts)
        )
      }
      e
    }
    
    data.frame(
      x = seq.int(1, 5, 1),
      y = 10
    ) %>%
      e_chart(x = x) %>%
      e_area2_(
        serie = "y",
        areaStyle = list(opacity = 0),
        lineStyle = list(opacity = 0),
        itemStyle = list(opacity = 0)
      )