D3.js: Position tooltips using element position, not mouse position?

后端 未结 4 907
滥情空心
滥情空心 2020-12-02 17:13

I\'m using D3 to draw a scatter graph. I would like to show tooltips when the user mouses over each circle.

My problem is that I can append tooltips, but they\'re p

4条回答
  •  一向
    一向 (楼主)
    2020-12-02 17:18

    In your particular case you can simply use d to position the tooltip, i.e.

    tooltip.html(d)  
      .style("left", d + "px")     
      .style("top", d + "px");
    

    To make this a bit more general, you can select the element that is being moused over and get its coordinates to position the tooltip, i.e.

    tooltip.html(d)  
      .style("left", d3.select(this).attr("cx") + "px")     
      .style("top", d3.select(this).attr("cy") + "px");
    

提交回复
热议问题