[英]How to get a circle position in a g element in SVG using D3.js
I have a circle inside g element in the SVG. 我在SVG中的g元素内有一个圆圈。 The g element can move simply according to the x-coordinates.
g元素可以根据x坐标简单地移动。 The position of the g element can change as it moves.
g元素的位置可以随其移动而改变。 The circle also moves as it is in that g element.
圆也按照该g元素的原样移动。 My question is how can I retrieve the position of that circle in the SVG NOT its position in g element?
我的问题是如何获取该圆在SVG中的位置而不是其在g元素中的位置? I tried the following code but could not get what I am expecting:
circleGroup.on("mouseover", function() { var translate = d3.transform(d3.select(this).attr("transform")).translate; alert(translate[0].x); });
我尝试了以下代码,但无法获得期望的结果:
circleGroup.on("mouseover", function() { var translate = d3.transform(d3.select(this).attr("transform")).translate; alert(translate[0].x); });
May anyone assist me to retrieve the position of the circle in the SVG not in the g element, please? 有人可以帮助我在g元素中而不在g元素中检索圆的位置吗? I need to get the position of the circle in order to draw a line starting from its position in the SVG not the g element where the circle is in. Your assistance would be very much appreciated.
我需要获取圆的位置才能从其在SVG中的位置而不是圆所在的g元素开始绘制一条线。非常感谢您的协助。 Here is: JsFiddle
这是: JsFiddle
If you want the position of the circle you moused-over, I would create my .on
handler on the circles instead of the g
element (this way you know which element received the event): 如果您希望将鼠标悬停在圆上,请在圆上创建
.on
处理程序,而不是g
元素(通过这种方式,您知道哪个元素接收了事件):
...
var circles = circleGroup.selectAll("circle")
.data(circleData)
.enter()
.append("circle")
.on("mouseover", mouseover)
...
function mouseover() {
var self = d3.select(this);
alert(self.attr('cx'));
}
EDITS FOR COMMENT 编辑评论
Opps, missed the transition. 糟糕,错过了过渡。 How about:
怎么样:
function mouseover()
{
var self = d3.select(this);
var translate = d3.transform(self.attr("transform")).translate;
alert(+self.attr('cx') + translate[0]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.