簡體   English   中英

d3獲取樹的所有節點

[英]d3 get all nodes of tree

我盡力做到這一點:

在摘要中,我有一個代表一棵樹的Json。 在d3的幫助下,我嘗試將根的所有子節點獲取為數組。 為此,我使用功能“節點”。

問題是我的子項鍵稱為“ _children”而不是“ children”。 我試圖找到一個好的解決方案,告訴節點函數檢查“ children”而不是“ children”。 如果我刪除所有子鍵的“ ”,它將起作用。

 var json = {"_name":"root","_children":[{"_name":"Application","_children":[{"_name":"Application Heap","_children":[],"_color":"#0000ff", "MEMORY":20},{"_name":"Other","_children":[],"_color":"#000055","MEMORY":30},{"_name":"Statement Heap","_children":[],"_color":"","MEMORY":40}]}]}; console.log(json); // tell d3 that my children key is "_children" var treemap = d3.layout.treemap() .children(function(d) { return d._children; }) .value(function(d) { return d.MEMORY; }); // With this line I try to get all child nodes of the root element var nodes = treemap.nodes(json) .filter(function(d) {return !d._children; }); console.log(json); // d3 sets the value and everything else correct console.log(nodes); // for some reason I get an empty array 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

一種可能的解決方案(但不是我想要的解決方案)是首先像這樣重命名字段遞歸:

var renameKeys=function(obj){
    if(obj._children){
        obj.children=obj._children;
        delete obj._children;
        obj.children.forEach(renameKeys);
    }
return obj;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM