Skip to content

Instantly share code, notes, and snippets.

@cpsievert
Created February 9, 2018 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsievert/28a629df62674f2ac1db5ac59e566f77 to your computer and use it in GitHub Desktop.
Save cpsievert/28a629df62674f2ac1db5ac59e566f77 to your computer and use it in GitHub Desktop.
Draw line shapes outlining brush
library(plotly)
library(crosstalk)
library(htmlwidgets)
mtcars %>%
SharedData$new() %>%
plot_ly(x = ~wt, y = ~mpg) %>%
highlight("plotly_selected", dynamic = TRUE) %>%
onRender(
"function(el, x) {
var gd = document.getElementById(el.id);
gd.on('plotly_selected', function(ed) {
var xmax = ed.range.x[1];
var ymin = ed.range.y[0];
var vline = {type: 'line', y0: 0, y1: 1, yref: 'paper', x0: xmax, x1: xmax, line: {dash: 'dot'}};
var hline = {type: 'line', x0: 0, x1: 1, xref: 'paper', y0: ymin, y1: ymin, line: {dash: 'dot'}};
Plotly.relayout(gd, {shapes: [vline, hline]})
})
}"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment