1

I have a plotly.js dashboard I've made to stream live data. The live data stream part works perfectly fine, but I would really like to animate all the traces at the same time as the data comes in. However, I can't manage to animate more than a single trace at a time, no matter what I pass to Plotly.animate. I've re-created this issue using code from their website.

<html>
<head>
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

<body>
  <div id="graph"></div>
  <button onclick="javascript:randomize();">Randomize!</button>
</body>

<script>
 Plotly.plot('graph', [{
 x: [1, 2, 3],
 y: [0, 0.5, 1],
 line: {simplify: false},
 }, 
 {
 x: [3, 2, 1],
 y: [0, 0.5, 1],
 line: {simplify: false}
 }]);

 function randomize() {
  Plotly.animate('graph', {
  data: [{y: [Math.random(), Math.random(), Math.random()]}],
  traces: [0,1],
  layout: {}
  }, {
  transition: {
  duration: 500,
  ease: 'cubic-in-out'
  }
  })


  }</script>
  </html>

Observe that even though I pass it the traces [0,1] it will only animate the first trace in that array! I couldn't find any documentation which would fix this issue.

1 Answer 1

2

I've found the answer. The issue was with the data I was passing to Plotly.animate, which only had one trace's updated information.

Here is the working code if anyone was wondering:

<html>
<head>
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
  <div id="graph"></div>
  <button onclick="javascript:randomize();">Randomize!</button>
</body>

<script>
Plotly.plot('graph', [{
  x: [1, 2, 3],
  y: [0, 0.5, 1],
  line: {simplify: false},
}, 
{
  x: [3, 2, 1],
  y: [0, 0.5, 1],
  line: {simplify: false}
}]);

function randomize() {
  Plotly.animate('graph', {
    data: [{y: [Math.random(), Math.random(), Math.random()]}, {y: [Math.random(), Math.random(), Math.random()]}],
    traces: [0,1],
    layout: {}
  }, {
    transition: {
      duration: 500,
      ease: 'cubic-in-out'
    }
  })


}</script>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.