在d3.js中从csv创建层次结构

时间:2012-11-21 14:11:37

标签: javascript d3.js

我有一个包含以下数据的csv:

world,country,state

World,US,CA

World,US,NJ

World,INDIA,OR

World,INDIA,AP

World,INDIA,TN

我需要转换为树层次结构,如下所示:

{

    "name": "World",
    "children": [
      { "name": "US",
          "children": [
           { "name": "CA" },
           { "name": "NJ" }
         ]
      },
      { "name": "INDIA",
          "children": [
          { "name": "OR" },
          { "name": "TN" },
          { "name": "AP" }
         ]
      }
 ]
};

然后,此树将用于树可视化。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:5)

d3.nest是你的朋友!

查看D3的Nest文档:https://github.com/d3/d3-collection/blob/master/README.md#nests

以下是我在学习使用nest时写的一些例子:https://gist.github.com/3176159

这个问题也有帮助:D3: use nest function to turn flat data with parent key into a hierarchy