Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created May 7, 2013 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/5530496 to your computer and use it in GitHub Desktop.
Save enjalot/5530496 to your computer and use it in GitHub Desktop.
force layout lamp
{"description":"force layout lamp","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}
/*
*
* working from:
* http://bl.ocks.org/mbostock/1748247
*/
var width = tributary.sw;
var height = tributary.sh;
var radius = d3.scale.sqrt().range([0, 12])
var padding = 6;
var xyGravity = [0, 1.5];
if(tributary.force) {
tributary.force
.gravity(0.1968)
.charge(2.22)
.friction(0.41762304)
}
tributary.init = function(g) {
var n = tributary.n = 100;
m = 10;
var color = d3.scale.category10().domain(d3.range(m));
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
})
}
var time = 0;
tributary.run = function(g,t) {
//force.alpha(0.01);
tributary.force.resume();
tributary.force.tick()
if(Math.floor(time) % 8 === 0) {
var i = Math.sin(time * Math.PI) * (tributary.n-1);
i = Math.abs(Math.floor(i));
if(i < tributary.nodes.length)
tributary.nodes[i].rsvp = !tributary.nodes[i].rsvp;
}
g.selectAll("circle")
.each(gravity(xyGravity))
.each(collide(0.1))
.each(eventer)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
tributary.force.stop();
time += t;
}
function eventer(d,i) {
if(d.rsvp) {
d.y -= 3.6
}
}
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;
}
};
}
circle {
stroke: #ffffff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment