frazman
frazman

Reputation: 33213

Converting networkx graph to flare json

I have a networkx graph

g

And I want to draw up this visualization

http://mbostock.github.io/d3/talk/20111018/tree.html

What that means is somehow I have to convert my graph into flare.json

https://bitbucket.org/john2x/d3test/src/2ce4dd511244/d3/examples/data/flare.json

To convert this graph into a tree.. I will give a seed node which serves as root of this json and then grow the tree by adding edges to this tree as its children upto say 3 hops.. How do i do this?

Upvotes: 2

Views: 1364

Answers (1)

Aric
Aric

Reputation: 25289

If you have a tree you can use the networkx tree_data() function to write the data in JSON tree format for that flare.json example.

The example shown there is:

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

To build a tree from your graph either bfs_tree() or dfs_tree() would work. Or maybe you already know how you want to build a tree from your graph.

There is an example of how to use the d3.js library with NetworkX at https://networkx.github.io/documentation/stable/auto_examples/index.html#javascript That uses the d3.js force layout code.

Upvotes: 2

Related Questions