From 898ff0f72946dadf4246349ecf36cfa08e05ea14 Mon Sep 17 00:00:00 2001 From: pvictor Date: Tue, 15 Dec 2020 19:14:25 +0100 Subject: [PATCH 1/2] add line to candlestick chart --- R/apex.R | 2 +- R/mixed-charts.R | 11 +++++++++-- R/parse-data.R | 4 +++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/R/apex.R b/R/apex.R index 69d0adf..1266508 100644 --- a/R/apex.R +++ b/R/apex.R @@ -141,7 +141,7 @@ make_series <- function(mapdata, mapping, type = NULL, serie_name = NULL) { mapdata$x[is.na(mapdata$x)] <- "NA" x_order <- unique(mapdata$x) if (is_x_datetime(mapdata)) { - add_names <- FALSE + add_names <- c("x", "y") x_order <- sort(x_order) } else { add_names <- names(mapping) diff --git a/R/mixed-charts.R b/R/mixed-charts.R index 8282081..8ef6407 100644 --- a/R/mixed-charts.R +++ b/R/mixed-charts.R @@ -31,8 +31,8 @@ add_line <- function(ax, } else { apex_type <- ax$x$mixed_type } - if (!isTRUE(apex_type %in% c("line", "bar", "scatter"))) - stop("add_line: apex() must be a column or scatter chart.", call. = FALSE) + if (!isTRUE(apex_type %in% c("line", "bar", "scatter", "candlestick"))) + stop("add_line: apex() must be a column, scatter or candlestick chart.", call. = FALSE) ax$x$ax_opts$chart$type <- "line" if (is.null(data)) data <- ax$x$data @@ -56,6 +56,13 @@ add_line <- function(ax, ax$x$ax_opts$stroke$width <- c(ax$x$ax_opts$stroke$width, 4) } } + if (identical(apex_type, "candlestick")) { + if (is.null(ax$x$ax_opts$stroke$width)) { + ax$x$ax_opts$stroke$width <- c(1, 4) + } else { + ax$x$ax_opts$stroke$width <- c(ax$x$ax_opts$stroke$width, 4) + } + } if (identical(type, "line")) { ax$x$ax_opts$stroke$curve <- "straight" } else if (identical(type, "spline")) { diff --git a/R/parse-data.R b/R/parse-data.R index 00ebb82..73888e4 100644 --- a/R/parse-data.R +++ b/R/parse-data.R @@ -35,7 +35,8 @@ parse_df <- function(data, add_names = FALSE) { # format(x) js_date(x) } else if (inherits(x, "POSIXt")) { - as.numeric(x) * 1000 + # as.numeric(x) * 1000 + js_date(x) } else if (inherits(x, "factor")) { as.character(x) } else { @@ -112,6 +113,7 @@ parse_timeline_data <- function(.list) { parse_candlestick_data <- function(.list) { list(list( + type = "candlestick", data = lapply( X = seq_len(length(.list[[1]])), FUN = function(i) { From 9a56de4d4ee5cbc59726fa7eb705a6d300074944 Mon Sep 17 00:00:00 2001 From: pvictor Date: Wed, 16 Dec 2020 14:12:57 +0100 Subject: [PATCH 2/2] candlestick: fixed timestamps issues --- R/apex.R | 4 ++-- R/mixed-charts.R | 2 +- R/parse-data.R | 12 ++++++------ tests/testthat/test-parse-data.R | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/R/apex.R b/R/apex.R index 1266508..3310a3b 100644 --- a/R/apex.R +++ b/R/apex.R @@ -113,7 +113,7 @@ apex <- function(data, mapping, type = "column", ..., # Construct series -make_series <- function(mapdata, mapping, type = NULL, serie_name = NULL) { +make_series <- function(mapdata, mapping, type = NULL, serie_name = NULL, force_datetime_names = FALSE) { if (identical(type, "candlestick")) { if (!all(c("x", "open", "high", "low", "close") %in% names(mapping))) stop("For candlestick charts 'x', 'open', 'high', 'low', and 'close' aesthetics must be provided.", call. = FALSE) @@ -141,7 +141,7 @@ make_series <- function(mapdata, mapping, type = NULL, serie_name = NULL) { mapdata$x[is.na(mapdata$x)] <- "NA" x_order <- unique(mapdata$x) if (is_x_datetime(mapdata)) { - add_names <- c("x", "y") + add_names <- force_datetime_names x_order <- sort(x_order) } else { add_names <- names(mapping) diff --git a/R/mixed-charts.R b/R/mixed-charts.R index 8ef6407..72af3e0 100644 --- a/R/mixed-charts.R +++ b/R/mixed-charts.R @@ -40,7 +40,7 @@ add_line <- function(ax, mapdata <- lapply(mapping, rlang::eval_tidy, data = data) ax$x$ax_opts$series <- c( ax$x$ax_opts$series, - make_series(mapdata, mapping, type, serie_name) + make_series(mapdata, mapping, type, serie_name, force_datetime_names = c("x", "y")) ) if (identical(apex_type, "scatter")) { if (is.null(ax$x$ax_opts$markers$size)) { diff --git a/R/parse-data.R b/R/parse-data.R index 73888e4..4b71751 100644 --- a/R/parse-data.R +++ b/R/parse-data.R @@ -31,12 +31,11 @@ parse_df <- function(data, add_names = FALSE) { X = data[], FUN = function(x) { if (inherits(x, "Date")) { - # as.numeric(x) * 86400000 - # format(x) - js_date(x) + # js_date(x) + as.numeric(x) * 1000 * 60*60*24 } else if (inherits(x, "POSIXt")) { - # as.numeric(x) * 1000 - js_date(x) + # js_date(x) + as.numeric(x) * 1000 } else if (inherits(x, "factor")) { as.character(x) } else { @@ -119,7 +118,8 @@ parse_candlestick_data <- function(.list) { FUN = function(i) { val <- lapply(.list, `[[`, i) list( - x = js_date(val$x)[[1]], + # x = js_date(val$x)[[1]], + x = as.numeric(val$x) * 1000, y = c(val$open, val$high, val$low, val$close) ) } diff --git a/tests/testthat/test-parse-data.R b/tests/testthat/test-parse-data.R index 4d53f2b..44371fa 100644 --- a/tests/testthat/test-parse-data.R +++ b/tests/testthat/test-parse-data.R @@ -44,11 +44,11 @@ test_that("parse_df works with Date/POSIXt", { ) res <- parse_df(x, add_names = TRUE) - expect_is(res[[1]]$date, "JS_EVAL") + expect_is(res[[1]]$date, "numeric") expect_is(res[[1]]$datetime, "numeric") res <- parse_df(x, add_names = FALSE) - expect_is(res[[1]][[1]], "JS_EVAL") + expect_is(res[[1]][[1]], "numeric") expect_is(res[[1]][[2]], "numeric") })