I want to make an SVG group to store a node and text in it. My generated HTML looks like this:
<g class="node" cx="946.6911642155153" cy="434.4579018064595">
<circle class="circle" r="4.5" />
<text dx="12" dy="0.35em">Harry Potter</text>
</g>
Nevertheless, the nodes and the text are stuck in the top left corner. I tried to build it from this question on SO.
My code looks like this:
function draw() {
node = svg.selectAll(".node");
node = node.data(force.nodes(), function(d) { return d.id;});
var gnode = node.enter().append('g').attr("class", "node");
gnode.append("circle").attr("class", "circle").attr("r", 4.5);
gnode.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });
force.start();
}
A working demo can be found here. I suspect that I have to push the gnode
to the force.nodes()
, but I am not a 100% sure.