Skip to content

Commit 742b015

Browse files
committed
colorbar limits now controls line.color/line.cmin/line.cmax, fixes #1236
1 parent 47ed487 commit 742b015

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Fixed algorithm for coercing the proposed layout to the plot schema (see #1156).
2424
* grid conversions in `ggplotly()` weren't respected a specified `height`/`width` in RStudio (see #1190).
2525
* `add_*()` no longer inherits `crosstalk::SharedData` key information when `inherit = FALSE`, see #1242.
26+
* The `limits` argument of `colorbar()` wasn't being applied to `line.color`/`line.cmin`/`line.cmax` (see #1236)
2627

2728
# 4.7.1
2829

R/helpers.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ colorbar_built <- function(p, ..., limits = NULL, which = 1) {
6666
x$marker[["color"]][col < limits[1] | limits[2] < col] <- default(NA)
6767
x$marker[["cmin"]] <- default(limits[1])
6868
x$marker[["cmax"]] <- default(limits[2])
69+
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])
6973
x
7074
})
7175
}

tests/testthat/test-plotly-colorbar.R

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,44 @@ test_that("positioning with multiple colorbars and legends", {
135135

136136
expect_true(b$x$layout$legend$y == 0.5)
137137
expect_true(b$x$layout$legend$yanchor == "top")
138+
})
139+
140+
141+
test_that("Colorbar limits controls marker.color and line.color", {
138142

143+
# https://github.com/ropensci/plotly/issues/1236
144+
p <- plot_ly(
145+
mtcars, x = ~hp, y = ~cyl, z = ~mpg, color = ~mpg,
146+
type = "scatter3d", mode = "lines+markers"
147+
)
139148

140-
141-
149+
b <- plotly_build(p)
150+
expect_length(b$x$data, 2)
151+
expect_true(all(b$x$data[[1]]$marker$color == mtcars$mpg))
152+
expect_true(all(b$x$data[[1]]$line$color == mtcars$mpg))
153+
expect_true(b$x$data[[1]]$marker$cmin == min(mtcars$mpg))
154+
expect_true(b$x$data[[1]]$marker$cmax == max(mtcars$mpg))
155+
expect_true(b$x$data[[1]]$line$cmin == min(mtcars$mpg))
156+
expect_true(b$x$data[[1]]$line$cmax == max(mtcars$mpg))
157+
158+
p2 <- colorbar(p, limits = c(0, 100))
159+
b2 <- plotly_build(p2)
160+
expect_length(b2$x$data, 2)
161+
expect_true(all(b2$x$data[[1]]$marker$color == mtcars$mpg))
162+
expect_true(all(b2$x$data[[1]]$line$color == mtcars$mpg))
163+
expect_true(b2$x$data[[1]]$marker$cmin == 0)
164+
expect_true(b2$x$data[[1]]$marker$cmax == 100)
165+
expect_true(b2$x$data[[1]]$line$cmin == 0)
166+
expect_true(b2$x$data[[1]]$line$cmax == 100)
167+
168+
p3 <- colorbar(p, limits = c(20, 100))
169+
b3 <- plotly_build(p3)
170+
mpg <- mtcars$mpg
171+
mpg[mpg < 20] <- NA
172+
expect_true(Reduce(`&`, Map(identical, b3$x$data[[1]]$marker$color, mpg)))
173+
expect_true(Reduce(`&`, Map(identical, b3$x$data[[1]]$line$color, mpg)))
174+
expect_true(b3$x$data[[1]]$marker$cmin == 20)
175+
expect_true(b3$x$data[[1]]$marker$cmax == 100)
176+
expect_true(b3$x$data[[1]]$line$cmin == 20)
177+
expect_true(b3$x$data[[1]]$line$cmax == 100)
142178
})

0 commit comments

Comments
 (0)