/
callbacks.R
76 lines (65 loc) 路 1.5 KB
/
callbacks.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
#' Callbacks
#'
#' Binds events to chart interactions.
#'
#' @inheritParams e_bar
#' @param query Condistion that triggers the handler
#' @param handler JavaSscript handler, passed to \link[htmlwidgets]{JS}.
#' @param event Event that triggers the handler.
#'
#' @examples
#' cars |>
#' e_charts(speed) |>
#' e_scatter(dist) |>
#' e_on(
#' list(seriesName = "dist"),
#' "function(){alert('Serie clicked')}"
#' )
#' @seealso \href{https://echarts.apache.org/en/api.html#echartsInstance.on}{official documentation}
#'
#' @name callbacks
#' @export
e_on <- function(e, query, handler, event = "click") {
if (missing(query) || missing(handler)) {
stop("must pass query and handler", call. = FALSE)
}
if (!length(e$x$on)) {
e$x$on <- list()
}
opts <- list(
event = event,
query = query,
handler = htmlwidgets::JS(handler)
)
e$x$on <- append(e$x$on, list(opts))
e
}
#' @rdname callbacks
#' @export
e_off <- function(e, query, handler, event = "click") {
if (missing(query) || missing(handler)) {
stop("must pass query and handler", call. = FALSE)
}
if (!length(e$x$off)) {
e$x$off <- list()
}
opts <- list(
event = event,
query = query,
handler = htmlwidgets::JS(handler)
)
e$x$off <- append(e$x$off, list(opts))
e
}
#' Blank Area
#'
#' Use this function to capture a click on a
#' blank area of the canvas.
#' Note that this may stops other "click" events
#' from working.
#'
#' @export
e_get_zr <- function(){
e$x$zr <- TRUE
e
}