File: annotation.R

package info (click to toggle)
r-cran-dygraphs 1.1.1.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,228 kB
  • sloc: sh: 19; makefile: 15
file content (104 lines) | stat: -rw-r--r-- 4,112 bytes parent folder | download | duplicates (3)
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

#' Annotation for dygraph chart
#' 
#' Define a text annotation for a data-point on a dygraph chart.
#' 
#' @param dygraph Dygraph to add an annotation to
#' @param x Either numeric or date value indicating where to place the
#'   annotation. For date value, this should be of class \code{POSIXct} or
#'   convertible to \code{POSIXct}.
#' @param text Text to overlay on the chart at the location of x
#' @param tooltip Additional tooltip text to display on mouse hover
#' @param width Width (in pixels) of the annotation flag.
#' @param height Height (in pixels) of the annotation flag.
#' @param cssClass CSS class to use for styling the annotation.
#' @param tickHeight Height of the tick mark (in pixels) connecting the point to
#'   its flag or icon.
#' @param attachAtBottom If true, attach annotations to the x-axis, rather than 
#'   to actual points.
#' @param clickHandler JavaScript function to call when an annotation is 
#'   clicked.
#' @param mouseOverHandler JavaScript function to call when the mouse hovers 
#'   over an annotation.
#' @param mouseOutHandler JavaScript function to call when the mouse exits an 
#'   annotation.
#' @param dblClickHandler JavaScript function to call when an annotation is 
#'   double clicked.
#' @param series Series to attach the annotation to. By default, the last series
#'   defined using \code{\link{dySeries}}.
#'   
#' @note Annotations are bound to specific series in the input data. If you have
#'   only one series or if you define annotations immediately after a call to 
#'   \code{\link{dySeries}} then you need not specify the series explicitly. 
#'   Otherwise, you should use the \code{series} parameter to indicate which
#'   series the annotation should be bound to.
#'   
#'   Annotation event handlers can also specified globally (see 
#'   \code{\link{dyCallbacks}}).
#'   
#' @note See the
#'   \href{https://rstudio.github.io/dygraphs/gallery-annotations.html}{online
#'   documentation} for additional details and examples.
#'   
#' @return Dygraph with specified annotation
#' 
#' @examples 
#' library(dygraphs)
#'   
#' dygraph(presidents, main = "Presidential Approval") %>%
#'   dyAxis("y", valueRange = c(0, 100)) %>%
#'   dyAnnotation("1950-7-1", text = "A", tooltip = "Korea") %>%
#'   dyAnnotation("1965-1-1", text = "B", tooltip = "Vietnam")   
#'   
#' @export
dyAnnotation <- function(dygraph,
                         x,
                         text, 
                         tooltip = NULL,
                         width = NULL,
                         height = NULL,
                         cssClass = NULL,
                         tickHeight = NULL,
                         attachAtBottom = FALSE,
                         clickHandler = NULL,
                         mouseOverHandler = NULL,
                         mouseOutHandler = NULL,
                         dblClickHandler = NULL,
                         series = NULL) {
  
  # convert x to date format then to a suitable time value if it is date format
  if (dygraph$x$format == "date")
    x <- asISO8601Time(x)
  
  # validate series if specified
  if (!is.null(series) && ! series %in% dygraph$x$attrs$labels) {
    stop("The specified series was not found. Valid series names are: ", 
         paste(dygraph$x$attrs$labels[-1], collapse = ", "))
  }
  
  # default the series if necessary
  if (is.null(series))
    series <- dygraph$x$attrs$labels[length(dygraph$x$attrs$labels)]
  
  # create annotation
  annotation <- list()
  annotation$x <- x
  annotation$shortText <- text
  annotation$text <- tooltip
  annotation$width <- width
  annotation$height <- height
  annotation$cssClass <- cssClass
  annotation$tickHeight <- tickHeight
  annotation$attachAtBottom <- attachAtBottom
  annotation$clickHandler <- JS(clickHandler)
  annotation$mouseOverHandler <- JS(mouseOverHandler)
  annotation$mouseOutHandler <- JS(mouseOutHandler)
  annotation$dblClickHandler <- JS(dblClickHandler)
  annotation$series <- series
  
  # add it
  dygraph$x$annotations[[length(dygraph$x$annotations) + 1]] <- annotation

  # return the dygraph
  dygraph
}