Skip to content

Commit

Permalink
edded v_event()
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Oct 23, 2024
1 parent 81d4574 commit 54f5137
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: vchartr
Title: Interactive Charts with the JavaScript 'VChart' Library
Version: 0.1.1
Version: 0.1.2
Authors@R: c(
person("Victor", "Perrier", email = "[email protected]", role = c("aut", "cre")),
person("Fanny", "Meyer", role = "aut"))
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(v_area)
export(v_bar)
export(v_boxplot)
export(v_circlepacking)
export(v_event)
export(v_facet_wrap)
export(v_gauge)
export(v_heatmap)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# vchartr 0.1.2

* Added `v_event()` to register events on chart.

# vchartr 0.1.1

* Updated @visactor/vchart to 1.12.8 (https://github.com/VisActor/VChart/releases).
Expand Down
40 changes: 40 additions & 0 deletions R/events.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#' VChart events
#'
#' @param vc A chart initialized with [vchart()].
#' @param name Name of the event, e.g. `"click"`.
#' @param params Parameters to specifically monitor events in a certain part of the chart.
#' @param fun JavaScript function executed when the event occurs.
#' @param ... Not used.
#'
#' @return A [vchart()] `htmlwidget` object.
#' @export
#'
#' @example examples/events.R
v_event <- function(vc,
name,
params,
fun,
...) {
if (is.null(vc$x$events)) {
vc$x$events <- list(
list(
name = name,
params = params,
fun = fun
)
)
} else {
vc$x$events <- append(
vc$x$events,
list(
list(
name = name,
params = params,
fun = fun
)
)
)
}
return(vc)
}
2 changes: 0 additions & 2 deletions R/select.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@



v_default_select <- function(vc,
mapdata,
data_id,
fun_values = identity,
...) {
mapdata <- split(mapdata, as.character(mapdata$select))
vc$x$select <- list(
Expand Down
16 changes: 16 additions & 0 deletions examples/events.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

library(vchartr)

vchart(top_generation) %>%
v_bar(aes(country, electricity_generation)) %>%
v_event(
name = "click",
params = list(level = "mark", type = "bar"),
fun = JS(
"e => {",
" console.log(e);",
" alert(e.datum.x);",
"}"
)
)

2 changes: 1 addition & 1 deletion inst/htmlwidgets/vchart.js

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions man/v_event.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions srcjs/widgets/vchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ HTMLWidgets.widget({
//vchart.setCurrentTheme(x.theme);
}

if (x.hasOwnProperty("events")) {
for (let i = 0; i < x.events.length; i++) {
vchart.on(x.events[i].name, x.events[i].params, x.events[i].fun);
}
}

if (x.hasOwnProperty("select")) {
var selectConfig = x.select.config;
selectConfig.select = "#" + el.id + "_select";
Expand Down

0 comments on commit 54f5137

Please sign in to comment.