如何在NetworkX中创建树并在D3.js中显示它

时间:2013-11-10 11:19:36

标签: python mysql d3.js networkx

我必须编写一段代码,从MySQL获取一些数据并使用数据在D3.js中生成图形。当我以这种格式提供静态数据时,我的D3.js代码正在工作:

    {
        "name": " ",
        "children": [{
                "name": "HSC",
                "size": 0.20,
                "children": [
                    [{
                        "name": "MPP",
                        "size": 15,
                        "children": [{
                            "name": "CMP",
                            "size": 8,
                            "children": [{
                                "name": "MEP",
                                "size": 6
                            }, {
                                "name": "GMP",
                                "size": 10,
                                "children": [{
                                    "name": "early PM",
                                    "size": 2,
                                    "children": [{
                                        "name": "early PM",
                                        "size": 2,
                                        "children": []
                                    }]
                                }]
                            }]
                        }]
                    }]
                },
                {
                    "name": "AML",
                    "size": 1,
                    "children": [{
                        "name": "AML t(8,21)",
                        "size": 30
                    }, {
                        "name": "AML t(11q23)",
                        "size": 10
                    }, {
                        "name": "AML inv(16)",
                        "size": 8
                    }, {
                        "name": "AML t(15,17)",
                        "size": 11
                    }]
                }
                ], "size": 1
        }

所以我需要一个用python编写的代码,它可以使用NetworkX制作上面的JSON结构。

到目前为止我的代码:

n = 1 # the number of children for each node 
depth = 1 # number of levels, starting from 0

G = nx.Graph()
G.add_node(1) # initialize root

ulim = 0
for level, row in enumerate(rows): #each row contains name and a size
  print row # loop over each level
  nl = n**level # number of nodes on a given level
  llim = ulim + 1 # index of first node on a given level 
  ulim = ulim + nl # index of last node on a given level
  for i in range(nl): # loop over nodes (parents) on a given level
    parent = llim + i
    offset = ulim + i * n + 1 # index pointing to node just before first child
    for j in range(n): # loop over children for a given node (parent)
      child = offset + j
      G.add_node(child)
      G.add_edge(parent, child)

1 个答案:

答案 0 :(得分:1)

由于您使用的是NetworkX,因此可以调用json_graph.tree_data()生成器。 请参阅此处的文档:http://networkx.lanl.gov/reference/generated/networkx.readwrite.json_graph.tree_data.html#networkx.readwrite.json_graph.tree_data 其中有例子

>>> from networkx.readwrite import json_graph
>>> G = nx.DiGraph([(1,2)])
>>> data = json_graph.tree_data(G,root=1)