Skip to content

Instantly share code, notes, and snippets.



Last active Sep 13, 2018
What would you like to do?
force-directed distribution
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<script src=""></script>
<script src="//"></script>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; background-color:black; }
svg { background-color: black;}
const width = 1000
const height = 500
const svg ="body").append("svg")
.attr("width", width)
.attr("height", height)
.attr('transform', 'translate()')
const party =, {});
const colorScale = d3.scaleOrdinal()
.range(['#FCFCFC', 'coral', '#FFFAE3', '#99E1D9', 'skyblue'])
const radius = 7
const sampleData = d3.range(600).map(() => ({r: radius,
value: width/2 + d3.randomNormal(0,2.8)() * 50}))
// define force
let force = d3.forceSimulation()
//.force('charge', manyBody)
//.force('center', center)
.force('collision', d3.forceCollide(d => d.r+1).strength(1))
// .velocityDecay(.48)
.force('x', d3.forceX(d => d.value).strength(3))
.force('y', d3.forceY(height - radius).strength(0.05))
// .alphaDecay(.5)
// .alpha(1)
.on('tick', changeNetwork)
.style('fill', (d,i) => colorScale(i))
.attr('r', d => d.r)
.attr('stroke', 'white')
.attr('stroke-width', .1)
function changeNetwork() {
.attr('cx', d => d.x)
.attr('cy', d => d.y = Math.min(d.y, height - radius - 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.