2018-07-30 22:54:39 +02:00
|
|
|
|
2020-02-12 11:57:24 +01:00
|
|
|
#' Create an apexcharts.js widget
|
2018-07-30 22:54:39 +02:00
|
|
|
#'
|
2019-02-15 22:33:14 +01:00
|
|
|
#' @param ax_opts A \code{list} in JSON format with chart parameters.
|
2020-02-12 11:57:24 +01:00
|
|
|
#' @param auto_update In Shiny application, update existing chart
|
2020-02-12 18:21:40 +01:00
|
|
|
#' rather than generating new one. Can be \code{TRUE}/\code{FALSE} or
|
|
|
|
#' use \code{\link{config_update}} for more control.
|
2018-07-30 22:54:39 +02:00
|
|
|
#' @param width A numeric input in pixels.
|
|
|
|
#' @param height A numeric input in pixels.
|
|
|
|
#' @param elementId Use an explicit element ID for the widget.
|
|
|
|
#'
|
|
|
|
#' @return A \code{apexcharts} \code{htmlwidget} object.
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @importFrom htmlwidgets createWidget sizingPolicy
|
|
|
|
#'
|
2019-11-26 11:26:47 +01:00
|
|
|
#' @example examples/apexchart.R
|
2020-02-12 18:21:40 +01:00
|
|
|
apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height = NULL, elementId = NULL) {
|
|
|
|
|
|
|
|
if (isTRUE(auto_update)) {
|
|
|
|
auto_update <- config_update()
|
|
|
|
}
|
2020-03-03 19:34:07 +01:00
|
|
|
|
2018-07-30 22:54:39 +02:00
|
|
|
x <- list(
|
|
|
|
ax_opts = ax_opts,
|
2020-04-16 16:42:34 +02:00
|
|
|
auto_update = auto_update,
|
|
|
|
sparkbox = FALSE
|
2018-07-30 22:54:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# create widget
|
|
|
|
htmlwidgets::createWidget(
|
2020-02-12 11:57:24 +01:00
|
|
|
name = "apexcharter",
|
2019-02-15 22:33:14 +01:00
|
|
|
x = x,
|
2018-07-30 22:54:39 +02:00
|
|
|
width = width,
|
|
|
|
height = height,
|
2020-02-12 11:57:24 +01:00
|
|
|
package = "apexcharter",
|
2018-07-30 22:54:39 +02:00
|
|
|
elementId = elementId,
|
2020-07-26 10:46:18 +02:00
|
|
|
preRenderHook = function(widget) {
|
|
|
|
widget$x$data <- NULL
|
2020-07-26 18:38:18 +02:00
|
|
|
widget$x$mapping <- NULL
|
2020-07-26 10:46:18 +02:00
|
|
|
add_locale_apex(widget)
|
|
|
|
},
|
2018-07-30 22:54:39 +02:00
|
|
|
sizingPolicy = htmlwidgets::sizingPolicy(
|
2019-02-18 20:29:51 +01:00
|
|
|
defaultWidth = "100%",
|
|
|
|
defaultHeight = "100%",
|
2018-07-30 22:54:39 +02:00
|
|
|
viewer.defaultHeight = "100%",
|
|
|
|
viewer.defaultWidth = "100%",
|
|
|
|
knitr.figure = FALSE,
|
2019-02-18 20:29:51 +01:00
|
|
|
knitr.defaultWidth = "100%",
|
|
|
|
knitr.defaultHeight = "350px",
|
2018-07-30 22:54:39 +02:00
|
|
|
browser.fill = TRUE,
|
2018-08-20 23:02:46 +02:00
|
|
|
viewer.suppress = FALSE,
|
2018-08-02 16:47:32 +02:00
|
|
|
browser.external = TRUE,
|
2020-03-03 19:34:07 +01:00
|
|
|
padding = 0
|
2018-07-30 22:54:39 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:35 +01:00
|
|
|
#' @importFrom jsonlite fromJSON
|
|
|
|
add_locale_apex <- function(widget) {
|
2020-03-03 19:34:07 +01:00
|
|
|
if (!is.null(widget$x$ax_opts$chart$defaultLocale)) {
|
|
|
|
defaultLocale <- widget$x$ax_opts$chart$defaultLocale
|
|
|
|
defaultLocale <- match.arg(
|
|
|
|
arg = defaultLocale,
|
2020-04-27 14:52:04 +02:00
|
|
|
choices = c("ca", "cs", "de", "el", "en", "es", "fi", "fr", "he", "hi",
|
|
|
|
"hr", "hy", "id", "it", "ko", "lt", "nb", "nl", "pl", "pt-br",
|
|
|
|
"pt", "ru", "se", "sk", "sl", "th", "tr", "ua")
|
2020-03-03 19:34:07 +01:00
|
|
|
)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-12 18:21:40 +01:00
|
|
|
#' Configuration for auto update
|
|
|
|
#'
|
|
|
|
#' @param series_animate Should the chart animate on re-rendering.
|
|
|
|
#' @param update_options Update or not global options for chart.
|
|
|
|
#' @param options_animate Should the chart animate on re-rendering.
|
|
|
|
#' @param options_redrawPaths When the chart is re-rendered,
|
|
|
|
#' should it draw from the existing paths or completely redraw
|
|
|
|
#' the chart paths from the beginning. By default, the chart
|
2020-04-15 14:41:15 +02:00
|
|
|
#' is re-rendered from the existing paths.
|
2020-04-15 16:20:29 +02:00
|
|
|
#' @param update_synced_charts All the charts in a group should
|
|
|
|
#' also update when one chart in a group is updated.
|
2020-02-12 18:21:40 +01:00
|
|
|
#'
|
|
|
|
#' @export
|
|
|
|
config_update <- function(series_animate = TRUE,
|
|
|
|
update_options = FALSE,
|
|
|
|
options_animate = TRUE,
|
2020-04-15 16:20:29 +02:00
|
|
|
options_redrawPaths = TRUE,
|
|
|
|
update_synced_charts = FALSE) {
|
2020-02-12 18:21:40 +01:00
|
|
|
list(
|
|
|
|
series_animate = series_animate,
|
|
|
|
update_options = update_options,
|
|
|
|
options_animate = options_animate,
|
2020-04-15 16:20:29 +02:00
|
|
|
options_redrawPaths = options_redrawPaths,
|
|
|
|
update_synced_charts = update_synced_charts
|
2020-02-12 18:21:40 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-24 12:20:22 +02:00
|
|
|
#' @title Shiny bindings for apexcharter
|
2018-07-30 22:54:39 +02:00
|
|
|
#'
|
2019-07-24 12:20:22 +02:00
|
|
|
#' @description Output and render functions for using apexcharter within Shiny
|
2018-07-30 22:54:39 +02:00
|
|
|
#' applications and interactive Rmd documents.
|
|
|
|
#'
|
|
|
|
#' @param outputId output variable to read from
|
|
|
|
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
|
|
|
|
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
|
|
|
|
#' string and have \code{'px'} appended.
|
|
|
|
#' @param expr An expression that generates a apexcharter
|
|
|
|
#' @param env The environment in which to evaluate \code{expr}.
|
|
|
|
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
|
|
|
|
#' is useful if you want to save an expression in a variable.
|
2019-07-24 12:20:22 +02:00
|
|
|
#'
|
|
|
|
#' @return An Apexchart output that can be included in the application UI.
|
2018-07-30 22:54:39 +02:00
|
|
|
#'
|
|
|
|
#' @name apexcharter-shiny
|
|
|
|
#'
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget
|
2019-07-19 14:11:41 +02:00
|
|
|
#'
|
2019-11-26 11:26:47 +01:00
|
|
|
#' @example examples/apexcharter-shiny.R
|
2020-02-12 11:57:24 +01:00
|
|
|
apexchartOutput <- function(outputId, width = "100%", height = "400px"){
|
|
|
|
htmlwidgets::shinyWidgetOutput(outputId, "apexcharter", width, height, package = "apexcharter")
|
2018-07-30 22:54:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#' @rdname apexcharter-shiny
|
|
|
|
#' @export
|
2018-09-07 18:06:41 +02:00
|
|
|
renderApexchart <- function(expr, env = parent.frame(), quoted = FALSE) {
|
2018-07-30 22:54:39 +02:00
|
|
|
if (!quoted) { expr <- substitute(expr) } # force quoted
|
2018-09-07 18:06:41 +02:00
|
|
|
htmlwidgets::shinyRenderWidget(expr, apexchartOutput, env, quoted = TRUE)
|
2018-07-30 22:54:39 +02:00
|
|
|
}
|