2018-07-30 22:54:39 +02:00
|
|
|
|
2021-11-29 18:58:47 +01:00
|
|
|
#' Create an ApexCharts widget
|
2018-07-30 22:54:39 +02:00
|
|
|
#'
|
2021-11-29 18:58:47 +01:00
|
|
|
#' @param ax_opts A `list` in JSON format with chart parameters.
|
2020-02-12 11:57:24 +01:00
|
|
|
#' @param auto_update In Shiny application, update existing chart
|
2021-11-29 18:58:47 +01:00
|
|
|
#' rather than generating new one. Can be `TRUE`/`FALSE` or
|
|
|
|
#' use [config_update()] for more control.
|
|
|
|
#' @param width,height A numeric input in pixels.
|
2018-07-30 22:54:39 +02:00
|
|
|
#' @param elementId Use an explicit element ID for the widget.
|
|
|
|
#'
|
2021-11-29 18:58:47 +01:00
|
|
|
#' @return An [apexchart()] `htmlwidget` object.
|
|
|
|
#' @seealso For quickly create a chart, see [apex()].
|
2018-07-30 22:54:39 +02:00
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @importFrom htmlwidgets createWidget sizingPolicy
|
|
|
|
#'
|
2019-11-26 11:26:47 +01:00
|
|
|
#' @example examples/apexchart.R
|
2021-11-29 18:58:47 +01:00
|
|
|
apexchart <- function(ax_opts = list(),
|
|
|
|
auto_update = TRUE,
|
|
|
|
width = NULL,
|
|
|
|
height = NULL,
|
|
|
|
elementId = NULL) {
|
|
|
|
|
2020-02-12 18:21:40 +01:00
|
|
|
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
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:53:55 +02:00
|
|
|
# dput(tools::file_path_sans_ext(list.files("inst/apexcharts-locale/")))
|
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,
|
2021-11-29 18:58:47 +01:00
|
|
|
choices = c("ca", "cs", "de", "el", "en", "es", "fi", "fr", "he", "hi",
|
|
|
|
"hr", "hu", "hy", "id", "it", "ja", "ka", "ko", "lt", "nb", "nl",
|
|
|
|
"pl", "pt-br", "pt", "rs", "ru", "se", "sk", "sl", "sq", "th",
|
2021-05-10 14:11:18 +02:00
|
|
|
"tr", "ua", "zh-cn")
|
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(
|
2021-08-20 16:53:55 +02:00
|
|
|
file.path("apexcharts-locale", paste0(defaultLocale, ".json")),
|
2020-03-03 19:34:07 +01:00
|
|
|
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,
|
2021-11-29 18:58:47 +01:00
|
|
|
#' 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.
|
2021-11-29 18:58:47 +01:00
|
|
|
#'
|
2020-02-12 18:21:40 +01:00
|
|
|
#' @export
|
2021-11-29 18:58:47 +01:00
|
|
|
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(
|
2021-11-29 18:58:47 +01:00
|
|
|
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.
|
|
|
|
#'
|
2021-11-29 18:58:47 +01:00
|
|
|
#' @param outputId Output variable to read from.
|
|
|
|
#' @param width,height Must be a valid CSS unit (like `100%`,
|
|
|
|
#' `400px`, `auto`) or a number, which will be coerced to a
|
|
|
|
#' string and have `px` appended.
|
|
|
|
#' @param expr An expression that generates a calendar
|
|
|
|
#' @param env The environment in which to evaluate `expr`.
|
|
|
|
#' @param quoted Is `expr` a quoted expression (with `quote()`)? This
|
2018-07-30 22:54:39 +02:00
|
|
|
#' is useful if you want to save an expression in a variable.
|
2021-11-29 18:58:47 +01:00
|
|
|
#'
|
|
|
|
#' @return Output element that can be included in UI. Render function to create output in server.
|
|
|
|
#'
|
|
|
|
#' @note To render a chart with facets (using [ax_facet_wrap()] or [ax_facet_grid()]) in Shiny,
|
|
|
|
#' see [apexfacetOutput()] (in UI) and [renderApexfacet()] (in Server).
|
2018-07-30 22:54:39 +02:00
|
|
|
#'
|
|
|
|
#' @name apexcharter-shiny
|
|
|
|
#'
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget
|
2021-11-29 18:58:47 +01:00
|
|
|
#'
|
2019-11-26 11:26:47 +01:00
|
|
|
#' @example examples/apexcharter-shiny.R
|
2021-01-06 14:47:34 +01:00
|
|
|
apexchartOutput <- function(outputId, width = "100%", height = "400px") { # nocov start
|
2020-02-12 11:57:24 +01:00
|
|
|
htmlwidgets::shinyWidgetOutput(outputId, "apexcharter", width, height, package = "apexcharter")
|
2021-01-06 14:47:34 +01:00
|
|
|
} # nocov end
|
2018-07-30 22:54:39 +02:00
|
|
|
|
|
|
|
#' @rdname apexcharter-shiny
|
|
|
|
#' @export
|
2021-01-06 14:47:34 +01:00
|
|
|
renderApexchart <- function(expr, env = parent.frame(), quoted = FALSE) { # nocov start
|
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)
|
2021-01-06 14:47:34 +01:00
|
|
|
} # nocov end
|