简体   繁体   中英

How to append text in d3 force layout?

I am trying to add text in force layout. First i am creating a svg group and i am appending circle and text into it. The circle is working fine but the text is not working. 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; })

The text is of the screen because you've missed out the positioning methods. If you add this you'll see text attached to the nodes .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM