D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jwilber
Full window
Github gist
force-directed distribution
Built with
blockbuilder.org
<!DOCTYPE html> <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; background-color:black; } svg { background-color: black;} </style> </head> <body> <script> const width = 1000 const height = 500 const svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append('g') .attr('transform', 'translate()') 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('collision', d3.forceCollide(d => d.r+1).strength(1)) .force('x', d3.forceX(d => d.value).strength(3)) .force('y', d3.forceY(height - radius).strength(0.05)) .nodes(sampleData) .on('tick', changeNetwork) svg.selectAll('circle') .data(sampleData) .enter() .append('circle') .style('fill', (d,i) => colorScale(i)) .attr('r', d => d.r) .attr('stroke', 'white') .attr('stroke-width', .1) function changeNetwork() { d3.selectAll('circle') .attr('cx', d => d.x) .attr('cy', d => d.y = Math.min(d.y, height - radius - 1)) } </script> </body>
https://d3js.org/d3.v4.min.js