added update_options in apexcharter()

This commit is contained in:
pvictor 2020-02-12 11:57:24 +01:00
parent 2be3c5b4b3
commit d44e251f31
6 changed files with 66 additions and 27 deletions

View File

@ -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 apexcharter 0.1.3
================== ==================

View File

@ -1,14 +1,21 @@
#' @title Quick ApexChart #' @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 mapping Default list of aesthetic mappings to use for chart
#' @param type Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"}, #' @param type Specify the chart type. Available Options:
#' \code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"heatmap"}. #' \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 ... 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 width A numeric input in pixels.
#' @param height A numeric input in pixels. #' @param height A numeric input in pixels.
#' @param elementId Use an explicit element ID for the widget. #' @param elementId Use an explicit element ID for the widget.
@ -21,7 +28,9 @@
#' @importFrom utils modifyList #' @importFrom utils modifyList
#' #'
#' @example examples/apex.R #' @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", type <- match.arg(type, c("column", "bar", "line", "area", "spline", "area-spline",
"pie", "donut", "radialBar", "radar", "scatter", "heatmap", "pie", "donut", "radialBar", "radar", "scatter", "heatmap",
"rangeBar")) "rangeBar"))
@ -44,8 +53,11 @@ apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width
} }
opts <- modifyList(opts, choose_config(type, mapdata)) opts <- modifyList(opts, choose_config(type, mapdata))
apexchart( apexchart(
ax_opts = opts, width = width, height = height, ax_opts = opts,
elementId = elementId, auto_update = auto_update width = width, height = height,
elementId = elementId,
auto_update = auto_update,
update_options = update_options
) )
} }

View File

@ -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 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 width A numeric input in pixels.
#' @param height A numeric input in pixels. #' @param height A numeric input in pixels.
#' @param elementId Use an explicit element ID for the widget. #' @param elementId Use an explicit element ID for the widget.
@ -13,21 +16,21 @@
#' @importFrom htmlwidgets createWidget sizingPolicy #' @importFrom htmlwidgets createWidget sizingPolicy
#' #'
#' @example examples/apexchart.R #' @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( x <- list(
ax_opts = ax_opts, ax_opts = ax_opts,
auto_update = auto_update auto_update = isTRUE(auto_update),
update_options = isTRUE(update_options)
) )
# create widget # create widget
htmlwidgets::createWidget( htmlwidgets::createWidget(
name = 'apexcharter', name = "apexcharter",
x = x, x = x,
width = width, width = width,
height = height, height = height,
package = 'apexcharter', package = "apexcharter",
elementId = elementId, elementId = elementId,
preRenderHook = function(widget) { preRenderHook = function(widget) {
if (!is.null(widget$x$ax_opts$chart$defaultLocale)) { 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 #' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget
#' #'
#' @example examples/apexcharter-shiny.R #' @example examples/apexcharter-shiny.R
apexchartOutput <- function(outputId, width = '100%', height = '400px'){ apexchartOutput <- function(outputId, width = "100%", height = "400px"){
htmlwidgets::shinyWidgetOutput(outputId, 'apexcharter', width, height, package = 'apexcharter') htmlwidgets::shinyWidgetOutput(outputId, "apexcharter", width, height, package = "apexcharter")
} }
#' @rdname apexcharter-shiny #' @rdname apexcharter-shiny

View File

@ -29,14 +29,17 @@ HTMLWidgets.widget({
// Generate or update chart // Generate or update chart
if (apexchart === null) { if (apexchart === null) {
apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts); apexchart = new ApexCharts(el, ax_opts);
apexchart.render(); apexchart.render();
} else { } else {
if (x.auto_update) { if (x.auto_update) {
apexchart.updateSeries(ax_opts.series); apexchart.updateSeries(ax_opts.series);
if (x.update_options) {
apexchart.updateOptions(ax_opts, true);
}
} else { } else {
apexchart.destroy(); apexchart.destroy();
apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts); apexchart = new ApexCharts(el, ax_opts);
apexchart.render(); apexchart.render();
} }
} }

View File

@ -10,22 +10,30 @@ apex(
type = "column", type = "column",
..., ...,
auto_update = TRUE, auto_update = TRUE,
update_options = FALSE,
width = NULL, width = NULL,
height = NULL, height = NULL,
elementId = NULL elementId = NULL
) )
} }
\arguments{ \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{mapping}{Default list of aesthetic mappings to use for chart}
\item{type}{Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"}, \item{type}{Specify the chart type. Available Options:
\code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"heatmap"}.} \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{...}{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.} \item{width}{A numeric input in pixels.}
@ -37,7 +45,8 @@ apex(
A \code{apexcharts} \code{htmlwidget} object. A \code{apexcharts} \code{htmlwidget} object.
} }
\description{ \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{ \examples{
library(ggplot2) library(ggplot2)

View File

@ -2,11 +2,12 @@
% Please edit documentation in R/apexcharter.R % Please edit documentation in R/apexcharter.R
\name{apexchart} \name{apexchart}
\alias{apexchart} \alias{apexchart}
\title{Create a apexcharts.js widget} \title{Create an apexcharts.js widget}
\usage{ \usage{
apexchart( apexchart(
ax_opts = list(), ax_opts = list(),
auto_update = TRUE, auto_update = TRUE,
update_options = FALSE,
width = NULL, width = NULL,
height = NULL, height = NULL,
elementId = NULL elementId = NULL
@ -15,7 +16,11 @@ apexchart(
\arguments{ \arguments{
\item{ax_opts}{A \code{list} in JSON format with chart parameters.} \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.} \item{width}{A numeric input in pixels.}
@ -27,7 +32,7 @@ apexchart(
A \code{apexcharts} \code{htmlwidget} object. A \code{apexcharts} \code{htmlwidget} object.
} }
\description{ \description{
Create a apexcharts.js widget Create an apexcharts.js widget
} }
\examples{ \examples{
library(apexcharter) library(apexcharter)