auto update

This commit is contained in:
pvictor 2019-02-15 23:33:40 +01:00
parent a21b9bd246
commit e298eb1332
6 changed files with 67 additions and 7 deletions

View File

@ -1,5 +1,5 @@
Package: apexcharter
Version: 0.0.2.900
Version: 0.0.2.910
Title: Create Interactive Chart with the JavaScript 'ApexCharts' Library
Description: Provides an 'htmlwidgets' interface to 'apexcharts.js'.
Authors@R: c(

View File

@ -6,6 +6,7 @@
#' @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{"bubble"}, \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 width A numeric input in pixels.
#' @param height A numeric input in pixels.
#' @param elementId Use an explicit element ID for the widget.
@ -15,7 +16,7 @@
#' @importFrom rlang eval_tidy as_name
#' @importFrom utils modifyList
#'
apex <- function(data, mapping, type = "column", ..., width = NULL, height = NULL, elementId = NULL) {
apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width = NULL, height = NULL, elementId = NULL) {
type <- match.arg(type, c("column", "bar", "line", "area", "spline", "area-spline",
"pie", "donut", "radialBar", "radar", "scatter", "bubble", "heatmap"))
data <- as.data.frame(data)
@ -33,7 +34,10 @@ apex <- function(data, mapping, type = "column", ..., width = NULL, height = NUL
)
}
opts <- modifyList(opts, choose_config(type, is_datetime(mapdata)))
apexchart(ax_opts = opts, width = width, height = height, elementId = elementId)
apexchart(
ax_opts = opts, width = width, height = height,
elementId = elementId, auto_update = auto_update
)
}

View File

@ -26,7 +26,7 @@ function(input, output, session) {
output$chart <- renderApexchart({
apex(data = data_r(), type = "bar", mapping = aes(x = manufacturer, y = n, fill = year))
apex(data = data_r(), type = "bar", mapping = aes(x = manufacturer, y = n, fill = year), auto_update = F)
})
}

View File

@ -29,7 +29,13 @@ HTMLWidgets.widget({
apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts);
apexchart.render();
} else {
apexchart.updateSeries(ax_opts.series);
if (x.auto_update) {
apexchart.updateSeries(ax_opts.series);
} else {
apexchart.destroy();
apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts);
apexchart.render();
}
}

View File

@ -4,8 +4,8 @@
\alias{apex}
\title{Quick Apex Chart}
\usage{
apex(data, mapping, type = "column", ..., width = NULL,
height = NULL, elementId = NULL)
apex(data, mapping, type = "column", ..., auto_update = TRUE,
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}.}
@ -17,6 +17,8 @@ apex(data, mapping, type = "column", ..., width = NULL,
\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{width}{A numeric input in pixels.}
\item{height}{A numeric input in pixels.}

View File

@ -0,0 +1,48 @@
context("test-ax_opt")
test_that("ax_opt works", {
opts <- structure(list(
x = list(ax_opts = list(
chart = list(type ="bar")
))
), class = c("list", "apexcharter"))
new_opts <- .ax_opt(opts, "series", data = 1:3)
expect_length(new_opts$x$ax_opts, 2)
expect_named(new_opts$x$ax_opts, c("chart", "series"))
expect_identical(new_opts$x$ax_opts$series$data, 1:3)
})
test_that("ax_opt update existing parameter", {
opts <- structure(list(
x = list(ax_opts = list(
chart = list(type ="bar")
))
), class = c("list", "apexcharter"))
new_opts <- .ax_opt(opts, "chart", type = "line")
expect_length(new_opts$x$ax_opts, 1)
expect_named(new_opts$x$ax_opts, c("chart"))
expect_identical(new_opts$x$ax_opts$chart$type, "line")
})
test_that("ax_opt2 works", {
opts <- structure(list(
x = list(ax_opts = list(
chart = list(type ="bar")
))
), class = c("list", "apexcharter"))
new_opts <- .ax_opt2(opts, "series", list(data = 1:3))
expect_length(new_opts$x$ax_opts, 2)
expect_named(new_opts$x$ax_opts, c("chart", "series"))
expect_identical(new_opts$x$ax_opts$series$data, 1:3)
})