varnodes=d3.select("#nodes")//select the #nodes <g>
.selectAll("circle")
.selectAll("circle")//select all circles within it (even if none exist)
.data(nodesData)
.data(nodesData)//attach data to this selection
.enter()
.enter()//find all data elements not matched to circles
.append("circle");
.append("circle");//append a circle for each unmatched data element
//repeat what you do for nodes, but with lines, the #links <g>, and the linksData array
varlinks=d3.select("#links")
varlinks=d3.select("#links")
.selectAll("line")
.selectAll("line")
.data(linksData)
.data(linksData)
.enter()
.enter()
.append("line");
.append("line");
d3.forceSimulation()
d3.forceSimulation()//create a force simulation
.nodes(nodesData)// add this line
.nodes(nodesData)//set the nodes for the simulation, based on the nodesData array
.force("center_force",d3.forceCenter(WIDTH/2,HEIGHT/2))//create a centering force in the middle of the svg
d3.forceSimulation()
.force("charge_force",d3.forceManyBody())//create a charge force, so the nodes repel each other
.nodes(nodesData)
.force("links",d3.forceLink(linksData).id(function(datum){//create a links so the nodes aren't repelled too far from nodes they're linked to in the linkData array
.force("center_force",d3.forceCenter(WIDTH/2,HEIGHT/2))// add this line
returndatum.name//create the links by associating each link's source/target with a name property from the nodesData array
.force("charge_force",d3.forceManyBody())//add this line
}).distance(160))//set the distance of the link
.force("links",d3.forceLink(linksData).id(function(datum){//add this
.on("tick",function(){//create a function that will be called with each "tick" of the simulation
returndatum.name//add this
//with each tick...
}).distance(160))//add this
//update the circle cx/cy values based on the x/y values d3 added to the nodes data
.on("tick",function(){
nodes.attr("cx",function(datum){returndatum.x;})
nodes.attr("cx",function(datum){returndatum.x;})
.attr("cy",function(datum){returndatum.y;});
.attr("cy",function(datum){returndatum.y;});
//update the line x1/y1/x2/y2 values based on the source/target x/y values d3 added to the links data