Skip to content

Commit 5a54e41

Browse files
committed
Relay traces tied to legend events
1 parent 5514bfe commit 5a54e41

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

inst/htmlwidgets/plotly.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,29 @@ HTMLWidgets.widget({
352352
Shiny.setInputValue(".clientValue-plotly_afterplot-" + x.source, "afterplot", {priority: "event"});
353353
});
354354

355+
var legendEventData = function(d) {
356+
// if legendgroup is not relevant just return the trace
357+
var trace = d.data[d.curveNumber];
358+
if (!trace.legendgroup) {
359+
Shiny.setInputValue(".clientValue-plotly_legendclick-" + x.source, trace);
360+
return;
361+
}
362+
363+
// if legendgroup was specified, return all traces that match the group
364+
var legendgrps = d.data.map(function(trace){ return trace.legendgroup; });
365+
var traces = [];
366+
for (i = 0; i < legendgrps.length; i++) {
367+
if (legendgrps[i] == trace.legendgroup) {
368+
traces.push(d.data[i]);
369+
}
370+
}
371+
372+
Shiny.setInputValue(".clientValue-plotly_legendclick-" + x.source, traces);
373+
};
374+
375+
graphDiv.on('plotly_legendclick', legendEventData);
376+
graphDiv.on('plotly_legenddoubleclick', legendEventData);
377+
355378
}
356379

357380
// Given an array of {curveNumber: x, pointNumber: y} objects,

0 commit comments

Comments
 (0)