D3 force directed graph direction

damtypo

I'm new to D3, and am trying to make a graph similar to this example but a few things confuse me. I'm unsure when the example is referring to things built in to D3, or just the data set they are using.

Like here, I'm not sure about the id in d.id. as the example's data looks like this, which makes me think id is from the data. Or does it represent an index value?

"nodes": [
    {"id": "Myriel", "group": 1},
    {"id": "Napoleon", "group": 1}
         ],
            // etc
"links": [
    {"source": "Napoleon", "target": "Myriel", "value": 1},
    {"source": "Mlle.Baptistine", "target": "Myriel", "value": 8},
    {"source": "Mme.Magloire", "target": "Myriel", "value": 10},
          //etc

var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.id; })) <-- where is this from?
    .force("charge", d3.forceManyBody())
    .force("center", d3.forceCenter(width / 2, height / 2));

SO! In my code I get an error repeating thousands of times, 'Uncaught Error: missing: X', where X is the value of the first source in my links array. I can console.log my data and it looks fine, and the elements are rendering to the DOM, but all bunched up to the top left of the SVG. I don't know what is wrong. I guess I have 2 questions.

  1. Could someone clarify about the example id thing?

  2. What does my error mean?

Any help would be appreciated.

My code;

var graph

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


d3.json("./mock.json", function(json) {
  var graph = json
      console.log(graph);
      console.log(graph.nodes);

      var simulation = d3.forceSimulation()
      .force("link", d3.forceLink(graph.links))
      .force("charge", d3.forceManyBody())
      .force("center", d3.forceCenter(width/2, height/2));

  var link = svg.append("g")
    .attr("class", "links")
    .selectAll("line")
    .data(graph.links)
    .enter().append("line")
    //.attr("stroke-width", function(d) {return Math.sqrt(d.value); });

  var node = svg.append("g")
    .attr("class", "nodes")
    .selectAll("circle")
    .data(graph.nodes)
    .enter().append("circle")
      .attr("r", 5)
      //.attr("fill", function(d) { return color(d.id); })
      .call(d3.drag()
        .on("start", dragstarted)
        .on("drag", dragged)
        .on("end", dragended));

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

    simulation
      .nodes(graph.nodes)
      .on("tick", ticked);

    simulation.force("link")
      .links(graph.links);

})

function ticked() {

  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; });
}


function dragstarted(d) {
    if(!d3.event.active) simulation.alphaTarget(0.3).restart();
    d.fx = d.x;
    d.fy = d.y;
}
function dragged(d) {
    d.fx = d3.event.x;
    d.fy = d3.event.y;
}
function dragended(d) {
    if(!d3.event.active) simulation.alphaTarget(0);
    d.fx = null;
    d.fy = null;
}

});

Data:

{
 "nodes":[
         {"reference":5, "year": 0, "text":"The amount of time spent on video gaming is related negatively to academic achievement", "tags":["Academic disturbance"]},
         {"reference":5, "year": 0, "text":"Digital addiction ranges from <1% and 38%", "tags":["Addiction"]},
         {"reference":58, "year": 0, "text":"Patological video game play impacts negativelly academic achievement", "tags":["Addiction"]},
         {"reference":77, "year": 2009, "text":"74% of adults have Internet access at home", "tags":["Adults"]},
         {"reference":64, "year": 0, "text":"Apathetic users spend short times on web pages, follow no logical order, and make random selections", "tags":["Apathetic hypertext users3"]},
         {"reference":8, "year": 0, "text":"49.8% of sessions are shorter than 5 seconds", "tags":["App usage"]}
       ],
 "links": [
       {"source":0,"target":2},
       {"source":0,"target":6},
       {"source":1,"target":6},
       {"source":1,"target":3},
       {"source":1,"target":2}

 ]
 }
Terry

There are two issues with your code:

  1. The index of 6, see in your links array, is not available in the nodes array. nodes has a length of 6, which means the largest index is 5. This causes an error even with the correct code. I have change 6 to 5 in my working example below, and I believe that is what you want.

  2. Since the links are based on the index, you can simply return the index instead of the ID, i.e. d3.forceLink().id(function(d,i) { return i; }.

Here is the proof-of-function example:

var graph

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

var json = {
  "nodes": [{
    "reference": 5,
    "year": 0,
    "text": "The amount of time spent on video gaming is related negatively to academic achievement",
    "tags": ["Academic disturbance"]
  }, {
    "reference": 5,
    "year": 0,
    "text": "Digital addiction ranges from <1% and 38%",
    "tags": ["Addiction"]
  }, {
    "reference": 58,
    "year": 0,
    "text": "Patological video game play impacts negativelly academic achievement",
    "tags": ["Addiction"]
  }, {
    "reference": 77,
    "year": 2009,
    "text": "74% of adults have Internet access at home",
    "tags": ["Adults"]
  }, {
    "reference": 64,
    "year": 0,
    "text": "Apathetic users spend short times on web pages, follow no logical order, and make random selections",
    "tags": ["Apathetic hypertext users3"]
  }, {
    "reference": 8,
    "year": 0,
    "text": "49.8% of sessions are shorter than 5 seconds",
    "tags": ["App usage"]
  }],
  "links": [{
      "source": 0,
      "target": 2
    }, {
      "source": 0,
      "target": 5
    }, {
      "source": 1,
      "target": 5
    }, {
      "source": 1,
      "target": 3
    }, {
      "source": 1,
      "target": 2
    }

  ]
};

var graph = json;

var simulation = d3.forceSimulation()
  .force("link", d3.forceLink().id(function(d, i) {
    return i;
  }))
  .force("charge", d3.forceManyBody())
  .force("center", d3.forceCenter(width / 2, height / 2));

var link = svg.append("g")
  .attr("class", "links")
  .selectAll("line")
  .data(graph.links)
  .enter().append("line")
  //.attr("stroke-width", function(d) {return Math.sqrt(d.value); });

var node = svg.append("g")
  .attr("class", "nodes")
  .selectAll("circle")
  .data(graph.nodes)
  .enter().append("circle")
  .attr("r", 5)
  //.attr("fill", function(d) { return color(d.id); })
  .call(d3.drag()
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended));

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

simulation
  .nodes(graph.nodes)
  .on("tick", ticked);

simulation.force("link")
  .links(graph.links);


function ticked() {

  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;
    });
}


function dragstarted(d) {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function dragged(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}
.links line {
  stroke: #aaa;
}

.nodes circle {
  pointer-events: all;
  stroke: none;
  stroke-width: 40px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js"></script>
<svg width="500" height="200"></svg>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

"Pinning" nodes in a D3 force-directed graph

From Dev

Adding label to the links in 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, links not being drawn

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

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

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

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

    Introducing Arrow(directed), in Force Directed Graph d3

  2. 2

    d3 force directed graph downward force simulation

  3. 3

    D3 force directed graph: Performance issue in a complex graph

  4. 4

    "Pinning" nodes in a D3 force-directed graph

  5. 5

    Adding label to the links in D3 force directed graph

  6. 6

    d3 force directed graph remove text cursor

  7. 7

    d3 force directed graph don't select text

  8. 8

    semantic zooming of the force directed graph in d3

  9. 9

    modifying the d3 force-directed graph example

  10. 10

    d3 force directed graph, links not being drawn

  11. 11

    D3 force directed graph moving text

  12. 12

    D3 Force Directed Graph ajax update

  13. 13

    D3 force-directed graph: update node position

  14. 14

    modifying the d3 force-directed graph example

  15. 15

    Load JSON object into D3 Force Directed Graph

  16. 16

    Dynamically change color of D3 Node (force directed graph)

  17. 17

    JUNG force directed graph

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

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

  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