Skip to content

Commit ea7a344

Browse files
Preserve milliseconds with dynamicTicks (#1871)
* Fix #1870 by explicitly including milliseconds when converting to JSON * Added visual test for #1870 * Fix POSIXt format for plotly JSON According to plotly documentation (https://plotly.com/chart-studio-help/date-format-and-time-series/): "Chart Studio’s date format is 'yyyy-mm-dd HH:MM:SS.ssssss'.", so time_format in jsonlite::toJSON() changed to match. * Fixing time format in test re: #1871 * Update test image for line-milliseconds.svg
1 parent a315277 commit ea7a344

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

R/utils.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,8 @@ try_file <- function(f, what) {
10211021
# preferred defaults for toJSON mapping
10221022
to_JSON <- function(x, ...) {
10231023
jsonlite::toJSON(x, digits = 50, auto_unbox = TRUE, force = TRUE,
1024-
null = "null", na = "null", ...)
1024+
null = "null", na = "null",
1025+
time_format = "%Y-%m-%d %H:%M:%OS6", ...)
10251026
}
10261027

10271028
# preferred defaults for toJSON mapping
Lines changed: 1 addition & 0 deletions
Loading

tests/testthat/test-ggplot-lines.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ test_that("different colored lines become different colored traces", {
4141
expect_identical(info$data[[2]]$x[1:n], x)
4242
})
4343

44-
44+
test_that("Milliseconds are preserved with dynamic ticks", {
45+
d <- data.frame(
46+
t = as.POSIXct("1970-01-01 00:00") + (0:999) / 10,
47+
y = sin((0:999) * 4 * pi / 1000)
48+
)
49+
gg <- ggplot(d, aes(t, y)) + geom_line()
50+
p <- ggplotly(gg, dynamicTicks = TRUE)
51+
j <- plotly_json(p, jsonedit = FALSE)
52+
t2 <- jsonlite::fromJSON(j)$data$x[[1]] %>%
53+
as.POSIXct(format = "%Y-%m-%d %H:%M:%OS")
54+
expect_equal(as.numeric(mean(diff(t2))), 0.1)
55+
expect_doppelganger_built(p, "line-milliseconds")
56+
})
4557

4658
test_that("Translates both dates and datetimes (with dynamic ticks) correctly", {
4759

0 commit comments

Comments
 (0)