获取点击节点在d3树和中心的坐标

编程入门 行业动态 更新时间:2024-10-10 06:14:14
本文介绍了获取点击节点在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树和中心的坐标

本文发布于:2023-11-29 10:58:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   坐标   中心

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!