score:10

Accepted answer

The problem you're having is that your current code is appending <text> to a <circle>, which won't show up. Instead, you need to create a group <g> for each node that contains both the <circle> and the <text>. Try this:

var nodes = svg.selectAll("g")
    .data(dataset.nodes).enter()
    .append("g");

nodes.append("circle")
    .attr("r", 10)
    .style("fill", function (d, i) { return colors(i); })
    .call(force.drag);

nodes.append("text")
    .attr("x", 12)
    .attr("dy", ".35em")
    .text(function (d) { return d.name; });

Updated JSFiddle here.


Related Query

More Query from same tag