本文介绍了获取点击节点在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:
jsfiddle/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树和中心的坐标
发布评论