From d44e251f312162cc2f477c046d95f1fa3ab8e536 Mon Sep 17 00:00:00 2001 From: pvictor Date: Wed, 12 Feb 2020 11:57:24 +0100 Subject: [PATCH] added update_options in apexcharter() --- NEWS.md | 7 +++++++ R/apex.R | 28 ++++++++++++++++++++-------- R/apexcharter.R | 21 ++++++++++++--------- inst/htmlwidgets/apexcharter.js | 7 +++++-- man/apex.Rd | 19 ++++++++++++++----- man/apexchart.Rd | 11 ++++++++--- 6 files changed, 66 insertions(+), 27 deletions(-) diff --git a/NEWS.md b/NEWS.md index 198e33d..3a4a12d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +apexcharter 0.1.4 +================== + +* Upgraded ApexCharts.js to 3.15.5 +* Fixed a bug in grouped bar charts with different levels in groups. + + apexcharter 0.1.3 ================== diff --git a/R/apex.R b/R/apex.R index b07a5e3..bd846b3 100644 --- a/R/apex.R +++ b/R/apex.R @@ -1,14 +1,21 @@ #' @title Quick ApexChart #' -#' @description Initialize a chart with three main parameters : data, mapping and type of chart. +#' @description Initialize a chart with three main parameters : +#' data, mapping and type of chart. #' -#' @param data Default dataset to use for chart. If not already a \code{data.frame}, it will be coerced to with \code{as.data.frame}. +#' @param data Default dataset to use for chart. If not already +#' a \code{data.frame}, it will be coerced to with \code{as.data.frame}. #' @param mapping Default list of aesthetic mappings to use for chart -#' @param type Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"}, -#' \code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"heatmap"}. +#' @param type Specify the chart type. Available Options: +#' \code{"column"}, \code{"bar"}, \code{"line"}, +#' \code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, +#' \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"heatmap"}. #' @param ... Other arguments passed on to methods. Not currently used. -#' @param auto_update In Shiny application, update existing chart rather than generating new one. +#' @param auto_update In Shiny application, update existing chart +#' rather than generating new one. +#' @param update_options In Shiny application, update or not global options +#' for chart. Applicable only if \code{auto_update} is \code{TRUE}. #' @param width A numeric input in pixels. #' @param height A numeric input in pixels. #' @param elementId Use an explicit element ID for the widget. @@ -21,7 +28,9 @@ #' @importFrom utils modifyList #' #' @example examples/apex.R -apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width = NULL, height = NULL, elementId = NULL) { +apex <- function(data, mapping, type = "column", ..., + auto_update = TRUE, update_options = FALSE, + width = NULL, height = NULL, elementId = NULL) { type <- match.arg(type, c("column", "bar", "line", "area", "spline", "area-spline", "pie", "donut", "radialBar", "radar", "scatter", "heatmap", "rangeBar")) @@ -44,8 +53,11 @@ apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width } opts <- modifyList(opts, choose_config(type, mapdata)) apexchart( - ax_opts = opts, width = width, height = height, - elementId = elementId, auto_update = auto_update + ax_opts = opts, + width = width, height = height, + elementId = elementId, + auto_update = auto_update, + update_options = update_options ) } diff --git a/R/apexcharter.R b/R/apexcharter.R index ca130b3..2c8f506 100644 --- a/R/apexcharter.R +++ b/R/apexcharter.R @@ -1,8 +1,11 @@ -#' Create a apexcharts.js widget +#' Create an apexcharts.js widget #' #' @param ax_opts A \code{list} in JSON format with chart parameters. -#' @param auto_update In Shiny application, update existing chart rather than generating new one. +#' @param auto_update In Shiny application, update existing chart +#' rather than generating new one. +#' @param update_options In Shiny application, update or not global options +#' for chart. Applicable only if \code{auto_update} is \code{TRUE}. #' @param width A numeric input in pixels. #' @param height A numeric input in pixels. #' @param elementId Use an explicit element ID for the widget. @@ -13,21 +16,21 @@ #' @importFrom htmlwidgets createWidget sizingPolicy #' #' @example examples/apexchart.R -apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height = NULL, elementId = NULL) { +apexchart <- function(ax_opts = list(), auto_update = TRUE, update_options = FALSE, width = NULL, height = NULL, elementId = NULL) { - # forward options using x x <- list( ax_opts = ax_opts, - auto_update = auto_update + auto_update = isTRUE(auto_update), + update_options = isTRUE(update_options) ) # create widget htmlwidgets::createWidget( - name = 'apexcharter', + name = "apexcharter", x = x, width = width, height = height, - package = 'apexcharter', + package = "apexcharter", elementId = elementId, preRenderHook = function(widget) { if (!is.null(widget$x$ax_opts$chart$defaultLocale)) { @@ -86,8 +89,8 @@ apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height #' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget #' #' @example examples/apexcharter-shiny.R -apexchartOutput <- function(outputId, width = '100%', height = '400px'){ - htmlwidgets::shinyWidgetOutput(outputId, 'apexcharter', width, height, package = 'apexcharter') +apexchartOutput <- function(outputId, width = "100%", height = "400px"){ + htmlwidgets::shinyWidgetOutput(outputId, "apexcharter", width, height, package = "apexcharter") } #' @rdname apexcharter-shiny diff --git a/inst/htmlwidgets/apexcharter.js b/inst/htmlwidgets/apexcharter.js index 6f5171f..ced32a0 100644 --- a/inst/htmlwidgets/apexcharter.js +++ b/inst/htmlwidgets/apexcharter.js @@ -29,14 +29,17 @@ HTMLWidgets.widget({ // Generate or update chart if (apexchart === null) { - apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts); + apexchart = new ApexCharts(el, ax_opts); apexchart.render(); } else { if (x.auto_update) { apexchart.updateSeries(ax_opts.series); + if (x.update_options) { + apexchart.updateOptions(ax_opts, true); + } } else { apexchart.destroy(); - apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts); + apexchart = new ApexCharts(el, ax_opts); apexchart.render(); } } diff --git a/man/apex.Rd b/man/apex.Rd index ef07546..c7d2680 100644 --- a/man/apex.Rd +++ b/man/apex.Rd @@ -10,22 +10,30 @@ apex( type = "column", ..., auto_update = TRUE, + update_options = FALSE, width = NULL, height = NULL, elementId = NULL ) } \arguments{ -\item{data}{Default dataset to use for chart. If not already a \code{data.frame}, it will be coerced to with \code{as.data.frame}.} +\item{data}{Default dataset to use for chart. If not already +a \code{data.frame}, it will be coerced to with \code{as.data.frame}.} \item{mapping}{Default list of aesthetic mappings to use for chart} -\item{type}{Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"}, -\code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"heatmap"}.} +\item{type}{Specify the chart type. Available Options: +\code{"column"}, \code{"bar"}, \code{"line"}, +\code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, +\code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"heatmap"}.} \item{...}{Other arguments passed on to methods. Not currently used.} -\item{auto_update}{In Shiny application, update existing chart rather than generating new one.} +\item{auto_update}{In Shiny application, update existing chart +rather than generating new one.} + +\item{update_options}{In Shiny application, update or not global options +for chart. Applicable only if \code{auto_update} is \code{TRUE}.} \item{width}{A numeric input in pixels.} @@ -37,7 +45,8 @@ apex( A \code{apexcharts} \code{htmlwidget} object. } \description{ -Initialize a chart with three main parameters : data, mapping and type of chart. +Initialize a chart with three main parameters : + data, mapping and type of chart. } \examples{ library(ggplot2) diff --git a/man/apexchart.Rd b/man/apexchart.Rd index 0c908a0..dbc1510 100644 --- a/man/apexchart.Rd +++ b/man/apexchart.Rd @@ -2,11 +2,12 @@ % Please edit documentation in R/apexcharter.R \name{apexchart} \alias{apexchart} -\title{Create a apexcharts.js widget} +\title{Create an apexcharts.js widget} \usage{ apexchart( ax_opts = list(), auto_update = TRUE, + update_options = FALSE, width = NULL, height = NULL, elementId = NULL @@ -15,7 +16,11 @@ apexchart( \arguments{ \item{ax_opts}{A \code{list} in JSON format with chart parameters.} -\item{auto_update}{In Shiny application, update existing chart rather than generating new one.} +\item{auto_update}{In Shiny application, update existing chart +rather than generating new one.} + +\item{update_options}{In Shiny application, update or not global options +for chart. Applicable only if \code{auto_update} is \code{TRUE}.} \item{width}{A numeric input in pixels.} @@ -27,7 +32,7 @@ apexchart( A \code{apexcharts} \code{htmlwidget} object. } \description{ -Create a apexcharts.js widget +Create an apexcharts.js widget } \examples{ library(apexcharter)