How to add text to d3 force layout?

I am trying to add text to a power layout. First I create an svg group and I add circle and text to it. The circle works fine, but the text doesn't work. Here is the code

var node = svg.selectAll("g")
                    .data(measures.nodes)
                    .enter().append("g")
                    .attr("class", "node")
                    .call(node_drag);
var circle = node.append("circle")
                    .attr("fill", "blue")
                    .attr("r",5)
                    .attr("dx", ".10em")
                    .attr("dy", ".10em");

var text = node.append("text")
                .data(measures.nodes)
                .attr("color", "blue")
                .text(function(d){ return d.name; })

      

+3


source to share


1 answer


Screen text because you missed positioning methods. If you add this, you will see text attached to the nodes .



        text.attr("x", function(d) { return d.x; })
        .attr("y", function(d) { return d.y; });

      

+1


source







All Articles