From 0a5ad3b9c98c8053554fb2b28edf4075f5c593ed Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Mon, 5 Nov 2018 16:00:10 -0600 Subject: [PATCH] fix definition of has_fill(), closes #1292 --- R/plotly_build.R | 11 ++++++++++- .../plotly-color/plotly-color-box-color-stroke.svg | 1 + tests/testthat/test-plotly-color.R | 11 +++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/figs/plotly-color/plotly-color-box-color-stroke.svg diff --git a/R/plotly_build.R b/R/plotly_build.R index f4a1c443b6..916b72faab 100644 --- a/R/plotly_build.R +++ b/R/plotly_build.R @@ -1005,4 +1005,13 @@ supplyUserPalette <- function(default, user) { # helper functions array_ok <- function(attr) isTRUE(tryNULL(attr$arrayOk)) -has_fill <- function(trace) isTRUE(trace$fill %in% c('tozeroy', 'tozerox', 'tonexty', 'tonextx', 'toself', 'tonext')) +has_fill <- function(trace) { + trace_type <- trace[["type"]] %||% "scatter" + # if trace type has fillcolor, but no fill attribute, then fill is always relevant + has_fillcolor <- has_attr(trace_type, "fillcolor") + has_fill <- has_attr(trace_type, "fill") + if (has_fillcolor && !has_fill) return(TRUE) + fill <- trace[["fill"]] %||% "none" + if (has_fillcolor && isTRUE(fill != "none")) return(TRUE) + FALSE +} diff --git a/tests/figs/plotly-color/plotly-color-box-color-stroke.svg b/tests/figs/plotly-color/plotly-color-box-color-stroke.svg new file mode 100644 index 0000000000..61bf9ac668 --- /dev/null +++ b/tests/figs/plotly-color/plotly-color-box-color-stroke.svg @@ -0,0 +1 @@ +AB−1.5−1−0.500.511.52ABxy diff --git a/tests/testthat/test-plotly-color.R b/tests/testthat/test-plotly-color.R index 3281fe18ce..4ed355372d 100644 --- a/tests/testthat/test-plotly-color.R +++ b/tests/testthat/test-plotly-color.R @@ -63,6 +63,17 @@ test_that("Mapping a numeric variable to color works", { expect_true(all(0 <= markerScale$colorscale[,1] & markerScale$colorscale[,1] <= 1)) }) +test_that("color/stroke mapping with box translates correctly", { + d <- data.frame(x = rep(c("A", "B"), each = 5), y = rnorm(10)) + l <- plot_ly(d) %>% + add_boxplot(x = ~x, y = ~y, color = ~x, colors = c('A' = "blue", 'B' = "red"), stroke = I("black")) %>% + expect_traces(2, "box-color-stroke") + expect_true(l$data[[1]]$fillcolor == toRGB("blue", 0.5)) + expect_true(l$data[[2]]$fillcolor == toRGB("red", 0.5)) + expect_true(l$data[[1]]$line$color == toRGB("black")) + expect_true(l$data[[2]]$line$color == toRGB("black")) +}) + test_that("Custom RColorBrewer pallette works for numeric variable", { p <- plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Petal.Width, colors = "Greens")