2017-02-20 85 views
1

我正在從mongodb .find()中檢索記錄,並嘗試使用d3v4 .stratify()嵌套這些記錄。不幸的是,我得到錯誤使用d3v4嵌套JSON .stratify()

Error: ambiguous: Product Line 1

我假設我誤解了API documentation

雖然這個問題似乎接近this post,我不認爲這是完全一樣的。但是,我試圖得到的結果是like this

有人可以幫助我理解如何正確使用.stratify()嗎?或建議一個更簡單的方法來做到這一點?

我的結果集(mongodb_data)不是很大(也許〜2500級的產品)。

我的平坦mongodb_data看起來像這樣,

[ 
    { 
    "name": "Product Line 1", 
    "reference": "product 1.A identifier" 
    }, 
    { 
    "name": "Product Line 1", 
    "reference": "product 1.B identifier" 
    }, 
    { 
    "name": "Product Line 2", 
    "reference": "product 2.A identifier" 
    }, 
    { 
    "name": "Product Line 2", 
    "reference": "product 2.B identifier" 
    } 
]; 

所需分層/嵌套數據需要像這樣,

{ 
"name": "Master Product Catalog", 
"children": [ 
      { 
       "name": "Product Line 1", 
       "children": [ 
          { "name": "product 1.A identifier" }, 
          { "name": "product 1.B identifier" } 
          ] 
      }, 
      { "name": "Product Line 2", 
       "children": [ 
          { "name": "product 2.A identifier" }, 
          { "name": "product 2.B identifier" } 
          ] 
      } 
      ] 
} 

我已經使用例如由API文檔被如下,

var stratdata = d3.stratify() 
        .id(function(d) { return d.name; }) 
        .parentId(function(d) { return d.name; }) 
        (mongodb_data); 

回答

0

您的平面數據不代表分層結構。它不回答「產品系列1」或「產品系列2」父母是誰的問題?此外,您的name屬性不會同時包含父級和子級信息(引用是父級,名稱是子級)。 d3.stratify()之前,您的數據應該是這樣的:

[{ 
    "reference": "Master Product Catalog", 
    "name": "" //<-- has no parent 
}, { 
    "reference": "Product Line 1", 
    "name": "Master Product Catalog" //<-- parent is master 
}, { 
    "reference": "Product Line 2", 
    "name": "Master Product Catalog" 
}, { 
    "name": "Product Line 1", 
    "reference": "product 1.A identifier" 
}, { 
    "name": "Product Line 1", 
    "reference": "product 1.B identifier" 
}, { 
    "name": "Product Line 2", 
    "reference": "product 2.A identifier" 
}, { 
    "name": "Product Line 2", 
    "reference": "product 2.B identifier" 
}]; 

運行代碼:

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script data-require="[email protected]" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <script> 
 
    var json = [{ 
 
     "reference": "Master Product Catalog", 
 
     "name": "" 
 
    }, { 
 
     "reference": "Product Line 1", 
 
     "name": "Master Product Catalog" 
 
    }, { 
 
     "reference": "Product Line 2", 
 
     "name": "Master Product Catalog" 
 
    }, { 
 
     "name": "Product Line 1", 
 
     "reference": "product 1.A identifier" 
 
    }, { 
 
     "name": "Product Line 1", 
 
     "reference": "product 1.B identifier" 
 
    }, { 
 
     "name": "Product Line 2", 
 
     "reference": "product 2.A identifier" 
 
    }, { 
 
     "name": "Product Line 2", 
 
     "reference": "product 2.B identifier" 
 
    }]; 
 

 
    var root = d3.stratify() 
 
     .id(function(d) { 
 
     return d.reference; 
 
     }) 
 
     .parentId(function(d) { 
 
     return d.name; 
 
     }) 
 
     (json); 
 

 
    console.log(root) 
 
    </script> 
 
</body> 
 

 
</html>

+0

嗯,有趣。它適用於我的