helgasoft / echarty

Minimal R/Shiny Interface to ECharts.js
https://helgasoft.github.io/echarty/
87 stars 3 forks source link

Legend issue #31

Closed MrMisc closed 11 months ago

MrMisc commented 11 months ago

Hi,

As mentioned before in another issue regarding a different subject, I am having trouble with getting the legend manipulation to actually work. I checked on another computer this time to see if the issue was consistent on my side and it appears to be so. I manually downloaded the latest version from github on this computer since I have that freedom on this device.

I am not sure what I am doing wrong here. For example, for the following dataset

remotes::install_github('helgasoft/echarty',force = TRUE)  # get latest
library(echarty)
wahis<-read.csv("modifiedanthrax_WAHIS_dataset.csv")
data2<-wahis %>% mutate(Cases = as.integer(Cases),Deaths = as.integer(Deaths)) %>%
  filter(`Animal Category` == "Domestic" &
           Species %in% c("Cattle","Sheep","Sheep/goats (mixed herd)","Goats","Swine")) %>%
  group_by(Year,Sub_Continent, Country) %>%
  summarise(n = n(),Cases = sum(Cases), Deaths = sum(Deaths)) %>%
  ungroup()

setting <- list(show = T,type= "scroll",orient= "horizontal", pageButtonPosition= 'start',
                right= 5,top = 30, icon = 'circle', align= 'right', height='85%')

data2 |> group_by(Sub_Continent) |> ec.init(
  tooltip= list(show= TRUE),
  tl.series= list(encode = list(x = 'Year',y = 'n',emphasis = list(focus ="series")),type= 'scatter', groupBy= 'Country'),
  xAxis = list(max = 2023,min = 2009),
  legend = setting,
  # visualMap= list(dimension=2, inRange= list( symbolSize = c(35,5)))
)

rstudio_hjU2h2JYpQ

The legend does not appear to scroll. I appear to have this issue recurring for other echarty plots as well so far and I do not know what is the cause.

Additionally, I wonder if it is possible to make the legend be sensitive to the timeline, and only show the countries that are present within each frame of the timeline?

Any guidance on this issue would be greatly appreciated. Thank you for your patience and help so far.

helgasoft commented 11 months ago

The legend effect is a bug. Will be fixed in upcoming version. Here is a workaround using ec.upd() and a way to filter legends "within each frame of the timeline".

# gather countries by continent
tmp <- data2 |> group_by(Sub_Continent) |> group_split()
cns <- lapply(seq_along(tmp), \(i) { as.list(unique(tmp[[i]]$Country)) })

data2 |> group_by(Sub_Continent) |> ec.init(
  tooltip= list(show= TRUE),
  tl.series= list(encode = list(x = 'Year',y = 'n',emphasis = list(focus ="series")),type= 'scatter', groupBy= 'Country'),
  xAxis = list(max = 2023,min = 2009),
  #legend = setting,  # =bug
) |> ec.upd({ 
  legend <- setting   # general legend settings
  # options are auto-generated by ec.init when tl.series (timeline) is defined
  options <- lapply(seq_along(options), \(i) {  
    options[[i]]$legend$data <- cns[[i]]  # fine-tune legends: data by continent
    options[[i]] 
  })
})

image

helgasoft commented 11 months ago

The legend bug has been fixed. Latest version of echarty is available. Install: remotes::install_github('helgasoft/echarty',force = TRUE) Check: RStudio > Packages > click echarty > click DESCRIPTION > Date should be >= 2023-12-14 Updated legend code:

data2 |> group_by(Sub_Continent) |> ec.init(
  tooltip= list(show= TRUE),
  tl.series= list(encode = list(x = 'Year',y = 'n'), type= 'scatter', groupBy= 'Country'),
  xAxis = list(scale=T),
  legend= setting   # general legend settings
) |> ec.upd({ 
  options <- lapply(seq_along(options), \(i) {  
    options[[i]]$legend$data <- cns[[i]]  # fine-tune legends: data by continent
    options[[i]] 
  })
})

NB: 📌 please close issue if problem solved.

MrMisc commented 11 months ago

Thank you! I will check out this updated version in a future project. Thank you so much for your help!