Original: http://mbostock.github.com/d3/ex/force.html
In his example as provided in the above link, I'm simply trying to replace the circle nodes with their names instead. I don't know much about D3 nor js/jquery but I'm trying to figure how it works.
I was able to replace the nodes with svg:text but when I do, they just "spawn" wherever they start and they don't animate.
I don't know if I should use groups here. If I do, teach me how.
So far, this is my modified code:
<div id="chart">
</div>
<script type="text/javascript">
<!--
var w = 960,
h = 500,
fill = d3.scale.category20();
var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);
d3.json("http://fourthdraft.com/ext/dataviz/miserables.json", function(json) {
var force = d3.layout.force()
.charge(-120)
.linkDistance(70)
.nodes(json.nodes)
.links(json.links)
.size([w, h])
.start();
var link = vis.selectAll("line.link")
.data(json.links)
.enter().append("svg:line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); })
.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; });
var node = vis.selectAll("circle.node")
.data(json.nodes)
.enter().append("svg:text")
.attr("class", "node")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.text(function(d) { return d.name; })
.style("fill", function(d) { return fill(d.group); })
.call(force.drag);
node.append("svg:title")
.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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
//-->
</script>
See this fiddle: http://jsfiddle.net/nrabinowitz/QMKm3/6/
The main issue with your code above is that you correctly changed the cx
and cy
attributes (which are specific to the svg:circle
element) to x
and y
in the part where you appended the svg:text
elements, but you didn't change them in the tick
handler, which is where the iterative layout update happens:
force.on("tick", function() {
// snip
node.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; });
});
You also should change the selection-and-appending from
var node = vis.selectAll("circle.node")
.data(json.nodes)
.enter().append("svg:text")
to
var node = vis.selectAll("text.node")
.data(json.nodes)
.enter().append("svg:text")
Even though I don't think this makes any difference in the context of this code, it will eventually trip you up - in D3, as explained here, you generally use the pattern "select with a selector, add missing nodes that match this selector, remove extra nodes that match this selector". In your code, the selector and the nodes you add don't match, which is a conceptual issue even if it doesn't have any ramifications for your code as written. (I should note that I still find this pattern a bit confusing and weird. But at a minimum, following it will make your code more legible to other developers.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With