Trees
by ramnathv
HTML
<script src="//d3js.org/d3.v3.min.js"></script>
JavaScript
var links = [
{source: "A1", target: "A2", attrib1: "X"},
{source: "A2", target: "A3", attrib1: "Y"},
{source: "A2", target: "A4", attrib1: "Z"}
]
function nodeByName(name) {
return nodesByName[name] || (nodesByName[name] = {name: name});
}
var nodesByName = {};
// Create nodes for each unique source and target.
links.forEach(function(link) {
var parent = link.source = nodeByName(link.source),
child = link.target = nodeByName(link.target);
if (parent.children) parent.children.push(child);
else parent.children = [child];
});
// Extract the root node and compute the layout.
var nodes = links[0].source
console.log(JSON.stringify(nodes))