javascript - 剧情 JS : hover event does not contain points

标签 javascript hover plotly

当鼠标悬停在我的图表上时,我需要 x 轴值。 根据 plotly 文档 ( https://plot.ly/javascript/hover-events/ ),悬停事件回调应该包含字段“points”,我应该从中获取 x 值。

但是如果您查看这个基本示例,您会发现回调不包含字段“points”。其他字段如“数据”也是未定义的:

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 } } );
})

请参阅此 fiddle 以便自己尝试: https://jsfiddle.net/c1kt3r82/158/

我做错了什么?

最佳答案

  • plotly-basic 似乎不支持悬停事件,请改用plotly-latest
  • 当使用 jQuery 选择元素时,它返回的对象与通过 document.getElementById 执行的不同
  • 调用plot后需要定义hover事件

$(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>

关于javascript - 剧情 JS : hover event does not contain points,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42836034/

相关文章:

javascript - 在 javascript 而不是 CSS(悬停)中更改链接颜色 --- 在 jsfiddle 而不是浏览器中有效?

java - 强制 JPopupMenu 禁用其所有者 JFrame 上的悬停效果?

r - 在 plotly 上修改热图中的标签 (x,y,z)?

python - 离线plot.ly无法在jupyter笔记本上工作

python - 如何在每个波段/箱中以数据百分比作为标签绘制正态分布?

javascript - 在 font-awesome 图标下方添加文字

javascript - 如何在MySQL数据库中更新数据后显示加载图像

javascript - 解析 JS/Angular 显示解析对象行

javascript - Angular MVC 项目 - 使用 templateUrl 获取 404

javascript - 简单的 Javascript 悬停