Skip to content

Commit

Permalink
more examples + player fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 23, 2024
1 parent 2b88636 commit d934926
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 39 deletions.
24 changes: 6 additions & 18 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ v_bar <- function(vc,
data_id <- data_id %||% genDataId()
vc <- .vchart_specs(
vc, "data",
list(
list(
id = data_id,
values = mapdata
)
)
list(list(id = data_id, values = filter_player_value(mapdata)))
)
serie <- list_(
type = "bar",
Expand Down Expand Up @@ -462,12 +457,7 @@ v_scatter <- function(vc,
data_id <- data_id %||% genDataId()
vc <- .vchart_specs(
vc, "data",
list(
list(
id = data_id,
values = mapdata
)
)
list(list(id = data_id, values = filter_player_value(mapdata)))
)
shapeField <- NULL
if (has_name(mapping, "shape"))
Expand Down Expand Up @@ -527,6 +517,9 @@ v_scatter <- function(vc,
if (identical(scale_size, "continuous")) {
vc <- v_scale_size(vc)
}
if (has_player(mapdata)) {
vc <- v_default_player(vc, mapdata, data_id)
}
return(vc)
}

Expand Down Expand Up @@ -621,12 +614,7 @@ v_pie <- function(vc,
data_id <- data_id %||% genDataId()
vc <- .vchart_specs(
vc, "data",
list(
list(
id = data_id,
values = mapdata
)
)
list(list(id = data_id, values = filter_player_value(mapdata)))
)
serie <- list_(
type = "pie",
Expand Down
18 changes: 6 additions & 12 deletions R/specs.R
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,7 @@ v_specs_axes <- function(vc,
#' @return A [vchart()] `htmlwidget` object.
#' @export
#'
#' @examples
#' library(vchartr)
#' data("mpg", package = "ggplot2")
#'
#' vchart(table(Class = mpg$class, Year = mpg$year)) %>%
#' v_bar(aes(Class, Freq, fill = Year)) %>%
#' v_specs_tooltip(
#' visible = FALSE
#' )
#' @example examples/player.R
v_specs_player <- function(vc, ...) {
vc <- .vchart_specs(
vc,
Expand All @@ -355,8 +347,9 @@ v_specs_player <- function(vc, ...) {
v_default_player <- function(vc,
mapdata,
data_id,
fun_values = create_values,
fun_values = identity,
...) {
mapdata <- split(mapdata, as.character(mapdata$player))
v_specs_player(
vc,
auto = FALSE,
Expand All @@ -367,7 +360,7 @@ v_default_player <- function(vc,
position = "middle",
type = "discrete",
specs = lapply(
X = mapdata,
X = unname(mapdata),
FUN = function(dat) {
list(
data = list(
Expand All @@ -390,7 +383,8 @@ v_default_player <- function(vc,
#'
#' @return A [vchart()] `htmlwidget` object.
#' @export
#'
#'
#' @example examples/custom_mark.R
v_specs_custom_mark <- function(vc, ...) {
vc <- .vchart_specs2(
vc,
Expand Down
11 changes: 7 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ create_values <- function(data, .names = NULL) {
has_player <- function(x) {
isTRUE(attr(x, "player"))
}
filter_player_value <- function(mapdata) {
if (has_player(mapdata)) {
player1 <- mapdata$player[1]
mapdata <- mapdata[mapdata$player == player1, , drop = FALSE]
}
return(mapdata)
}

create_chart <- function(type,
specs,
Expand Down Expand Up @@ -168,10 +175,6 @@ eval_mapping_ <- function(data, mapping, na_rm = FALSE) {
mapdata$y <- as.numeric(mapdata$y)
}
if (has_name(mapdata, "player")) {
mapdata <- lapply(
X = unname(split(as.data.frame(mapdata), as.character(mapdata$player))),
FUN = as.list
)
attr(mapdata, "player") <- TRUE
}
attr(mapdata, "scale_x") <- scale_x
Expand Down
34 changes: 34 additions & 0 deletions examples/custom_mark.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

library(vchartr)

world_electricity %>%
subset(type == "detail") %>%
vchart() %>%
v_bar(
aes(source, generation, player = year),
direction = "h",
data_id = "mydata"
) %>%
v_specs_custom_mark(
type = "text",
dataId = "mydata",
style = list(
textBaseline = "bottom",
fontSize = 60,
textAlign = "right",
fontWeight = 700,
text = JS("datum => datum.player"),
x = JS(
"(datum, ctx) => {",
" return ctx.vchart.getChart().getCanvasRect().width - 50;",
"}"
),
y = JS(
"(datum, ctx) => {",
" return ctx.vchart.getChart().getCanvasRect().height - 150;",
"}"
),
fill = "grey",
fillOpacity = 0.5
)
)
11 changes: 11 additions & 0 deletions examples/player.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

library(vchartr)

world_electricity %>%
subset(type == "detail") %>%
vchart() %>%
v_bar(
aes(source, generation, player = year)
)


36 changes: 36 additions & 0 deletions man/v_specs_custom_mark.Rd

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

13 changes: 8 additions & 5 deletions man/v_specs_player.Rd

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

0 comments on commit d934926

Please sign in to comment.