1

I need the x-axis values, when hovering over my plotly chart. According to the plotly docs (https://plot.ly/javascript/hover-events/) the hover event callback should contain the field "points" from which I should get the x value.

But if you look at this basic example you can see that the callback does not contain the field "points". Also other fields like "data" are undefined:

HTML

<div id="tester" style="width:600px;height:250px;"></div>

JS

$(document).ready(function(){

   var tester = $('#tester');

   tester.on('plotly_hover', function(data){
     console.log(data)
   });

   Plotly.plot( 'tester', [{
       x: [1, 2, 3, 4, 5],
       y: [1, 2, 4, 8, 16] }], { 
       margin: { t: 0 } } );
})

See this fiddle in order to try it yourself: https://jsfiddle.net/c1kt3r82/158/

What am I doing wrong?

1 Answer 1

2
  • plotly-basic does not seem to support hover events, use plotly-latest instead
  • when using jQuery to select the element, it returns a different object than doing it via document.getElementById
  • the hover events need to be defined after calling plot

$(document).ready(function() {

  var tester = document.getElementById('tester');

  Plotly.plot(tester, [{
    x: [1, 2, 3, 4, 5],
    y: [1, 2, 4, 8, 16]
  }], {
    margin: {
      t: 0
    }
  });
  tester.on('plotly_hover', function(data) {
    console.log(data)
  });
});
<div id="tester" style="width:600px;height:250px;"></div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was it! :)

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.