d3 force directed graph, links not being drawn

Cathal Cronin

I'm new to d3 and haven't much web frontend development experience. For a web application I have I'm trying to draw a force directed graph. I've been trying the last few hours to get it to work. I've been looking at lots of different code example and what I'm doing looks very similar. I eventually got nodes to draw but the links between the nodes don't show up and I was trying different things and nothing seems to work. I don't know why my code wouldn't draw the edges.

From printing the nodes and links to the console I saw that the nodes got extra attributes like the d3 docs had mentioned but the links never seem to get these attributes. Below is my javascript file and the JSON file. I reduced the JSON file to only 3 entries to try and make it easier to solve the problem.

var height = 1080;
var width = 1920;

var color = d3.scale.category20();

var force = d3.layout.force()
    .linkDistance(-120)
    .linkStrength(30)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);


d3.json("/static/javascript/language_data.json", function(data){

force
    .nodes(data.languages)
    .links(data.language_pairs)
    .start();

var link = svg.selectAll(".link")
  .data(data.language_pairs)
  .enter().append("line")
  .attr("class", "link");

var node = svg.selectAll(".node")
  .data(data.languages)
  .enter().append("circle")
  .attr("class", "node")
  .attr("r", 5)
  .style("fill", function(d) { return color(d.group); })
  .call(force.drag);

node.append("title")
.text(function(d) { return d.language; });

force.on("tick", function() {
  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; });
  });
});

Here is the JSON file:

From looking at few examples my understanding is that the source and target are index positions from the list of nodes.

{
   "languages":[
      {"language": "TypeScript", "group": 1},
      {"language": "Java",  "group": 2},
      {"language": "VHDL", "group": 3}
   ],
   "language_pairs":[
      {"source": "0", "target": "1", "value": 5},
      {"source": "1", "target": "2", "value": 5},
      {"source": "2", "target": "0", "value": 5}
   ]
}

Sorry if I left out anything! Thanks for any help!

Mark

Two issues:

1.) Your "language_pairs" source/target indexes are strings and not numbers. Get rid of the quotes:

"language_pairs":[
  {"source": 0, "target": 1, "value": 5},
  {"source": 1, "target": 2, "value": 5},
  {"source": 2, "target": 0, "value": 5}
]

2.) Your linkDistance and linkStrength parameters don't make sense:

var force = d3.layout.force()
    .linkDistance(-120) // negative distance? 
    .linkStrength(30) // according to the docs, this must be between 0 and 1?
    .size([width, height]);

Here's an example that fixes these problems.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding label to the links in D3 force directed graph

From Dev

D3 force directed graph direction

From Dev

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

From Dev

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

From Dev

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

From Dev

D3 Force directed Graph: Why don't the links appear and why is the simulation on the left?

From Dev

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

From Dev

Introducing Arrow(directed), in Force Directed Graph d3

From Dev

d3 force directed graph downward force simulation

From Dev

D3 force directed graph: Performance issue in a complex graph

From Dev

Iteratively appending links to a D3 force directed network visualization

From Dev

"Pinning" nodes in a D3 force-directed graph

From Dev

d3 force directed graph remove text cursor

From Dev

d3 force directed graph don't select text

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 moving text

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

D3js - Force directed graph - advanced highlighting of neigbour nodes and links, is it possible?

From Dev

D3v4 force directed graph - localStorage disconnects links and nodes

From Dev

d3.js force directed graph: How to make node size depends on the value of the links?

From Dev

D3 force graph rendering nodes and links

From Dev

d3 force graph using node names for links

From Dev

D3 force directed layout - changing node color and its links color on button click

From Dev

d3 force directed layout - drawing links without changing the location of the nodes

Related Related

  1. 1

    Adding label to the links in D3 force directed graph

  2. 2

    D3 force directed graph direction

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

    D3 Force directed Graph: Why don't the links appear and why is the simulation on the left?

  7. 7

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

  8. 8

    Introducing Arrow(directed), in Force Directed Graph d3

  9. 9

    d3 force directed graph downward force simulation

  10. 10

    D3 force directed graph: Performance issue in a complex graph

  11. 11

    Iteratively appending links to a D3 force directed network visualization

  12. 12

    "Pinning" nodes in a D3 force-directed graph

  13. 13

    d3 force directed graph remove text cursor

  14. 14

    d3 force directed graph don't select text

  15. 15

    semantic zooming of the force directed graph in d3

  16. 16

    modifying the d3 force-directed graph example

  17. 17

    D3 force directed graph moving text

  18. 18

    D3 Force Directed Graph ajax update

  19. 19

    D3 force-directed graph: update node position

  20. 20

    modifying the d3 force-directed graph example

  21. 21

    Load JSON object into D3 Force Directed Graph

  22. 22

    Dynamically change color of D3 Node (force directed graph)

  23. 23

    D3js - Force directed graph - advanced highlighting of neigbour nodes and links, is it possible?

  24. 24

    D3v4 force directed graph - localStorage disconnects links and nodes

  25. 25

    d3.js force directed graph: How to make node size depends on the value of the links?

  26. 26

    D3 force graph rendering nodes and links

  27. 27

    d3 force graph using node names for links

  28. 28

    D3 force directed layout - changing node color and its links color on button click

  29. 29

    d3 force directed layout - drawing links without changing the location of the nodes

HotTag

Archive