JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://labs.pegleg.com.au/test_example.json"></script>
<body>
</body>
JavaScript
var width = 960,
height = 300
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(.05)
.distance(100)
.charge(-100)
.size([width, height]);
d3.json("http://labs.pegleg.com.au/test_example.json", function(error, json) {
/**** Start of bit I'm struggling with ****/
// make links reference nodes directly for this particular data format:
var hash_lookup = [];
// make it so we can lookup nodes in O(1):
json.Nodes.forEach(function(d, i) {
hash_lookup[d.Id] = d;
});
json.Links.forEach(function(d, i) {
alert(d);
alert(i);
d.source = hash_lookup[d.Source];
d.target = hash_lookup[d.Target];
});
/**** End of bit I'm struggling with ****/
force
.nodes(json.Nodes)
.links(json.Links)
.start();
var link = svg.selectAll(".link")
.data(json.Links)
.enter().append("line")
.attr("class", "link");
var node = svg.selectAll(".node")
.data(json.Nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("class", "node")
.attr("r", 5);
node.append("svg:a")
.attr("xlink:href", function(d){return d.url;}).append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name })
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
});