Skip to content Skip to sidebar Skip to footer

How To Change Plotly Legendstatus Of Trace N With A Generic Button + SelectInput Through Javascript In R Shiny

Thanks to answers on this previous question, I have developed a plotly plot with buttons linked to its legend, where clicking the legend changes the state of a reactive variable co

Solution 1:

I'm not sure I understand, but let's start with something.

js1 <- c(
  "function toggleLegend(id){",
  "  var ids = id.split('-');",
  "  var plotid = ids[1];",
  "  var index = parseInt(ids[2])-1;",
  "  var plot = document.getElementById(plotid);",
  "  var data = plot.data;",
  "  var v0 = data[index].visible || true;",
  "  var v = v0 == true ? 'legendonly' : true;",
  "  Plotly.restyle(plot, {visible: v}, [index]);",
  "}",
  "function toggleLegend2(){",
  "  var index = parseInt($('#SelectTrace').val())-1;",
  "  var plot = document.getElementById('plot1');",
  "  var data = plot.data;",
  "  var v0 = data[index].visible || true;",
  "  var v = v0 == true ? 'legendonly' : true;",
  "  Plotly.restyle(plot, {visible: v}, [index]);",
  "}")

actionButton(inputId = 'SwitchExt', ......, onclick = "toggleLegend2()")

Is it what you want ?


Post a Comment for "How To Change Plotly Legendstatus Of Trace N With A Generic Button + SelectInput Through Javascript In R Shiny"