获取点击节点在d3树和中心的坐标 [英] Get coordinates of clicked on node in d3 tree and centre
本文介绍了获取点击节点在d3树和中心的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个点击事件侦听器的树。我想在用户单击的节点上重新居中我的树。
I have a tree with on click event listeners. I'd like to re-centre my tree on whichever node the user clicked.
如何获取点击事件中树节点的实际x / y值?
How do I get the actual x / y values of a tree node inside the click event?
推荐答案
要获得x / y,必须通过您已应用的任何翻译将节点翻译回来:
To get the x/y you must translate the nodes back through whatever translation you've already applied:
http://jsfiddle.net/WLaVU 显示了一个工作示例
// click event handler
function click_handler(d)
{
// these dudes must be smooshed back through the same transform
var x = xs(d);
var y = ys(d);
// normalize for width/height
var new_x = (-x + (width / 2));
var new_y = (-y + (height / 2));
// move the main container g
svg.attr("transform", "translate(" + new_x + "," + new_y + ")");
}
这篇关于获取点击节点在d3树和中心的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文