Skip to content

Commit 704e95f

Browse files
committed
consider legendgroup a mapping variable, closes #1148
1 parent 72bd60f commit 704e95f

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

R/plotly_build.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
189189
dataArrayAttrs, special_attrs(trace), npscales(), "frame",
190190
# for some reason, text isn't listed as a data array in some traces
191191
# I'm looking at you scattergeo...
192-
".plotlyGroupIndex", "text", "key", "fillcolor", "name"
192+
".plotlyGroupIndex", "text", "key", "fillcolor", "name", "legendgroup"
193193
)
194194
tr <- trace[names(trace) %in% allAttrs]
195195
# TODO: does it make sense to "train" matrices/2D-tables (e.g. z)?
@@ -210,11 +210,7 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
210210
if (any(isSplit)) {
211211
paste2 <- function(x, y) if (identical(x, y)) x else paste(x, y, sep = br())
212212
splitVars <- builtData[isSplit]
213-
traceIndex <- Reduce(paste2, splitVars)
214-
if (!is.null(trace$name)) {
215-
traceIndex <- paste2(traceIndex, trace$name)
216-
}
217-
builtData[[".plotlyTraceIndex"]] <- traceIndex
213+
builtData[[".plotlyTraceIndex"]] <- Reduce(paste2, splitVars)
218214
# in registerFrames() we need to strip the frame from .plotlyTraceIndex
219215
# so keep track of which variable it is...
220216
trace$frameOrder <- which(names(splitVars) %in% "frame")

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ verify_attr <- function(proposed, schema) {
492492
proposed[[attr]] <- structure(proposed[[attr]], apiSrc = TRUE)
493493
}
494494

495-
if (length(proposed$name) > 0) {
495+
if (length(proposed[["name"]]) > 0) {
496496
proposed$name <- uniq(proposed$name)
497497
}
498498

tests/testthat/test-plotly.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,31 @@ test_that("Vector of redundant text is reduced to string when hoveron=fills", {
251251
d[[2]]$text == '<br> <b>Example</b> of <em>custom</em> hover text <br> B <br> xyz <br>.'
252252
)
253253
})
254+
255+
256+
test_that("Can map data to legendgroup", {
257+
d <- data.frame(
258+
x = 1:100,
259+
y = runif(100),
260+
group = letters[1:5]
261+
)
262+
263+
l <- plot_ly(data = d, x = ~x, y = ~y) %>%
264+
add_bars(color = ~group, legendgroup = ~group) %>%
265+
add_markers(color = ~group, legendgroup = ~group) %>%
266+
plotly_build()
267+
268+
expect_length(l$x$data, 10)
269+
270+
markers <- compact(lapply(l$x$data, function(tr) if (tr$type == "scatter") tr))
271+
for (i in seq_along(markers)) {
272+
expect_length(markers[[i]]$legendgroup, 1)
273+
expect_true(markers[[i]]$legendgroup == letters[[i]])
274+
}
275+
276+
bars <- compact(lapply(l$x$data, function(tr) if (tr$type == "bar") tr))
277+
for (i in seq_along(bars)) {
278+
expect_length(bars[[i]]$legendgroup, 1)
279+
expect_true(bars[[i]]$legendgroup == letters[[i]])
280+
}
281+
})

0 commit comments

Comments
 (0)