[ Launch: force layout ] 5530308 by enjalot
-
-
Save enjalot/5530308 to your computer and use it in GitHub Desktop.
force layout
This file contains 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
{"description":"force layout","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":true,"loop":false,"restart":true,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/J84eWQY.png"} |
This file contains 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
/* | |
* | |
* working from: | |
* http://bl.ocks.org/mbostock/1748247 | |
*/ | |
var width = tributary.sw; | |
var height = tributary.sh; | |
var n = 42, | |
m = 10, | |
padding = 6, | |
radius = d3.scale.sqrt().range([0, 12]), | |
color = d3.scale.category10().domain(d3.range(m)); | |
if(tributary.force) { | |
tributary.force | |
.gravity(0.1) | |
.charge(2.22) | |
.friction(0.41762304) | |
} | |
tributary.init = function(g) { | |
tributary.nodes = d3.range(n).map(function() { | |
var i = Math.floor(Math.random() * m); | |
var v = (i + 1) / m * -Math.log(Math.random()); | |
var nx = 9 | |
var y = Math.floor(arguments[1]/nx) * height/nx; | |
var x = (arguments[1] % nx) * width/nx; | |
return { | |
//px: Math.random() * width, | |
//py: Math.random() * width, | |
px: x, | |
py: y, | |
x: x, | |
y: y, | |
radius: radius(v), | |
color: color(i) | |
}; | |
}); | |
tributary.force = d3.layout.force() | |
.size([width, height]) | |
.nodes(tributary.nodes) | |
.start() | |
//.stop(); | |
var circle = g.selectAll("circle") | |
.data(tributary.nodes) | |
.enter().append("circle") | |
.attr("r", function(d) { return d.radius; }) | |
.style("fill", function(d) { return d.color; }) | |
.call(tributary.force.drag) | |
.on("click", function(d) { | |
d.rsvp = !d.rsvp | |
}) | |
} | |
tributary.run = function(g,t) { | |
//force.alpha(0.01); | |
tributary.force.resume(); | |
tributary.force.tick() | |
g.selectAll("circle") | |
.each(gravity([0, 1.512])) | |
.each(collide(0.72)) | |
.each(eventer) | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
//console.log(force.nodes()[n/2]); | |
tributary.force.stop(); | |
} | |
function eventer(d,i) { | |
if(d.rsvp) { | |
d.y -= 2.5 | |
} | |
} | |
function gravity(g) { | |
return function(d) { | |
d.x += g[0] | |
d.y += g[1] | |
} | |
} | |
// Resolves collisions between d and all other circles. | |
function collide(alpha) { | |
var quadtree = d3.geom.quadtree(tributary.nodes); | |
return function(d) { | |
var r = d.radius + radius.domain()[1] + padding, | |
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.color !== quad.point.color) * padding; | |
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; | |
}); | |
}; | |
} | |
// Move d to be adjacent to the cluster node. | |
function cluster(alpha) { | |
var max = {}; | |
// Find the largest node for each cluster. | |
nodes.forEach(function(d) { | |
if (!(d.color in max) || (d.radius > max[d.color].radius)) { | |
max[d.color] = d; | |
} | |
}); | |
return function(d) { | |
var node = max[d.color], | |
l, | |
r, | |
x, | |
y, | |
k = 1, | |
i = -1; | |
// For cluster nodes, apply custom gravity. | |
if (node == d) { | |
node = {x: width / 2, y: height / 2, radius: -d.radius}; | |
k = .1 * Math.sqrt(d.radius); | |
} | |
x = d.x - node.x; | |
y = d.y - node.y; | |
l = Math.sqrt(x * x + y * y); | |
r = d.radius + node.radius; | |
if (l != r) { | |
l = (l - r) / l * alpha * k; | |
d.x -= x *= l; | |
d.y -= y *= l; | |
node.x += x; | |
node.y += y; | |
} | |
}; | |
} |
This file contains 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
circle { | |
stroke: #ffffff; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment