GKVM
GKVM

Reputation: 335

How to return Neo4j OGM entities in GraphJSON format instead of tree format for visualization?

I am getting JSON data from Neo4j OGM (Object Graph Mapping) in nested-tree like format. http://bl.ocks.org/mbostock/4063550

But for visualizing complex graphs, I need to get the JSON data in the following graphJSON format. http://bl.ocks.org/mbostock/4062045

i.e.,with nodes and links separated.

This is for visualizing data from Neo4j using d3.js visualization after performing various operations using Java.

Upvotes: 2

Views: 867

Answers (2)

Michael Hunger
Michael Hunger

Reputation: 41676

There is documentation on how to do it in the examples linked from:

The examples are in:

Upvotes: 1

Cyril Cherian
Cyril Cherian

Reputation: 32327

If your data json is in the format defined in http://bl.ocks.org/mbostock/4063550 and you want to make it into the json format defined in http://bl.ocks.org/mbostock/4062045.

Do something like this:

//this will make the nodes
function flatten(root) {
    var nodes = [];
    var i = 0;

    function recurse(node) {
        if (node.children) node.children.forEach(recurse);
        if (!node.id) node.id = ++i;
        nodes.push(node);
    }

    recurse(root);
    return nodes;
}
//Here root is the json defined in http://bl.ocks.org/mbostock/4063550
nodes = flatten(root);
//this will return the links in the desired format
links = d3.layout.tree().links(nodes);
//check console for output
console.log(nodes)
console.log(links)

Yes you can change data dynamically there are many examples on this. Working code here Hope this helps!

Upvotes: 1

Related Questions