Writing graph to JSON

26 views
Skip to first unread message

gre...@eng.ucsd.edu

unread,
Jul 5, 2017, 12:26:27 PM7/5/17
to d3-js
I'm creating a D3 Sankey diagram which loads the nodes and links from a JSON file.

I'd like to allow the user to interactively change the links and then output the new data to another JSON file.

I can't find any documentation on how to take the data and write it to a file as JSON.

Can anyone point me in the right direction?

Thanks.
-Tony

gre...@eng.ucsd.edu

unread,
Jul 5, 2017, 11:38:22 PM7/5/17
to d3-js
I found it.

 txt = '{\n"nodes":[\n';
       
            d3.selectAll("g.node").each(function(d) {
                txt += '{"node":' + d.node + ', "name":"' + d.name + '"},\n'
            })
            txt = txt.substring(0, txt.length - 2); // strip last ,
            txt += '\n],\n"links": [\n'
           
            d3.selectAll(".link").each(function(d) {
                txt += '{"source":' + d.source.node + ', "target":'
                + d.target.node + ',"value":' + d.value + '},\n'
            })   
            txt = txt.substring(0, txt.length - 2); // strip last ,
            txt += '\n]}'
           
            console.log(txt);   
            };
Reply all
Reply to author
Forward
0 new messages