D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
bsuthersan
Full window
Github gist
Two groups of nodes
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script> var width = 960, height = 500, padding = 2, clusterPadding = 20, maxRadius = 12; var n = 1328, m = 2; var color = d3.scale.category10() .domain(d3.range(m)); var clusters = new Array(m); var nodes = d3.range(n).map(function() { var i = Math.floor(Math.random() * m), r = 3, d = { cluster: i, radius: r, x: Math.cos(i / m * 2 * Math.PI) * 200 + width / 2 + Math.random(), y: Math.sin(i / m * 2 * Math.PI) * 200 + height / 2 + Math.random() }; if (!clusters[i] || (r > clusters[i].radius)) clusters[i] = d; return d; }); var force = d3.layout.force() .nodes(nodes) .size([width, height]) .gravity(.02) .charge(0) .on("tick", tick) .start(); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); var node = svg.selectAll("circle") .data(nodes) .enter().append("circle") .style("fill", function(d) { return color(d.cluster); }) .call(force.drag); node.transition() .duration(100) .delay(function(d, i) { return i * 5; }) .attrTween("r", function(d) { var i = d3.interpolate(0, d.radius); return function(t) { return d.radius = i(t); }; }); function tick(e) { node .each(cluster(10 * e.alpha * e.alpha)) .each(collide(.5)) .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); } function cluster(alpha) { return function(d) { var cluster = clusters[d.cluster]; if (cluster === d) return; var x = d.x - cluster.x, y = d.y - cluster.y, l = Math.sqrt(x * x + y * y), r = d.radius + cluster.radius; if (l != r) { l = (l - r) / l * alpha; d.x -= x *= l; d.y -= y *= l; cluster.x += x; cluster.y += y; } }; } svg.append("text") .attr("x", 801) .attr("y", 79) .attr("text-anchor", "middle") .style("font-size", "10px") .style("font-family","sans-serif") .text("Nodes which are not orange") svg.append("rect") .attr("x", 725) .attr("y", 70) .attr("width", 10) .attr("height",10) .style("fill","#006699") svg.append("rect") .attr("x", 725) .attr("y", 90) .attr("width", 10) .attr("height",10) .style("fill","orange") svg.append("text") .attr("x", 795) .attr("y", 99) .attr("text-anchor", "middle") .style("font-size", "10px") .style("font-family","sans-serif") .text("Nodes which are not blue") function collide(alpha) { var quadtree = d3.geom.quadtree(nodes); return function(d) { var r = d.radius + maxRadius + Math.max(padding, clusterPadding), nx1 = d.x - r, nx2 = d.x + r, ny1 = d.y - r, ny2 = d.y + r; quadtree.visit(function(quad, x1, y1, x2, y2) { if (quad.point && (quad.point !== d)) { var x = d.x - quad.point.x, y = d.y - quad.point.y, l = Math.sqrt(x * x + y * y), r = d.radius + quad.point.radius + (d.cluster === quad.point.cluster ? padding : clusterPadding); if (l < r) { l = (l - r) / l * alpha; d.x -= x *= l; d.y -= y *= l; quad.point.x += x; quad.point.y += y; } } return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1; }); }; } </script>
https://d3js.org/d3.v3.min.js