dygraphs icon indicating copy to clipboard operation
dygraphs copied to clipboard

Vectorized version of dyEvent() and dyShading()

Open ThomasSiegmund opened this issue 10 years ago • 3 comments

Hi,

for dynamic datasets it would be nice to have vectorized versions of dyEvent and dyShading, e.g.

dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>%
  dyAxis("y", valueRange = c(0, 100)) %>%
  dyEvent(date = c("1950-6-30", "1965-2-09"), label = c("Korea", "Vietnam"), labelLoc = c("bottom", "bottom"))

Alternatively dyEvent could accept a data frame.

Thanks

Thomas

ThomasSiegmund avatar Jul 09 '15 08:07 ThomasSiegmund

Here is my workaround for dyShading - I think it would work similarly for dyEvent

vec_dyShading <- function(dy, from, to, color){

  # assuming that from, to, and color have all same length
  for (i in seq_along(from)){
    dy <- dyShading(dy, from = from[i], to = to[i], color = color[i])
  }

  dy
}

ijlyttle avatar Jul 27 '15 14:07 ijlyttle

Hi Ian,

thanks for the hint - I'm actually using a similar workaround now. It works for dyShading and for dyEvent. Nevertheless I think these kind of API is slightly broken if the first thing you need is a workaround.

ThomasSiegmund avatar Jul 27 '15 14:07 ThomasSiegmund

Here is my workaround for dyShading - I think it would work similarly for dyEvent

vec_dyShading <- function(dy, from, to, color){

  # assuming that from, to, and color have all same length
  for (i in seq_along(from)){
    dy <- dyShading(dy, from = from[i], to = to[i], color = color[i])
  }

  dy
}

I used the same kind of workaround but I find it to be extremely slow when adding a lot of shadings since it grows a list with a for loop. Instead I now use something like this:

vec_dyShading <- function(dyg, from, to, color) {
  
  # assuming that from, to, and color have all same length
  n <- length(from)
  if (n == 0) return(dyg)
  
  new_shades <- vector(mode = "list", length = n)
  for (i in 1:n) {
    new_shades[[i]] <- list(from = from[i],
                          to = to[i],
                          color = color[i],
                          axis = "x")
  }
  dyg$x$shadings <- c(dyg$x$shadings, new_shades)
  dyg
}

I would really be useful to let dyEvent() and dyShading() accept vectors.

JMPivette avatar Jan 06 '20 15:01 JMPivette