Skip to content

Commit

Permalink
uniq() text vectors before collapsing on fills; apply toRGB at the tr…
Browse files Browse the repository at this point in the history
…ace level; add test; closes #1233
  • Loading branch information
cpsievert committed Apr 12, 2018
1 parent 124bce8 commit 982836b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/plotly_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = "
# i.e., interpret values as color codes
if (any(isConstant)) {
colorCodes <- Map(`%||%`, color, rep(colorway, length.out = length(traces)))
colorCodes <- toRGB(colorCodes[isConstant], alphas[isConstant])
colorCodes <- Map(toRGB, colorCodes[isConstant], alphas[isConstant])
isColorway <- lengths(color[isConstant]) == 0
traces[isConstant] <- Map(mapColor, traces[isConstant], colorCodes, isColorway)
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ verify_attr <- function(proposed, schema) {

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

# ensure data_arrays of length 1 are boxed up by to_JSON()
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,43 @@ test_that("span/size controls errorbar thickness/width", {
expect_true(d[[1]]$error_y$width == 10)
expect_true(d[[1]]$error_y$color == toRGB("red"))
})


test_that("Vector of redundant text is reduced to string when hoveron=fills", {

# see https://github.com/ropensci/plotly/issues/1233
d <- data.frame(
AA = c(2,3,3,2, NA, 6,7,7,6, NA),
BB = c(2,2,3,2, NA, 6,6,7,6, NA),
CC = c(rep('abc', 5), rep('xyz', 5)),
LL = c(rep('A', 5), rep('B', 5))
)


p <- plot_ly(d) %>%
add_trace(x = ~AA,
y = ~BB,
text = ~paste('<br> <b>Example</b> of <em>custom</em> hover text <br>', LL, '<br>', CC, '<br>.'),
split = ~LL,
mode = "lines",
fill = "toself",
hoveron = 'fills',
type = "scatter",
color = I(c(rep(toRGB("black", 1), 5),
rep(toRGB("red", 1), 5)))
)

b <- plotly_build(p)
d <- b$x$data
expect_length(d, 2)
expect_true(d[[1]]$line$color == toRGB("black"))
expect_true(d[[1]]$fillcolor == toRGB("black", 0.5))
expect_true(d[[2]]$line$color == toRGB("red"))
expect_true(d[[2]]$fillcolor == toRGB("red", 0.5))
expect_true(
d[[1]]$text == '<br> <b>Example</b> of <em>custom</em> hover text <br> A <br> abc <br>.'
)
expect_true(
d[[2]]$text == '<br> <b>Example</b> of <em>custom</em> hover text <br> B <br> xyz <br>.'
)
})

0 comments on commit 982836b

Please sign in to comment.