Get coordinates of clicked on node in d3 tree and centre

ε祈祈猫儿з 提交于 2019-12-21 22:45:15

问题


I have a tree with on click event listeners. I'd like to re-centre my tree on whichever node the user clicked.

How do I get the actual x / y values of a tree node inside the click event?


回答1:


To get the x/y you must translate the nodes back through whatever translation you've already applied:

http://jsfiddle.net/WLaVU shows a working example of what you want.

// 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 + ")");
}


来源:https://stackoverflow.com/questions/18924778/get-coordinates-of-clicked-on-node-in-d3-tree-and-centre

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