Skip to content

Commit 982836b

Browse files
committed
uniq() text vectors before collapsing on fills; apply toRGB at the trace level; add test; closes #1233
1 parent 124bce8 commit 982836b

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

R/plotly_build.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = "
742742
# i.e., interpret values as color codes
743743
if (any(isConstant)) {
744744
colorCodes <- Map(`%||%`, color, rep(colorway, length.out = length(traces)))
745-
colorCodes <- toRGB(colorCodes[isConstant], alphas[isConstant])
745+
colorCodes <- Map(toRGB, colorCodes[isConstant], alphas[isConstant])
746746
isColorway <- lengths(color[isConstant]) == 0
747747
traces[isConstant] <- Map(mapColor, traces[isConstant], colorCodes, isColorway)
748748
}

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ verify_attr <- function(proposed, schema) {
481481

482482
# plotly.js should really handle this logic
483483
if (isTRUE(grepl("fill", proposed[["hoveron"]]))) {
484-
proposed[["text"]] <- paste(proposed[["text"]], collapse = "\n")
484+
proposed[["text"]] <- paste(uniq(proposed[["text"]]), collapse = "\n")
485485
}
486486

487487
# ensure data_arrays of length 1 are boxed up by to_JSON()

tests/testthat/test-plotly.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,43 @@ test_that("span/size controls errorbar thickness/width", {
209209
expect_true(d[[1]]$error_y$width == 10)
210210
expect_true(d[[1]]$error_y$color == toRGB("red"))
211211
})
212+
213+
214+
test_that("Vector of redundant text is reduced to string when hoveron=fills", {
215+
216+
# see https://github.com/ropensci/plotly/issues/1233
217+
d <- data.frame(
218+
AA = c(2,3,3,2, NA, 6,7,7,6, NA),
219+
BB = c(2,2,3,2, NA, 6,6,7,6, NA),
220+
CC = c(rep('abc', 5), rep('xyz', 5)),
221+
LL = c(rep('A', 5), rep('B', 5))
222+
)
223+
224+
225+
p <- plot_ly(d) %>%
226+
add_trace(x = ~AA,
227+
y = ~BB,
228+
text = ~paste('<br> <b>Example</b> of <em>custom</em> hover text <br>', LL, '<br>', CC, '<br>.'),
229+
split = ~LL,
230+
mode = "lines",
231+
fill = "toself",
232+
hoveron = 'fills',
233+
type = "scatter",
234+
color = I(c(rep(toRGB("black", 1), 5),
235+
rep(toRGB("red", 1), 5)))
236+
)
237+
238+
b <- plotly_build(p)
239+
d <- b$x$data
240+
expect_length(d, 2)
241+
expect_true(d[[1]]$line$color == toRGB("black"))
242+
expect_true(d[[1]]$fillcolor == toRGB("black", 0.5))
243+
expect_true(d[[2]]$line$color == toRGB("red"))
244+
expect_true(d[[2]]$fillcolor == toRGB("red", 0.5))
245+
expect_true(
246+
d[[1]]$text == '<br> <b>Example</b> of <em>custom</em> hover text <br> A <br> abc <br>.'
247+
)
248+
expect_true(
249+
d[[2]]$text == '<br> <b>Example</b> of <em>custom</em> hover text <br> B <br> xyz <br>.'
250+
)
251+
})

0 commit comments

Comments
 (0)