Skip to content

Commit 3efb6d3

Browse files
committed
be more careful when searching for defaults
1 parent 270c134 commit 3efb6d3

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

R/plotly_build.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ map_color <- function(traces, title = "", na.color = "transparent") {
433433
colorDefaults <- traceColorDefaults()
434434
for (i in which(isConstant)) {
435435
# https://github.com/plotly/plotly.js/blob/c83735/src/plots/plots.js#L58
436-
idx <- i %% length(colorDefaults) + i %/% length(colorDefaults)
436+
idx <- i %% length(colorDefaults)
437+
if (idx == 0) idx <- 10
437438
col <- color[[i]] %||% colorDefaults[[idx]]
438439
alpha <- traces[[i]]$alpha %||% 1
439440
rgb <- toRGB(col, alpha)

R/subplots.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,13 @@ retrain_color_defaults <- function(traces) {
342342
colorDefaults <- traceColorDefaults()
343343
for (i in seq_along(traces)) {
344344
# https://github.com/plotly/plotly.js/blob/c83735/src/plots/plots.js#L58
345-
idx <- i %% length(colorDefaults) + i %/% length(colorDefaults)
345+
idx <- i %% length(colorDefaults)
346+
if (idx == 0) idx <- 10
346347
newDefault <- colorDefaults[[idx]]
347348
for (j in c("marker", "line", "text")) {
348-
alpha <- attr(traces[[i]][[j]][["color"]], "defaultAlpha")
349+
obj <- traces[[i]][[j]]
350+
if (!"color" %in% names(obj)) next
351+
alpha <- attr(obj[["color"]], "defaultAlpha")
349352
if (is.null(alpha)) next
350353
traces[[i]][[j]][["color"]] <- toRGB(colorDefaults[[idx]], alpha)
351354
}

tests/testthat/test-plotly.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test_that("Alpha still applies when no color is applied", {
120120
p <- plot_ly(mtcars, x = ~mpg, y = ~disp, alpha = 0.5)
121121
l <- expect_traces(p, 1, "alpha-no-color")
122122
# verify the correct alpha for the points
123-
expect_equal(l$data[[1]]$marker$color, "rgba(31,119,180,0.5)")
123+
expect_true(l$data[[1]]$marker$color == "rgba(31,119,180,0.5)")
124124
})
125125

126126

0 commit comments

Comments
 (0)