Skip to content

Commit

Permalink
added v_waterfall()
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 9, 2024
1 parent 83a80fa commit 212d2a8
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export(v_theme)
export(v_theme_builtin)
export(v_treemap)
export(v_venn)
export(v_waterfall)
export(v_wordcloud)
export(vars)
export(vchart)
Expand Down
64 changes: 64 additions & 0 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1477,3 +1477,67 @@ v_venn <- function(vc,
return(vc)
}




#' Create a Waterfall Chart
#'
#' @inheritParams v_bar
#'
#' @return A [vchart()] `htmlwidget` object.
#' @export
#'
#'
#' @example examples/v_waterfall.R
v_waterfall <- function(vc,
mapping = NULL,
data = NULL,
name = NULL,
...,
serie_id = NULL,
data_id = NULL) {
stopifnot(
"\'vc\' must be a chart constructed with vchart()" = inherits(vc, "vchart")
)
data <- get_data(vc, data)
mapping <- get_mapping(vc, mapping)
mapdata <- eval_mapping_(data, mapping)
vc$x$type <- c(vc$x$type, "waterfall")
serie_id <- serie_id %||% genSerieId()
data_id <- data_id %||% genDataId()
vc <- .vchart_specs(
vc, "data",
list(
list(
id = data_id,
values = mapdata
)
)
)
serie <- list_(
type = "waterfall",
id = serie_id,
dataId = data_id,
name = name,
xField = "x",
yField = "y",
total = list(
type = "field",
tagField = "total",
valueField = "y"
),
seriesField = if (has_name(mapping, "colour")) "colour",
...
)
vc <- .vchart_specs(vc, "series", list(serie))
scale_x <- attr(mapdata, "scale_x")
if (identical(scale_x, "discrete")) {
vc <- v_scale_x_discrete(vc)
} else if (identical(scale_x, "date")) {
vc <- v_scale_x_date(vc)
} else if (identical(scale_x, "continuous")) {
vc <- v_scale_x_continuous(vc)
}
vc <- v_scale_y_continuous(vc, zero = TRUE)
return(vc)
}
2 changes: 2 additions & 0 deletions R/scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ v_scale_x_continuous <- function(vc,
labels_tooltip = labels_tooltip,
min = min,
max = max,
zero = zero,
...
)
}
Expand Down Expand Up @@ -470,6 +471,7 @@ v_scale_y_continuous <- function(vc,
labels_tooltip = labels_tooltip,
min = min,
max = max,
zero = zero,
...
)
}
Expand Down
31 changes: 31 additions & 0 deletions examples/v_waterfall.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

library(vchartr)

balance <- data.frame(
desc = c("Starting Cash",
"Sales", "Refunds", "Payouts", "Court Losses",
"Court Wins", "Contracts", "End Cash"),
amount = c(2000, 3400, -1100, -100, -6600, 3800, 1400, 2800)
)

vchart(balance) %>%
v_waterfall(aes(x = desc, y = amount))


# With total values and formatting
data.frame(
x = c("Feb.4", "Feb.11", "Feb.20", "Feb.25", "Mar.4",
"Mar.11", "Mar.19", "Mar.26", "Apr.1", "Apr.8",
"Apr.15", "Apr.22", "Apr.29", "May.6", "total"),
total = c(TRUE, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, TRUE),
y = c(45L, -5L, 2L, -2L, 2L, 2L, -2L, 1L, 1L, 1L, 2L, 1L, -2L, -1L, NA)
) %>%
vchart() %>%
v_waterfall(
aes(x = x, y = y, total = total),
stackLabel = list(
valueType = "absolute",
formatMethod = JS("text => text + '%'")
)
) %>%
v_specs_legend(visible = TRUE)
69 changes: 69 additions & 0 deletions man/v_waterfall.Rd

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

0 comments on commit 212d2a8

Please sign in to comment.