Created
September 28, 2011 23:19
-
-
Save bstaats/1249554 to your computer and use it in GitHub Desktop.
Force Layouts - Multiple Foci Collision
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> | |
<title>Force Layouts - Multiple Foci Collision</title> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js"></script> | |
<style type="text/css"> | |
rect{ | |
fill: white; | |
} | |
circle { | |
stroke: #000; | |
stroke-opacity: .5; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="body"> | |
<div id="chart"></div> | |
</div> | |
<script type="text/javascript"> | |
var w = 960, | |
h = 500; | |
var nodes = d3.range(5).map(function(i) { | |
return {type: Math.random() * 5 | 0, | |
radius: 30, | |
fixed:true, | |
type:i, | |
x: (i+1) * (w / 6), | |
y: h / 2}; | |
}), | |
color = d3.scale.category10(); | |
var force = d3.layout.force() | |
.gravity(0) | |
.charge(0) | |
.nodes(nodes) | |
.size([w, h]); | |
force.start(); | |
var svg = d3.select("#chart").append("svg:svg") | |
.attr("width", w) | |
.attr("height", h); | |
svg.append("svg:rect") | |
.attr("width", w) | |
.attr("height", h); | |
svg.selectAll("circle") | |
.data(nodes) | |
.enter().append("svg:circle") | |
.attr("r", function(d) { return d.radius - 2; }) | |
.style("fill", function(d, i) { return color(d.type); }); | |
force.on("tick", function(e) { | |
var q = d3.geom.quadtree(nodes), | |
k = e.alpha * .1, | |
i = 0, | |
n = nodes.length, | |
o; | |
while (++i < n) { | |
o = nodes[i]; | |
if (o.fixed) continue; | |
c = nodes[o.type]; | |
o.x += (c.x - o.x) * k; | |
o.y += (c.y - o.y) * k; | |
q.visit(collide(o)); | |
} | |
svg.selectAll("circle") | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
}); | |
var p0; | |
svg.on("mousemove", function() { | |
var p1 = d3.svg.mouse(this), | |
node = {radius:Math.random() * 12 + 4, type: Math.random() * 5 | 0, x: p1[0], y: p1[1], px: (p0 || (p0 = p1))[0], py: p0[1]}; | |
p0 = p1; | |
svg.append("svg:circle") | |
.data([node]) | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }) | |
.attr("r", function(d) { return d.radius - 2; }) | |
.style("fill", function(d) {return color(d.type);}) | |
.transition() | |
.delay(3000) | |
.attr("r", 1e-6) | |
.each("end", function() { nodes.splice(6, 1); }) | |
.remove(); | |
nodes.push(node); | |
force.resume(); | |
}); | |
function collide(node) { | |
var r = node.radius + 16, | |
nx1 = node.x - r, | |
nx2 = node.x + r, | |
ny1 = node.y - r, | |
ny2 = node.y + r; | |
return function(quad, x1, y1, x2, y2) { | |
if (quad.point && (quad.point !== node)) { | |
var x = node.x - quad.point.x, | |
y = node.y - quad.point.y, | |
l = Math.sqrt(x * x + y * y), | |
r = node.radius + quad.point.radius; | |
if (l < r) { | |
l = (l - r) / l * .5; | |
node.px += x * l; | |
node.py += y * l; | |
} | |
} | |
return x1 > nx2 | |
|| x2 < nx1 | |
|| y1 > ny2 | |
|| y2 < ny1; | |
}; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this tidy example! I've had so much fun forking and tinkering with it :-) (http://bl.ocks.org/1306938)