D3 force directed graph moving text

GeoGyro

I'm want to add moving labels on nodes in a D3 force directed graph (example : http://mbostock.github.io/d3/talk/20111116/force-collapsible.html). I don't have problem to add some text (nodes names defined in my data.json), but is X/Y position is fixed to the initial nodes X/Y position. I try transform and translate functions but without success. Here is a sample of my code :

// Update the nodes…
node = vis.selectAll("circle.node")
    .data(nodes, function(d) { return d.id; })
    .style("fill", color);    

node.transition()
    .attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; });

// Labels   
node.enter().append("svg:text")     
    .attr("dx", function(d) { return d.x; })
    .attr("dy", function(d) { return d.y; })        
    .attr("fill", "#ffffff")
    .attr('text-anchor', 'center')
    .text(function(d) { return d.name });   

// Enter any new nodes.
node.enter().append("svg:circle")
    .attr("class", "node")    
    .attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; })
    .attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; })
    .style("fill", color)
    .on("click", click)  
    .call(force.drag);      

// Exit any old nodes.
node.exit().remove();   
} 

function tick() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; }); 


    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });

}   
Cyril Cherian

You will first have to label selection like this

text = vis.selectAll("text.label")    
     .data(nodes);

   // Enter any new label
    text.enter()
        .append("text")
        .attr("class", "label")
        .text(function(d) { return d.name; });
  //remove text for collapsed nodes.
  text.exit().remove();  

Then in the ticks function handle the text move (This is what you were missing as a result of which your text was coming fixed)

function tick() {
  link.attr("x1", function(d) { return d.source.x; })
      .attr("y1", function(d) { return d.source.y; })
      .attr("x2", function(d) { return d.target.x; })
      .attr("y2", function(d) { return d.target.y; });

  node.attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; });
  //this will handle the label move
  text.attr("transform", function(d){ return "translate("+d.x+","+d.y+")"; });    
}

working example here

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

d3 force directed graph remove text cursor

From Dev

d3 force directed graph don't select text

From Dev

D3 force directed graph direction

From Dev

Introducing Arrow(directed), in Force Directed Graph d3

From Dev

D3 force directed graph node - text is being duplicated when expading

From Dev

d3 force directed graph downward force simulation

From Dev

D3 force directed graph: Performance issue in a complex graph

From Dev

"Pinning" nodes in a D3 force-directed graph

From Dev

Adding label to the links in D3 force directed graph

From Dev

semantic zooming of the force directed graph in d3

From Dev

modifying the d3 force-directed graph example

From Dev

d3 force directed graph, links not being drawn

From Dev

D3 Force Directed Graph ajax update

From Dev

D3 force-directed graph: update node position

From Dev

modifying the d3 force-directed graph example

From Dev

Load JSON object into D3 Force Directed Graph

From Dev

Dynamically change color of D3 Node (force directed graph)

From Dev

JUNG force directed graph

From Dev

Having trouble converting a D3 v3 Force Directed graph into D3 v4 library implementation?

From Dev

How to add a dynamic legend to a D3 force directed graph in Apex?

From Dev

How to draw a simple Force-directed graph in D3 Javascript

From Dev

The links between the nodes in Force-Directed Graph in D3 Javascript

From Dev

How to restrict number of nodes initially in d3 Force directed graph

From Dev

How to render links as elbow connectors in d3 force directed graph

From Dev

How to make it so D3 Force Directed Graph generates nodes in same position each time

From Dev

Unable to Delete links from a D3 force directed graph with arrows

From Dev

Collapsible D3 force directed graph with non-tree data

From Dev

How to choose which JSON file to use in your D3 force directed graph

From Dev

D3 Force Directed Graph: why don't the flags appear?

Related Related

  1. 1

    d3 force directed graph remove text cursor

  2. 2

    d3 force directed graph don't select text

  3. 3

    D3 force directed graph direction

  4. 4

    Introducing Arrow(directed), in Force Directed Graph d3

  5. 5

    D3 force directed graph node - text is being duplicated when expading

  6. 6

    d3 force directed graph downward force simulation

  7. 7

    D3 force directed graph: Performance issue in a complex graph

  8. 8

    "Pinning" nodes in a D3 force-directed graph

  9. 9

    Adding label to the links in D3 force directed graph

  10. 10

    semantic zooming of the force directed graph in d3

  11. 11

    modifying the d3 force-directed graph example

  12. 12

    d3 force directed graph, links not being drawn

  13. 13

    D3 Force Directed Graph ajax update

  14. 14

    D3 force-directed graph: update node position

  15. 15

    modifying the d3 force-directed graph example

  16. 16

    Load JSON object into D3 Force Directed Graph

  17. 17

    Dynamically change color of D3 Node (force directed graph)

  18. 18

    JUNG force directed graph

  19. 19

    Having trouble converting a D3 v3 Force Directed graph into D3 v4 library implementation?

  20. 20

    How to add a dynamic legend to a D3 force directed graph in Apex?

  21. 21

    How to draw a simple Force-directed graph in D3 Javascript

  22. 22

    The links between the nodes in Force-Directed Graph in D3 Javascript

  23. 23

    How to restrict number of nodes initially in d3 Force directed graph

  24. 24

    How to render links as elbow connectors in d3 force directed graph

  25. 25

    How to make it so D3 Force Directed Graph generates nodes in same position each time

  26. 26

    Unable to Delete links from a D3 force directed graph with arrows

  27. 27

    Collapsible D3 force directed graph with non-tree data

  28. 28

    How to choose which JSON file to use in your D3 force directed graph

  29. 29

    D3 Force Directed Graph: why don't the flags appear?

HotTag

Archive