Skip to content

Commit

Permalink
added v_sunburst()
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 10, 2024
1 parent 212d2a8 commit cb16249
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export(v_specs_indicator)
export(v_specs_legend)
export(v_specs_player)
export(v_specs_tooltip)
export(v_sunburst)
export(v_theme)
export(v_theme_builtin)
export(v_treemap)
Expand Down
92 changes: 92 additions & 0 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1541,3 +1541,95 @@ v_waterfall <- function(vc,
vc <- v_scale_y_continuous(vc, zero = TRUE)
return(vc)
}







#' Create a Sunburst Chart
#'
#' @inheritParams v_bar
#' @param drill Drill-down function switch.
#' @param gap Layer gap, supports passing an array to configure layer gaps layer by layer.
#'
#' @return A [vchart()] `htmlwidget` object.
#' @export
#'
#' @example examples/v_sunburst.R
v_sunburst <- function(vc,
mapping = NULL,
data = NULL,
name = NULL,
drill = TRUE,
gap = 5,
...,
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, "sunburst")
if (is.null(name) & !is.null(mapping$y))
name <- rlang::as_label(mapping$y)
serie_id <- serie_id %||% genSerieId()
data_id <- data_id %||% genDataId()
lvl_vars <- grep(pattern = "lvl\\d*", x = names(mapping), value = TRUE)
lvl_vars <- sort(lvl_vars)
if (length(lvl_vars) > 1) {
values <- create_tree(
data = mapdata,
levels = lvl_vars,
value = "value"
)
} else {
values <- create_values(mapdata, .names = list(name = "x", value = "y"))
}
vc <- .vchart_specs(
vc, "data",
list(
list(
id = data_id,
values = values
)
)
)
serie <- list_(
type = "sunburst",
id = serie_id,
dataId = data_id,
name = name,
categoryField = "name",
valueField = "value",
drill = drill,
gap = gap,
...
)
vc <- .vchart_specs(vc, "series", list(serie))
if (has_player(mapdata)) {
if (length(lvl_vars) > 1) {
vc <- v_default_player(
vc,
mapdata,
data_id,
levels = lvl_vars,
value = "value"
)
} else {
vc <- v_default_player(
vc,
mapdata,
data_id,
fun_values = create_values,
.names = list(name = "x", value = "y")
)
}
}
return(vc)
}


58 changes: 58 additions & 0 deletions examples/v_sunburst.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

library(vchartr)

# Create a sunburst and auto hide labels
vchart(countries_gdp) %>%
v_sunburst(
aes(lvl1 = REGION_UN, lvl2 = SUBREGION, lvl3 = ADMIN, value = GDP_MD),
gap = 10,
labelAutoVisible = list(
enable = TRUE
),
labelLayout = list(
align = "center",
rotate = "radial"
)
)


# Custom tooltip
vchart(countries_gdp) %>%
v_sunburst(
aes(lvl1 = REGION_UN, lvl2 = SUBREGION, lvl3 = ADMIN, value = GDP_MD)
) %>%
v_specs_tooltip(
mark = list(
title = list(
value = JS("val => val?.datum?.map(data => data.name).join(' / ')")
)
)
)


# Custom layout options
vchart(countries_gdp) %>%
v_sunburst(
aes(lvl1 = REGION_UN, lvl2 = SUBREGION, lvl3 = ADMIN, value = GDP_MD),
gap = 0,
innerRadius = c(0, 0.4, 0.8),
outerRadius = c(0.3, 0.7, 0.85),
labelAutoVisible = list(
enable = TRUE,
circumference = 1
),
labelLayout = list(
list(
align = "center",
rotate = "tangential",
offset = 0
),
NULL,
list(
align = "start",
rotate = "radial",
offset = 15
)
)
) %>%
v_specs(padding = 70)
102 changes: 102 additions & 0 deletions man/v_sunburst.Rd

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

0 comments on commit cb16249

Please sign in to comment.