Minimal force layout example.
From D3 in Depth book by Peter Cook.
license: gpl-3.0 | |
height: 320 | |
border: no |
Minimal force layout example.
From D3 in Depth book by Peter Cook.
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<head> | |
<title>Force layout</title> | |
</head> | |
<style> | |
circle { | |
fill: cadetblue; | |
} | |
</style> | |
<body> | |
<div id="content"> | |
<svg width="300" height="300"> | |
</svg> | |
</div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> | |
<script> | |
var width = 300, height = 300 | |
var nodes = [{text: 'Mike'}, {text:'John'}, {text:'Paul'}, {text:'Ringo'}, {text:'George'}] | |
var simulation = d3.forceSimulation(nodes) | |
.force('charge', d3.forceManyBody().strength(-10)) | |
.force('center', d3.forceCenter(width / 2, height / 3)) | |
.on('tick', ticked); | |
function ticked() { | |
var u = d3.select('svg') | |
.selectAll('text') | |
.data(nodes) | |
u.enter() | |
.append('text') | |
// .attr('r', 5) | |
.merge(u) | |
.attr('x', function(d) { | |
return d.x | |
}) | |
.attr('y', function(d) { | |
return d.y | |
}) | |
.text(function(d) {return d.text}) | |
u.exit().remove() | |
} | |
</script> | |
</body> | |
</html> |
�PNG | |