概要
plunkerでd3やってみた。
練習問題、やってみた。
練習問題
オブジェクトからd3.hierarchy用の木構造を写し取る。
方針
- 再帰でやる。
サンプルコード
function maketree(obj) {
var obj2 = {};
Object.keys(obj).forEach(function(key) {
if (key == "Name")
{
obj2["name"] = obj.Name.Data;
}
if (key == "Children")
{
if (obj.Children != [])
{
obj2["children"] = [];
obj.Children.forEach((childNode) => {
obj2.children.push(maketree(childNode));
});
}
}
})
return obj2;
}
実行結果
{"name":"test1","children":[{"name":"T","children":[{"name":"Visual","children":[{"name":"Canvas","children":[{"name":"Image","children":[{"name":"Image","children":[{"name":"ConnectPoint","children":[]}]},{"name":"Vertical Layout","children":[{"name":"Text","children":[]}]}]}]}]}]},{"name":"Display","children":[{"name":"Visual","children":[{"name":"Canvas","children":[{"name":"Image","children":[{"name":"Image","children":[{"name":"ConnectPoint","children":[{"name":"Wire","children":[]}]}]},{"name":"Vertical Layout","children":[{"name":"Vertical Layout","children":[{"name":"Text","children":[]}]}]}]}]}]}]}]}
成果物
以上。