Search code examples
rradar-chartecharts4r

Gradient color in radarchart using echarts in R


I want to reproduce this gradient color effect using echarts in R https://www.programmersought.com/article/75124213435/.

As far as I have understood it is possible to recreate all echarts in R using nested lists. I´ve tried following some of the examples from here https://www.programmersought.com/article/75124213435/ but I quickly realized that I don't know how to use the lists or the arguments to get the gradient colors.

This is what I have done so far:

library(echarts4r)

df <- data.frame(
  x = LETTERS[1:10],
  y = round(runif(10, 1, 5) * 2 ) /2
)

df %>%
  e_charts(x) %>%
  e_radar(y, max = 5,
          areaStyle = list(
            list(color = "green"),
            list(color = "red")
          ),
          itemStyle = list(
            list(color = "green"), 
            list(color = "red")
          ))

I am stuck here and would appreciate some help on how to go from here.


Solution

  • Making use of htmlwidgets::JS and the specification of the color gradient in the linked code you could add a color gradient to your radar chart like so:

    ``` r
    library(echarts4r)
    
    set.seed(42)
    
    df <- data.frame(
      x = LETTERS[1:10],
      y = round(runif(10, 1, 5) * 2) / 2
    )
    
    linear_gradient <- htmlwidgets::JS(
      "new echarts.graphic.LinearGradient(
        0, 0, 0, 1,
        [
          { offset: 0, color: 'red' },
          { offset: 1, color: 'green' }
        ])"
    )
    
    df %>%
      e_charts(x) %>%
      e_radar(y,
              max = 5,
              itemStyle = list(
                color = linear_gradient
              ),
              areaStyle = list(
                color = linear_gradient
              )
      )