D3.js Titles on Collapsible Force-Directed graph

后端 未结 2 647
天涯浪人
天涯浪人 2020-12-18 08:43

So i\'ve been working on a collapsible force-directed graph based of the following example.

Im trying to advance from that and add titles to each node. I\'ve followe

2条回答
  •  猫巷女王i
    2020-12-18 09:40

    Add titles to node like this.

    title = svg.selectAll("text.title")    
         .data(nodes);
    
    title.enter()
        .append("text") //In your code you used title instead of text
        .attr("class", "title")
        .text(function(d) { return d.name; });
    
    title.exit().remove();
    

    Note that titles should be appended after circle nodes. Or else titles may cut off.

    Also update the position of title in tick function.

     title.attr("transform", function(d){ return "translate("+d.x+","+d.y+")"; });
    

    Here is the jsfiddle

提交回复
热议问题