D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
houseofaragon
Full window
Github gist
immigration
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <div class='main-viz'> <h1>Immigration</h1> <canvas width="960" height="760"></canvas> </div> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var canvas = document.querySelector("canvas"), context = canvas.getContext("2d"), width = canvas.width, height = canvas.height, tau = 2 * Math.PI; var nodes = d3.range(1000).map(function(i) { return { r: 3, index: i, color: i < 10 ? "brown" : "#dddddd" }; }); var simulation = d3.forceSimulation(nodes) .velocityDecay(0.2) .force("x", d3.forceX().strength(0.001)) .force("y", d3.forceY().strength(0.1)) .force("brown", isolate(d3.forceX(width/8), function(d) { return d.color === 'brown'; })) .force("#dddddd", isolate(d3.forceX(-width/8), function(d) { return d.color === '#dddddd'; })) .force("collide", d3.forceCollide().radius(function(d) { return d.r + 5; }).iterations(5)) .on("tick", ticked); function ticked(d) { context.clearRect(0, 0, width, height); context.save(); context.translate(width / 2, height / 2); nodes.forEach(drawNode); context.restore(); } function drawNode(d) { context.beginPath(); context.moveTo(d.x + d.r, d.y); context.arc(d.x, d.y, d.r, 0, tau); context.fillStyle = d.color; context.fill(); } function isolate(force, filter) { console.log(force,filter); var initialize = force.initialize; force.initialize = function() { initialize.call(force, nodes.filter(filter)); }; return force; } </script>
https://d3js.org/d3.v4.min.js