I extract nodes and relationships from a neo4j database using the following cypher query: match p=(:Root) <-[:linkedTo]- () unwind nodes(p) as n unwind rels(p) as r return {nodes: collect(distinct n), links: collect(distinct {source: id(endNode(r)), target: id(startNode(r))})}
I convert the result of the query to arrays of nodes and links as follow:
var obj = JSON.parse(xmlhttp.responseText);
var json = obj.data[0][0];
// Extract node list from neo4j/json data
var nodes = [];
for (n in json.nodes) {
var nodeObj = json.nodes[n];
var node = nodeObj.data;
node.id = nodeObj.metadata.id;
node.type = nodeObj.metadata.labels[0];
nodes.push(node);
}
// Create a node map
var nodeMap = {};
nodes.forEach(function(x) { nodeMap['_'+x.id] = x; nodeMap['_'+x.id].children = []; });
// Extract link list from neo4j/json data
var links = json.links.map(function(x) {
nodeMap['_'+x.source].children.push(nodeMap['_'+x.target]);
return { source: nodeMap['_'+x.source], target: nodeMap['_'+x.target] };
});
How should I generate a tree in d3 from the nodes and links? Console.log() shows that both the node and link arrays have the proper format, each node also contains the list of its children.