-
Notifications
You must be signed in to change notification settings - Fork 194
/
event.R
121 lines (113 loc) · 4.04 KB
/
event.R
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#' dygraph event line
#'
#' Add a vertical event line to a dygraph
#'
#' @param dygraph Dygraph to add event line to
#' @param x Either numeric or date/time for the event, depending on the format
#' of the x-axis of the dygraph. (For date/time must be a \code{POSIXct}
#' object or another object convertible to \code{POSIXct} via
#' \code{as.POSIXct})
#' @param label Label for event. Defaults to blank.
#' @param labelLoc Location for label (top or bottom)
#' @param color Color of event line. This can be of the form "#AABBCC" or
#' "rgb(255,100,200)" or "yellow". Defaults to black.
#' @param strokePattern A predefined stroke pattern type ("dotted", "dashed",
#' "dotdash", or "solid") or a custom pattern array where the even index is
#' a draw and odd is a space in pixels. Defaults to dashed.
#' @param date (deprecated) See argument \code{x}.
#'
#' @return A dygraph with the specified event line.
#'
#' @note See the \href{http://rstudio.github.io/dygraphs/gallery-event-lines.html}{online
#' documentation} for additional details and examples.
#'
#' @examples
#' library(dygraphs)
#'
#' dygraph(presidents, main = "Presidential Approval") %>%
#' dyAxis("y", valueRange = c(0, 100)) %>%
#' dyEvent("1950-6-30", "Korea", labelLoc = "bottom") %>%
#' dyEvent("1965-2-09", "Vietnam", labelLoc = "bottom")
#'
#' @export
dyEvent <- function(dygraph,
x,
label = NULL,
labelLoc = c("top", "bottom"),
color = "black",
strokePattern = "dashed",
date) {
# Check usage of deprecated argument 'date'
if (!missing(date)) {
x <- date
warning("Argument 'date' is deprecated, please use argument 'x' instead")
}
# create events
if (!is.null(label) && length(x) != length(label))
stop("Length of 'x' and 'label' does not match")
events <-
lapply(
seq_along(x), function(i)
{
list(
pos = ifelse(dygraph$x$format == "date", asISO8601Time(x[i]), x[i])
,label = ifelse(is.null(label),NULL, label[i] )
,labelLoc = match.arg(labelLoc, c("top", "bottom"))
,color = color
,strokePattern = resolveStrokePattern(strokePattern)
,axis = "x"
)
}
)
# add it to list of events
dygraph$x$events <- c(dygraph$x$events, events)
# return modified dygraph
dygraph
}
#' dygraph limit line
#'
#' Add a horizontal limit line to a dygraph
#'
#' @param dygraph Dygraph to add limit line to
#' @param limit Numeric position of limit.
#' @param label Label for limit. Defaults to blank.
#' @param labelLoc Location for label (left or right).
#' @param color Color of limit line. This can be of the form "#AABBCC" or
#' "rgb(255,100,200)" or "yellow". Defaults to black.
#' @param strokePattern A predefined stroke pattern type ("dotted", "dashed",
#' "dotdash", or "solid") or a custom pattern array where the even index is
#' a draw and odd is a space in pixels. Defaults to dashed.
#'
#' @return A dygraph with the specified limit line.
#'
#' @note See the \href{http://rstudio.github.io/dygraphs/gallery-event-lines.html}{online
#' documentation} for additional details and examples.
#'
#' @examples
#' library(dygraphs)
#'
#' dygraph(presidents, main = "Presidential Approval") %>%
#' dyAxis("y", valueRange = c(0, 100)) %>%
#' dyLimit(max(presidents, na.rm = TRUE), "Max",
#' strokePattern = "solid", color = "blue")
#'
#' @export
dyLimit <- function(dygraph,
limit,
label = NULL,
labelLoc = c("left", "right"),
color = "black",
strokePattern = "dashed") {
# create event
event <- list()
event$pos <- limit
event$label <- label
event$labelLoc <- match.arg(labelLoc)
event$color <- color
event$strokePattern <- resolveStrokePattern(strokePattern)
event$axis <- "y"
# add it to list of events
dygraph$x$events[[length(dygraph$x$events) + 1]] <- event
# return modified dygraph
dygraph
}