score:1

Accepted answer

Look at the objects in your links array:

[{"source": "0", "target": "1", "value": 1},...
//this is a string ---------^

You don't have numbers here, just strings. For the force to work, you have to set the number of the source and the target.

A simple solution is coercing:

links.forEach(function(d) {
    d.source = +d.source;
    d.target = +d.target;
})

Here is the resulting code:

var nodes = [{
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxxn"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}, {
  "name": "xxx"
}];
var links = [{
  "source": "0",
  "target": "1",
  "value": 1
}, {
  "source": "2",
  "target": "1",
  "value": 1
}, {
  "source": "3",
  "target": "1",
  "value": 1
}, {
  "source": "4",
  "target": "1",
  "value": 1
}, {
  "source": "5",
  "target": "1",
  "value": 1
}, {
  "source": "6",
  "target": "1",
  "value": 1
}, {
  "source": "7",
  "target": "1",
  "value": 1
}, {
  "source": "8",
  "target": "1",
  "value": 1
}, {
  "source": "9",
  "target": "1",
  "value": 1
}, {
  "source": "10",
  "target": "1",
  "value": 1
}, {
  "source": "11",
  "target": "1",
  "value": 1
}, {
  "source": "12",
  "target": "1",
  "value": 1
}, {
  "source": "13",
  "target": "1",
  "value": 1
}, {
  "source": "14",
  "target": "1",
  "value": 1
}, {
  "source": "1",
  "target": "15",
  "value": 1
}, {
  "source": "1",
  "target": "16",
  "value": 1
}, {
  "source": "1",
  "target": "17",
  "value": 1
}, {
  "source": "1",
  "target": "18",
  "value": 1
}, {
  "source": "1",
  "target": "19",
  "value": 1
}, {
  "source": "1",
  "target": "20",
  "value": 1
}, {
  "source": "1",
  "target": "21",
  "value": 1
}, {
  "source": "1",
  "target": "22",
  "value": 1
}, {
  "source": "1",
  "target": "23",
  "value": 1
}, {
  "source": "1",
  "target": "24",
  "value": 1
}, {
  "source": "1",
  "target": "25",
  "value": 1
}, {
  "source": "1",
  "target": "26",
  "value": 1
}, {
  "source": "1",
  "target": "27",
  "value": 1
}, {
  "source": "1",
  "target": "28",
  "value": 1
}, {
  "source": "1",
  "target": "29",
  "value": 1
}, {
  "source": "1",
  "target": "30",
  "value": 1
}, {
  "source": "1",
  "target": "31",
  "value": 1
}, {
  "source": "1",
  "target": "32",
  "value": 1
}, {
  "source": "1",
  "target": "33",
  "value": 1
}, {
  "source": "1",
  "target": "34",
  "value": 1
}, {
  "source": "1",
  "target": "35",
  "value": 1
}, {
  "source": "1",
  "target": "36",
  "value": 1
}];
links.forEach(function(d) {
  d.source = +d.source;
  d.target = +d.target;
})
var width = 400,
  height = 400;
var svg = d3.select('body').append('svg')
  .attr('width', width)
  .attr('height', height);

var force = d3.layout.force()
  .size([width, height])
  .nodes(nodes)
  .links(links);

force.linkDistance(width / 2);

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

var node = svg.selectAll('.node')
  .data(nodes)
  .enter().append('circle')
  .attr('class', 'node');
  
force.on("start", function(){
  svg.append("text")
    .attr("class", "text")
    .attr("x", 150)
    .attr("y", 80)
    .text("Calculating...");
});

force.on('end', function() {
  svg.select(".text").remove();
  node.attr('r', width / 25)
    .attr('cx', function(d) {
      return d.x;
    })
    .attr('cy', function(d) {
      return d.y;
    });
  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;
    });

});
force.start();
<script src='https://d3js.org/d3.v3.min.js'></script>

EDIT: Since you're using .on("end" to paint your nodes, you'll have to wait until the force finishes.


Related Query