Skip to content

Commit c3c62ec

Browse files
committed
colorbar limits shouldn't control non-color-scale mapping
1 parent 742b015 commit c3c62ec

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

R/helpers.R

+14-6
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,23 @@ colorbar_built <- function(p, ..., limits = NULL, which = 1) {
6161
p$x$data[[i]]$zmax <- limits[2]
6262
} else {
6363
# since the colorscale is in a different trace, retrain all traces
64+
# TODO: without knowing which traces are tied to the colorscale,
65+
# there are always going to be corner-cases where limits are applied
66+
# to more traces than it should
6467
p$x$data <- lapply(p$x$data, function(x) {
68+
type <- x[["type"]] %||% "scatter"
6569
col <- x$marker[["color"]]
66-
x$marker[["color"]][col < limits[1] | limits[2] < col] <- default(NA)
67-
x$marker[["cmin"]] <- default(limits[1])
68-
x$marker[["cmax"]] <- default(limits[2])
70+
if (has_color_array(type, "marker") && is.numeric(col)) {
71+
x$marker[["color"]][col < limits[1] | limits[2] < col] <- default(NA)
72+
x$marker[["cmin"]] <- default(limits[1])
73+
x$marker[["cmax"]] <- default(limits[2])
74+
}
6975
col <- x$line[["color"]]
70-
x$line[["color"]][col < limits[1] | limits[2] < col] <- default(NA)
71-
x$line[["cmin"]] <- default(limits[1])
72-
x$line[["cmax"]] <- default(limits[2])
76+
if (has_color_array(type, "line") && is.numeric(col)) {
77+
x$line[["color"]][col < limits[1] | limits[2] < col] <- default(NA)
78+
x$line[["cmin"]] <- default(limits[1])
79+
x$line[["cmax"]] <- default(limits[2])
80+
}
7381
x
7482
})
7583
}

R/plotly_build.R

-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,6 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = "
840840
}
841841

842842
# make sure the colorscale is going to convert to JSON nicely
843-
# TODO:
844843
traces[[i]]$marker$colorscale <- as_df(traces[[i]]$marker$colorscale)
845844
}
846845
}

tests/testthat/test-plotly-colorbar.R

+14
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,17 @@ test_that("Colorbar limits controls marker.color and line.color", {
176176
expect_true(b3$x$data[[1]]$line$cmin == 20)
177177
expect_true(b3$x$data[[1]]$line$cmax == 100)
178178
})
179+
180+
test_that("colorbar limits shouldn't control non-color-scale mapping(s)", {
181+
182+
p <- plot_ly(x = 1:10, y = 1:10, color = 1:10) %>%
183+
add_markers() %>%
184+
add_lines(x = 1:3, y = 1:3, color = I('red')) %>%
185+
colorbar(limits = c(1, 5))
186+
187+
b <- plotly_build(p)
188+
expect_length(b$x$data, 3)
189+
190+
expect_true(sum(is.na(b$x$data[[1]]$marker$color)) == 5)
191+
expect_true(b$x$data[[2]]$line$color == toRGB("red"))
192+
})

0 commit comments

Comments
 (0)