xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var margin = {top:20, bottom:20, left:20, right:20};
var width = 600 - margin.left - margin.right;
var height = 400 - margin.top - margin.bottom;
var radius = 25;
var colors = d3.scaleOrdinal(d3.schemeCategory10);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
d3.json('miserables.json', function(err,data){
data.nodes.forEach(function(node){
var perRow = 4
node.focusX = width/2;
node.focusY = height/2;
});
// console.log(data.links)
var simulation = d3.forceSimulation(data.nodes)
.force('center', d3.forceCenter(width/2, height/2))
.force('collide', d3.forceCollide(radius+45))
.force('y', d3.forceY(height/2))
.force('links', d3.forceLink(data.links).id(function(d){return d.id}))
.on('tick', ticked);
var links = svg.selectAll("line")
.data(data.links)
.enter().append('line')
.attr('stroke','#ccc');
var circles = svg.selectAll("circle")
.data(data.nodes, function(d){return d.id})
.enter().append("circle")
.attr("r", radius*2.5)
function ticked(){
circles.attr('cx', function(d) {return d.x})
.attr('cy', function (d) {return d.y})
links.attr('x1', function(d){return d.source.x})
.attr('x2', function(d) {return d.target.x})
.attr('y1', function(d) {return d.source.y})
.attr('y2', function(d) {return d.target.y})
}
});
</script>
</body>
https://d3js.org/d3.v4.min.js