From c8c23aa43b30d4ba47593204b6de9b88d2ff6489 Mon Sep 17 00:00:00 2001 From: pvictor Date: Tue, 3 Mar 2020 19:34:07 +0100 Subject: [PATCH] formating & examples --- R/apex-utils.R | 4 --- R/apexcharter.R | 50 ++++++++++++++++++++-------------- R/utils.R | 13 ++++++--- examples/ax_chart.R | 53 +++++++++++++++++++++++++++++++++--- examples/localization.R | 6 ++++- man/ax_chart.Rd | 59 +++++++++++++++++++++++++++++++++++++---- 6 files changed, 148 insertions(+), 37 deletions(-) diff --git a/R/apex-utils.R b/R/apex-utils.R index 2bb8c9d..cec565b 100644 --- a/R/apex-utils.R +++ b/R/apex-utils.R @@ -1,8 +1,4 @@ - - - - # ApexCharts API ---------------------------------------------------------- diff --git a/R/apexcharter.R b/R/apexcharter.R index b481a52..8fa1bf6 100644 --- a/R/apexcharter.R +++ b/R/apexcharter.R @@ -20,7 +20,7 @@ apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height if (isTRUE(auto_update)) { auto_update <- config_update() } - + x <- list( ax_opts = ax_opts, auto_update = auto_update @@ -34,24 +34,7 @@ apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height height = height, package = "apexcharter", elementId = elementId, - preRenderHook = function(widget) { - if (!is.null(widget$x$ax_opts$chart$defaultLocale)) { - defaultLocale <- widget$x$ax_opts$chart$defaultLocale - defaultLocale <- match.arg( - arg = defaultLocale, - choices = c("ca", "de", "el", "en", "es", "fi", "fr", "hi", "hr", "hy", - "id", "it", "ko", "nl", "pt-br", "ru", "se", "tr", "ua") - ) - if (!is.null(widget$x$ax_opts$chart$locales)) { - warning("defaultLocale is used but will be ignored since a custom array for locales is provided.") - } else { - path <- system.file(file.path("htmlwidgets/lib/apexcharts-locales", paste0(defaultLocale, ".json")), package = "apexcharter") - locale <- jsonlite::fromJSON(txt = path) - widget$x$ax_opts$chart$locales <- list(locale) - } - } - widget - }, + preRenderHook = add_locale, sizingPolicy = htmlwidgets::sizingPolicy( defaultWidth = "100%", defaultHeight = "100%", @@ -63,12 +46,39 @@ apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height browser.fill = TRUE, viewer.suppress = FALSE, browser.external = TRUE, - padding = 20 + padding = 0 ) ) } +add_locale <- function(widget) { + if (!is.null(widget$x$ax_opts$chart$defaultLocale)) { + defaultLocale <- widget$x$ax_opts$chart$defaultLocale + defaultLocale <- match.arg( + arg = defaultLocale, + choices = c("ca", "de", "el", "en", "es", "fi", "fr", "hi", "hr", "hy", + "id", "it", "ko", "nl", "pt-br", "ru", "se", "tr", "ua") + ) + if (!is.null(widget$x$ax_opts$chart$locales)) { + warning( + "defaultLocale is used but will be ignored since", + " a custom array for locales is provided." + ) + } else { + path <- system.file( + file.path("htmlwidgets/lib/apexcharts-locales", paste0(defaultLocale, ".json")), + package = "apexcharter" + ) + locale <- jsonlite::fromJSON(txt = path) + widget$x$ax_opts$chart$locales <- list(locale) + } + } + widget +} + + + #' Configuration for auto update #' diff --git a/R/utils.R b/R/utils.R index e17f5ff..7c05238 100644 --- a/R/utils.R +++ b/R/utils.R @@ -5,7 +5,6 @@ dropNulls <- function(x) { x[!vapply(x, is.null, FUN.VALUE = logical(1))] } - `%||%` <- function(x, y) { if (!is.null(x)) x else y } @@ -32,7 +31,11 @@ formatNoSci <- function(x) { if (is.null(ax$x$ax_opts[[name]])) { ax$x$ax_opts[[name]] <- list(...) } else { - ax$x$ax_opts[[name]] <- utils::modifyList(x = ax$x$ax_opts[[name]], val = list(...), keep.null = TRUE) + ax$x$ax_opts[[name]] <- utils::modifyList( + x = ax$x$ax_opts[[name]], + val = list(...), + keep.null = TRUE + ) } return(ax) @@ -52,7 +55,11 @@ formatNoSci <- function(x) { if (is.null(ax$x$ax_opts[[name]])) { ax$x$ax_opts[[name]] <- l } else { - ax$x$ax_opts[[name]] <- utils::modifyList(x = ax$x$ax_opts[[name]], val = l, keep.null = TRUE) + ax$x$ax_opts[[name]] <- utils::modifyList( + x = ax$x$ax_opts[[name]], + val = l, + keep.null = TRUE + ) } return(ax) diff --git a/examples/ax_chart.R b/examples/ax_chart.R index 6e70ccb..4834f28 100644 --- a/examples/ax_chart.R +++ b/examples/ax_chart.R @@ -1,13 +1,21 @@ library(dplyr) data("diamonds", package = "ggplot2") -# Stack bar type +## Stack bar type +# default is dodge +apex( + data = count(diamonds, cut, color), + mapping = aes(x = cut, y = n, fill = color) +) + +# stack apex( data = count(diamonds, cut, color), mapping = aes(x = cut, y = n, fill = color) ) %>% ax_chart(stacked = TRUE) +# stack filled apex( data = count(diamonds, cut, color), mapping = aes(x = cut, y = n, fill = color) @@ -15,12 +23,49 @@ apex( ax_chart(stacked = TRUE, stackType = "100%") -# Toolbar + + +# Toolbar -------------------------------------- + +# Hide the toolbar apex( data = count(diamonds, cut, color), mapping = aes(x = cut, y = n, fill = color) ) %>% ax_chart(toolbar = list(show = FALSE)) - - \ No newline at end of file +# Hide download buttons +data("economics", package = "ggplot2") +apex( + data = economics, + mapping = aes(x = date, y = pce), + type = "line" +) %>% + ax_chart( + toolbar = list(tools= list(download = FALSE)) + ) + + + +# Zoom ----------------------------------------- + +# Disable +apex( + data = economics, + mapping = aes(x = date, y = pce), + type = "line" +) %>% + ax_chart( + zoom = list(enabled = FALSE) + ) + + +# Auto-scale Y axis +apex( + data = economics, + mapping = aes(x = date, y = pce), + type = "line" +) %>% + ax_chart( + zoom = list(autoScaleYaxis = TRUE) + ) diff --git a/examples/localization.R b/examples/localization.R index a563504..7620351 100644 --- a/examples/localization.R +++ b/examples/localization.R @@ -1,3 +1,7 @@ + +# Localization --------------------------------- + + # Use included localization config dat <- data.frame( x = Sys.Date() + 1:20, @@ -18,7 +22,7 @@ apex(dat, aes(x, y), "line") %>% apex(dat, aes(x, y), "line") %>% ax_chart(locales = list( list( - name = "en", # override en locale + name = "en", # override 'en' locale options = list( toolbar = list( exportToSVG = "GET SVG", diff --git a/man/ax_chart.Rd b/man/ax_chart.Rd index 39704db..52292a4 100644 --- a/man/ax_chart.Rd +++ b/man/ax_chart.Rd @@ -82,13 +82,21 @@ Chart parameters library(dplyr) data("diamonds", package = "ggplot2") -# Stack bar type +## Stack bar type +# default is dodge +apex( + data = count(diamonds, cut, color), + mapping = aes(x = cut, y = n, fill = color) +) + +# stack apex( data = count(diamonds, cut, color), mapping = aes(x = cut, y = n, fill = color) ) \%>\% ax_chart(stacked = TRUE) +# stack filled apex( data = count(diamonds, cut, color), mapping = aes(x = cut, y = n, fill = color) @@ -96,15 +104,56 @@ apex( ax_chart(stacked = TRUE, stackType = "100\%") -# Toolbar + + +# Toolbar -------------------------------------- + +# Hide the toolbar apex( data = count(diamonds, cut, color), mapping = aes(x = cut, y = n, fill = color) ) \%>\% ax_chart(toolbar = list(show = FALSE)) - - +# Hide download buttons +data("economics", package = "ggplot2") +apex( + data = economics, + mapping = aes(x = date, y = pce), + type = "line" +) \%>\% + ax_chart( + toolbar = list(tools= list(download = FALSE)) + ) + + + +# Zoom ----------------------------------------- + +# Disable +apex( + data = economics, + mapping = aes(x = date, y = pce), + type = "line" +) \%>\% + ax_chart( + zoom = list(enabled = FALSE) + ) + + +# Auto-scale Y axis +apex( + data = economics, + mapping = aes(x = date, y = pce), + type = "line" +) \%>\% + ax_chart( + zoom = list(autoScaleYaxis = TRUE) + ) + +# Localization --------------------------------- + + # Use included localization config dat <- data.frame( x = Sys.Date() + 1:20, @@ -125,7 +174,7 @@ apex(dat, aes(x, y), "line") \%>\% apex(dat, aes(x, y), "line") \%>\% ax_chart(locales = list( list( - name = "en", # override en locale + name = "en", # override 'en' locale options = list( toolbar = list( exportToSVG = "GET SVG",