D3 force directed graph direction
<p>I'm new to D3, and am trying to make a graph similar to this <a href="http://bl.ocks.org/mbostock/2675ff61ea5e063ede2b5d63c08020c7" rel="nofollow">example</a> 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. </p>

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

<pre><code>"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; })) &lt;-- where is this from?
    .force("charge", d3.forceManyBody())
    .force("center", d3.forceCenter(width / 2, height / 2));
</code></pre>

<p>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. </p>

<ol>
<li><p>Could someone clarify about the example id thing? </p></li>
<li><p>What does my error mean?</p></li>
</ol>

<p>Any help would be appreciated. </p>

<p>My code;</p>

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

});
</code></pre>

<p>Data: </p>

<pre><code>{
 "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 &lt;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}

 ]
 }
</code></pre>