From 4ef564e60590f84c85854b6e419db1ba1a594bf0 Mon Sep 17 00:00:00 2001 From: pvictor Date: Thu, 1 Dec 2022 15:38:15 +0100 Subject: [PATCH] facets: added grid_width arg --- NAMESPACE | 2 ++ R/facets.R | 19 +++++++++++++++---- man/apex-facets.Rd | 10 +++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index add9c19..694a955 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -81,10 +81,12 @@ export(vars) importFrom(ggplot2,aes) importFrom(ggplot2,label_value) importFrom(ggplot2,vars) +importFrom(htmltools,css) importFrom(htmltools,renderTags) importFrom(htmltools,resolveDependencies) importFrom(htmltools,tagList) importFrom(htmltools,tags) +importFrom(htmltools,validateCssUnit) importFrom(htmlwidgets,JS) importFrom(htmlwidgets,JSEvals) importFrom(htmlwidgets,createWidget) diff --git a/R/facets.R b/R/facets.R index 09fa172..bb1bf94 100644 --- a/R/facets.R +++ b/R/facets.R @@ -221,7 +221,7 @@ build_facets <- function(chart) { # serie_name = chart$x$add_line$serie_name # ) } - new$height <- chart$x$facet$chart_height + new$height <- chart$height %||% chart$x$facet$chart_height new$x$facet <- NULL class(new) <- setdiff(class(new), "apex_facet") return(new) @@ -262,7 +262,8 @@ get_last_row <- function(mat) { #' @param scales Should scales be fixed (`"fixed"`, the default), #' free (`"free"`), or free in one dimension (`"free_x"`, `"free_y"`)? #' @param labeller A function with one argument containing for each facet the value of the faceting variable. -#' @param chart_height Individual chart height. +#' @param chart_height Individual chart height, ignored if an height is defined in `apex()` or `apexcharter()`. +#' @param grid_width Total width for the grid, regardless of the number of column. #' #' @return An [apexchart()] `htmlwidget` object with an additionnal class `"apex_facet"`. #' @@ -282,7 +283,8 @@ ax_facet_wrap <- function(ax, ncol = NULL, scales = c("fixed", "free", "free_y", "free_x"), labeller = label_value, - chart_height = "300px") { + chart_height = "300px", + grid_width = "100%") { if (!inherits(ax, "apex")) stop("ax_facet_wrap only works with charts generated with apex()", call. = FALSE) scales <- match.arg(scales) @@ -295,6 +297,7 @@ ax_facet_wrap <- function(ax, scales = scales, labeller = labeller, chart_height = chart_height, + grid_width = grid_width, type = "wrap" ) class(ax) <- c("apex_facet", class(ax)) @@ -314,7 +317,8 @@ ax_facet_grid <- function(ax, cols = NULL, scales = c("fixed", "free", "free_y", "free_x"), labeller = label_value, - chart_height = "300px") { + chart_height = "300px", + grid_width = "100%") { if (!inherits(ax, "apex")) stop("ax_facet_wrap only works with charts generated with apex()", call. = FALSE) scales <- match.arg(scales) @@ -330,6 +334,7 @@ ax_facet_grid <- function(ax, scales = scales, labeller = labeller, chart_height = chart_height, + grid_width = grid_width, type = "grid" ) class(ax) <- c("apex_facet", class(ax)) @@ -343,6 +348,7 @@ ax_facet_grid <- function(ax, # Tag --------------------------------------------------------------------- #' @importFrom rlang %||% +#' @importFrom htmltools tags css validateCssUnit build_facet_tag <- function(x) { facets <- build_facets(x) content <- facets$facets @@ -469,6 +475,11 @@ build_facet_tag <- function(x) { TAG ) } + TAG <- tags$div( + style = css(width = validateCssUnit(x$x$facet$grid_width)), + class = "apexcharter-facet", + TAG + ) return(TAG) } diff --git a/man/apex-facets.Rd b/man/apex-facets.Rd index 93a69af..9fd325d 100644 --- a/man/apex-facets.Rd +++ b/man/apex-facets.Rd @@ -13,7 +13,8 @@ ax_facet_wrap( ncol = NULL, scales = c("fixed", "free", "free_y", "free_x"), labeller = label_value, - chart_height = "300px" + chart_height = "300px", + grid_width = "100\%" ) ax_facet_grid( @@ -22,7 +23,8 @@ ax_facet_grid( cols = NULL, scales = c("fixed", "free", "free_y", "free_x"), labeller = label_value, - chart_height = "300px" + chart_height = "300px", + grid_width = "100\%" ) } \arguments{ @@ -37,7 +39,9 @@ free (\code{"free"}), or free in one dimension (\code{"free_x"}, \code{"free_y"} \item{labeller}{A function with one argument containing for each facet the value of the faceting variable.} -\item{chart_height}{Individual chart height.} +\item{chart_height}{Individual chart height, ignored if an height is defined in \code{apex()} or \code{apexcharter()}.} + +\item{grid_width}{Total width for the grid, regardless of the number of column.} \item{rows, cols}{A set of variables or expressions quoted by \code{vars()} and defining faceting groups on the rows or columns dimension.}