dyplot.default.R 2.25 KB
Newer Older
unknown's avatar
unknown committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
dyplot.default <- function(x, col.Precip = "royalblue", col.Q = c("black", "red"),
                           xlab = "Time", ylab = c("Precipitation [mm]", "Flow [mm]"), main = NULL,
                           RangeSelector = TRUE, dyLegendShow = c("follow", "auto", "always", "onmouseover", "never"), ...) {

  if (! any(class(x) %in% c("ObsGR", "CalGR", "SimGR"))) {
    stop("Non convenient data for x argument. Must be of class \"ObsGR\" \"CalGR\", or \"SimGR\"")
  }
  
  if (any(class(x) %in% "ObsGR")) {
    data <- data.frame(DatesR = x$InputsModel$DatesR,
                       Precip = x$InputsModel$Precip,
                       Qobs   = x$Qobs)
  } else {
    data <- data.frame(DatesR = x$OutputsModel$DatesR,
                       Precip = x$OutputsModel$Precip,
                       Qobs   = x$Qobs,
                       Qsim   = x$OutputsModel$Qsim)
    if (length(col.Q) < 2) {
      col.Q <- rep(col.Q, 2)
    }
  }
  data.xts <- xts(data[, -1L], order.by = data$DatesR)
  
  graphOut <- dygraph(data.xts, main = main)
  graphOut <- dyAxis(dygraph = graphOut, name = "y", label = "Flow",
                     valueRange = range(data.xts[, grep("^Q", colnames(data.xts))], na.rm = TRUE) * c(0.01, 1.59))
  graphOut <- dyAxis(dygraph = graphOut, name = "y2", label = "Precip.", independentTicks = FALSE,
                     valueRange = rev(range(data.xts[, "Precip"], na.rm = TRUE) * c(0.01, 2.99)))
  graphOut <- dySeries(dygraph = graphOut, "Qobs"  , axis = 'y' , color = col.Q[seq_along(grep("^Q", colnames(data.xts)))])
  graphOut <- dySeries(dygraph = graphOut, "Precip", axis = 'y2', stepPlot = TRUE, fillGraph = TRUE, color = col.Precip)
  # if (ModelPeriod) {
  #   graphOut <- dyShading(dygraph = graphOut, from = x$PeriodModel$WarmUp[1L], to = x$PeriodModel$WarmUp[2L], color = "red")
  #   graphOut <- dyShading(dygraph = graphOut, from = x$PeriodModel$Run[1L]   , to = x$PeriodModel$Run[2L]   , color = "blue")
  # }
  if (RangeSelector) {
    graphOut <- dyRangeSelector(dygraph = graphOut, height = 15)
  }
  if (any(dyLegendShow %in% c("follow", "auto", "always", "onmouseover", "never"))) {
    graphOut <-  dyLegend(dygraph = graphOut, show = dyLegendShow[1L])
  }  
  graphOut <- dyOptions(dygraph = graphOut, useDataTimezone = TRUE)
  return(graphOut)
  
}