JohnCoene / echarts4r

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

e_add_nested fails with multiple series. .serie argument intention? #574

Closed stuvet closed 8 months ago

stuvet commented 9 months ago

Hi, & thanks for all the work.

I've hit a roadblock trying to add itemStyle: color to a chart with more than one series using e_add_nested. Hope the attached reprex explains it. I've tried both the latest CRAN release & the latest development version.

Please note, I've tried the .serie argument of e_add_nested and it skips any changes completely (produces the graph unchanged). From reading the code for this argument it appears to target the individual datapoint, rather than the series itself. Perhaps this is the intention? If so, it would be helpful for the docs to be more explicit. Thanks.

Unexpected behaviour

library(echarts4r)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

.data <- tibble(x = 2 * pi * (1:100)/100) |>
    mutate(
        y1_bar = 0.5 * (sin(x)) + 1,
        y2_bar = 0.5 * (sin(x)) - 1,
        deltaY1 = rnorm(100, sd = 0.1),
        deltaY2 = rnorm(100, sd = 0.1),
        y1 = y1_bar + deltaY1,
        y2 = y2_bar + deltaY2,
        sizeY1 = abs(deltaY1),
        sizeY2 = abs(deltaY2),
        # Top series should be teal & black
        colorY1 = case_when(y1_bar < y1 ~ "#008080", TRUE ~ "black"), 
        # Bottom series should be magenta & black
        colorY2 = case_when(y2_bar < y2 ~ "#AA00AA", TRUE ~ "black")) 

e <- .data |>
    echarts4r::e_chart(x = x) |>
    echarts4r::e_scatter(serie = y1, size = sizeY1) |>
    # Teal (& black) colors correctly applied to y1 (top)
    echarts4r::e_add_nested("itemStyle", color = colorY1) |>
    echarts4r::e_scatter(serie = y2, size = sizeY2)

e |>
    # NOTE: itemStyle skipped when using the `.serie` parameter, leaving graph unchanged.
    # according to the code `.serie` appears to target the individual datapoint? Without this parameter the y1 series turns magenta.
    echarts4r::e_add_nested("itemStyle", color = colorY2, .serie = 2)

Created on 2023-09-23 with reprex v2.0.2

Expected behaviour


# Correct Now
e$x$opts$series[[2]]$data <- mapply(\(x, y) {x[["itemStyle"]] <- list(color = y); x},
                                    e$x$opts$series[[2]]$data, .data$colorY2, SIMPLIFY = FALSE)
e$x$opts$color <- c("#008080", "#AA00AA")

e

munoztd0 commented 9 months ago

Weird behaviour, on it.