From 9c9a277aeefb913ba0027e438dfcd7acce0fabfb Mon Sep 17 00:00:00 2001 From: pvictor Date: Fri, 17 Apr 2020 08:57:23 +0200 Subject: [PATCH] x-axis datetime for column & scatter --- DESCRIPTION | 2 +- R/apex.R | 32 ++++++++++++++++++++++++------- tests/testthat/test-apex-config.R | 6 +++--- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b566595..b784ade 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: apexcharter -Version: 0.1.4.920 +Version: 0.1.4.930 Title: Create Interactive Chart with the JavaScript 'ApexCharts' Library Description: Provides an 'htmlwidgets' interface to 'apexcharts.js'. 'Apexcharts' is a modern JavaScript charting library to build interactive charts and visualizations with simple API. diff --git a/R/apex.R b/R/apex.R index b20f7e7..37a199a 100644 --- a/R/apex.R +++ b/R/apex.R @@ -218,12 +218,12 @@ choose_config <- function(type, mapdata) { switch( type, "bar" = config_bar(horizontal = TRUE), - "column" = config_bar(horizontal = FALSE), + "column" = config_bar(horizontal = FALSE, datetime = datetime), "line" = config_line(datetime = datetime), "area" = config_line(datetime = datetime), "spline" = config_line(curve = "smooth", datetime = datetime), - "scatter" = config_scatter(range_x = range_x, range_y = range_y), - "bubble" = config_scatter(range_x = range_x, range_y = range_y), + "scatter" = config_scatter(range_x = range_x, range_y = range_y, datetime = datetime), + "bubble" = config_scatter(range_x = range_x, range_y = range_y, datetime = datetime), "timeline" = config_timeline(), list() ) @@ -231,13 +231,16 @@ choose_config <- function(type, mapdata) { # Config for column & bar charts -config_bar <- function(horizontal = FALSE) { +config_bar <- function(horizontal = FALSE, datetime = FALSE) { config <- list( dataLabels = list(enabled = FALSE), plotOptions = list( bar = list( horizontal = horizontal ) + ), + tooltip = list( + shared = TRUE ) ) if (isTRUE(horizontal)) { @@ -248,6 +251,9 @@ config_bar <- function(horizontal = FALSE) { ) )) } + if (isTRUE(datetime)) { + config$xaxis$type <- "datetime" + } config } @@ -269,15 +275,23 @@ config_line <- function(curve = "straight", datetime = FALSE) { } -config_scatter <- function(range_x, range_y) { +config_scatter <- function(range_x, range_y, datetime = FALSE) { config <- list( dataLabels = list(enabled = FALSE), xaxis = list( type = "numeric", - min = range_x[1], max = range_x[2] + min = range_x[1], max = range_x[2], + crosshairs = list( + show = TRUE, + stroke = list(dashArray = 0) + ) ), yaxis = list( - min = range_y[1], max = range_y[2] + min = range_y[1], max = range_y[2], + decimalsInFloat = 3, + tooltip = list( + enabled = TRUE + ) ), grid = list( xaxis = list( @@ -287,6 +301,10 @@ config_scatter <- function(range_x, range_y) { ) ) ) + if (isTRUE(datetime)) { + config$xaxis$type <- "datetime" + } + config } config_timeline <- function() { diff --git a/tests/testthat/test-apex-config.R b/tests/testthat/test-apex-config.R index 07f9789..d6c2d2b 100644 --- a/tests/testthat/test-apex-config.R +++ b/tests/testthat/test-apex-config.R @@ -60,14 +60,14 @@ test_that("choose_config works", { ) expect_identical(choose_config("bar", mapdata), config_bar(horizontal = TRUE)) - expect_identical(choose_config("column", mapdata), config_bar(horizontal = FALSE)) + expect_identical(choose_config("column", mapdata), config_bar(horizontal = FALSE, datetime = TRUE)) expect_identical(choose_config("line", mapdata), config_line(datetime = TRUE)) expect_identical(choose_config("area", mapdata), config_line(datetime = TRUE)) expect_identical(choose_config("spline", mapdata), config_line(curve = "smooth", datetime = TRUE)) - expect_identical(choose_config("scatter", mapdata), config_scatter(range_num(mapdata$x), range_num(mapdata$y))) - expect_identical(choose_config("bubble", mapdata), config_scatter(range_num(mapdata$x), range_num(mapdata$y))) + expect_identical(choose_config("scatter", mapdata), config_scatter(range_num(mapdata$x), range_num(mapdata$y), datetime = TRUE)) + expect_identical(choose_config("bubble", mapdata), config_scatter(range_num(mapdata$x), range_num(mapdata$y), datetime = TRUE)) expect_identical(choose_config("timeline", mapdata), config_timeline())