-
-
Save paladini/1dae7d005c5718edfbf6da3fb7d63036 to your computer and use it in GitHub Desktop.
Multi-Foci 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
license: gpl-3.0 |
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.node { | |
stroke-width: 1.5px; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var fill = d3.scale.category10(); | |
var nodes = [], | |
foci = [{x: 150, y: 150}, {x: 350, y: 250}, {x: 700, y: 400}]; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var force = d3.layout.force() | |
.nodes(nodes) | |
.links([]) | |
.gravity(0) | |
.size([width, height]) | |
.on("tick", tick); | |
var node = svg.selectAll("circle"); | |
function tick(e) { | |
var k = .1 * e.alpha; | |
// Push nodes toward their designated focus. | |
nodes.forEach(function(o, i) { | |
o.y += (foci[o.id].y - o.y) * k; | |
o.x += (foci[o.id].x - o.x) * k; | |
}); | |
node | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
} | |
for (var i = 0; i < 150; i = i + 1) { | |
nodes.push({id: ~~(Math.random() * foci.length)}); | |
force.start(); | |
node = node.data(nodes); | |
node.enter().append("circle") | |
.attr("class", "node") | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }) | |
.attr("r", 8) | |
.style("fill", function(d) { return fill(d.id); }) | |
.style("stroke", function(d) { return d3.rgb(fill(d.id)).darker(2); }) | |
.call(force.drag); | |
} | |
/* setInterval(function(){ | |
nodes.push({id: ~~(Math.random() * foci.length)}); | |
force.start(); | |
node = node.data(nodes); | |
node.enter().append("circle") | |
.attr("class", "node") | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }) | |
.attr("r", 8) | |
.style("fill", function(d) { return fill(d.id); }) | |
.style("stroke", function(d) { return d3.rgb(fill(d.id)).darker(2); }) | |
.call(force.drag); | |
}, 10); */ | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment