x position of tooltip in d3 stacked bar chart not working

旧巷老猫 提交于 2019-12-11 13:47:32

问题


I don't really know why but my xPosition in the stacked bar chart that I am working on is not really working. Can someone tell what I am doing wrong?

No matter what bar I hover over it always comes out on the side:

Here's a JS Bin code that I am working on: https://jsbin.com/qudoyewiba/edit?js,output

All help is appreciated. Thanks!


回答1:


Inspect the console, the console is your friend. You have a lot of NaNs. The reason is simple: your rect elements don't have a "x" attribute. So, this doesn't work:

var xPos = parseFloat(d3.select(this).attr("x"));

Instead, you have to get the "x" translation of the group that is the parent of that respective rectangle:

var group = d3.select(d3.select(this).node().parentNode);
var xPos = d3.transform(group.attr("transform")).translate[0];

Here is your working code: https://jsbin.com/dutetokoti/1/edit



来源:https://stackoverflow.com/questions/39339341/x-position-of-tooltip-in-d3-stacked-bar-chart-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!