首页 > 解决方案 > d3.js tree - How to set Y depth of a specific level?

问题描述

In d3.js tree code, there is the following which sets the y depth for all child levels/nodes:

  // Normalize for fixed-depth.
  nodes.forEach(function(d) {
    d.y = d.depth * 220;
  });

How can I set the Y depth of a specific level in the hierarchy, e.g. the 3rd level?

标签: javascriptd3.js

解决方案


I guess you refer to this code
Simple example for the third level.

nodes.forEach(function(d) {
 (d.depth === 1) ? d.y = d.depth * 100: d.y = d.depth * 300; 
});

It display this https://jsfiddle.net/0rv8z276/

You can use a switch to set for different level, or maybe use an array or a hashmap with the key associated to the level and depth as the value.


推荐阅读