0
votes

I am experimenting with plotly.js and would like to make a plot that changes subject to a dropdown selection. I am using jQuery and bootstrap in my code also. The problem I have is that when a new selection is made, it redraws a new line on the same axis and the previous lines are still on the plot as opposed to completely redrawing a new graph that replaces the graph of the previous selection. I tried to explore the .empty() method in jQuery but couldn't get this to work properly. Can anyone spot what I could change in the code below so that only the new selection is plotted?

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Plotly Test</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.plot.ly/plotly-1.2.0.min.js"></script>
  </head>
  <body>

  <div>
    <div class="btn-group">
      <button id="plot-drop-button" type="button" class="btn btn-primary">Select</button>
      <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
        <span class="caret"></span>
      </button>
      <ul id="plot-drop" class="dropdown-menu" role="menu">
        <li value="1"><a href="#">1</a></li>
        <li value="2"><a href="#">2</a></li>
        <li value="3"><a href="#">3</a></li>
      </ul>
    </div>
    <div id="tester" style="height:350px"></div>
  </div>

  <script src="plotly_test.js"></script>
  </body>
</html>

JS

$('#plot-drop li').click( function(event) {
  event.preventDefault(); 
  TESTER = document.getElementById('tester');
  var val = $(this).attr('value');
  $("#plot-drop-button").html(val);
  var a = parseFloat(val);
  var trace1 = {x: [1, 2, 3, 4, 5], y: [1, 2, 4, 8, 16 + a] };
  var data = [trace1];
  Plotly.plot( TESTER, data, { margin: { t: 0 } } );
});
1

1 Answers

0
votes

I managed to answer my own question. I needed to replace:

Plotly.plot()

with:

Plotly.newPlot()