Alignment of nodes on graph d3.js

See http://jsbin.com/okUxAvE/28 . Then click query. Notice how the children begin to appear on the level query. Now pan to methods(child query) and click on it to expand it. Please note that children methodexpand differently, almost from the center. It is like behavior when there are no more children.

How can I make these children expand just like the children from queryexpand?

I am sure that the problem is in the following code (I think), but I still cannot find the culprit. I just don’t see what the last set of children will look like.

var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);

nodes.forEach(function(d) { //iterate through the nodes
  if(d.parent){ //if the node has a parent
    for(var i = 0; i < d.parent.children.length; i++){ //check parent children
      if(d.parent.children[i].name == d.name){ //find current node
        d.yOffset = i; //index is how far node must be moved down
        d.parentYoffset = d.parent.yOffset; //must also account for parent downset
      }
    }
  }
  if(d.yOffset === undefined){ d.yOffset = 0; }
  if(d.parentYoffset === undefined){ d.parentYoffset = 0; }
  d.x = (d.yOffset * 40) + (d.parentYoffset * 40) + 20;
  d.y = d.depth * 200;
}); 
+4
source share
1

y node y . yoffset ( node).

Y :

var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);

nodes.forEach(function(d) { //iterate through the nodes
  if(d.parent){ //if the node has a parent
    for(var i = 0; i < d.parent.children.length; i++){ //check parent children
      if(d.parent.children[i].name == d.name){ //find current node
        d.yOffset = i; //index is how far node must be moved down
        d.parentYoffset = d.parent.yOffset; //must also account for parent downset

        if (d.parent.parentYoffset) {
            d.parentYoffset += d.parent.parentYoffset;
        }
      }
    }
  }
  if(d.yOffset === undefined){ d.yOffset = 0; }
  if(d.parentYoffset === undefined){ d.parentYoffset = 0; }
  d.x = (d.yOffset * 40) + (d.parentYoffset * 40) + 20;
  d.y = d.depth * 200;
}); 
+3

Source: https://habr.com/ru/post/1544914/


All Articles