【问题标题】:How to assign alternative depth values only to the child nodes in d3js?如何仅将替代深度值分配给 d3js 中的子节点?
【发布时间】:2017-01-24 07:57:16
【问题描述】:

现在所有节点都有固定的深度。如何仅将替代值分配给子节点

我尝试更改值nodes.forEach(function(d) { d.y = d.depth * 200; });,但它会影响所有节点。我想要这样的输出:

【问题讨论】:

  • 请添加代码示例。也许最好不要链接所有文本,而是在每个句子的末尾添加一个链接,这样你的问题就可以清楚地格式化
  • 我的意思是你应该格式化你的帖子,比如现在所有节点都有固定的深度。我怎样才能只为子节点分配一个替代值[见截图]我试图改变值 nodes.forEach (函数(d) { d.y = d.depth * 200; });但它会影响所有节点。我想要这样的输出 [见截图] I.E.分离您的文本并为您放置的链接提供一个单独的 plase
  • 是否应该只有最后一个父级的子级沿 Y 变化?像奇偶序列一样?
  • @ThirueswaranRajagopalan 是的,我想要那样。

标签: javascript json node.js d3.js


【解决方案1】:

您可以使用d.depth 过滤值:

nodes.forEach(function(d) { 
    // value of y differ if d.depth === 1
    d.y = d.depth === 1 ? d.depth * maxLabel : d.depth * 200;
});

http://jsfiddle.net/zmozdpum/

注意:如果您想为奇数节点设置不同的大小,请查看fiddle

【讨论】:

    【解决方案2】:

    演示http://jsfiddle.net/b2rd7ps8/1/

    function normalizeNodesDepth(node){
        /* get index of parent from it's parent's children array..*/
        var parentIndex = (!!node.parent && node.parent != 'null' && !!node.parent.parent && node.parent.parent != 'null' ) ? node.parent.parent.children.map(e => e.id).indexOf(node.parent.id) : null;
        /* if node is the last child and it's parent index is even, mutiple by greater ...*/
        node.y = node.depth * maxLabel * ((parentIndex != null && !(parentIndex % 2) && (!node.children || !!node.children.length)) ? 1.5 : 1);
    
        if(!node.children) { return; }
    
        /* recur for all its children ...*/
        node.children.forEach((child, index) => {
            normalizeNodesDepth(child);
        });
    }     
    
     //nodes.forEach(function(d) { d.y = d.depth * maxLabel; });
     normalizeNodesDepth(root); 
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2017-09-10
      • 1970-01-01
      • 2014-02-28
      相关资源
      最近更新 更多