Commit dba9baf2 authored by Mike Bostock's avatar Mike Bostock
Browse files

Remove force-dynamic example.

Replaced by <http://bl.ocks.org/929623>.
parent 3208345a
<!DOCTYPE html>
<meta charset="utf-8">
<title>Force-Directed Layout (Dynamic)</title>
<style>
rect {
fill: #fff;
}
.node {
fill: #000;
}
.cursor {
fill: none;
stroke: brown;
pointer-events: none;
}
.link {
stroke: #999;
}
</style>
<body>
<script src="../../d3.js"></script>
<script>
var width = 960,
height = 500,
fill = d3.scale.category20(),
nodes = [],
links = [];
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.distance(30)
.nodes(nodes)
.links(links)
.size([width, height]);
var cursor = svg.append("circle")
.attr("r", 30)
.attr("transform", "translate(-100,-100)")
.attr("class", "cursor");
force.on("tick", function() {
svg.selectAll("line.link")
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
svg.selectAll("circle.node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
svg.on("mousemove", function() {
cursor.attr("transform", "translate(" + d3.mouse(this) + ")");
});
svg.on("mousedown", function() {
var point = d3.mouse(this),
node = {x: point[0], y: point[1]},
n = nodes.push(node);
// add links to any nearby nodes
nodes.forEach(function(target) {
var x = target.x - node.x,
y = target.y - node.y;
if (Math.sqrt(x * x + y * y) < 30) {
links.push({source: node, target: target});
}
});
restart();
});
restart();
function restart() {
force.start();
svg.selectAll("line.link")
.data(links)
.enter().insert("line", "circle.node")
.attr("class", "link")
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
svg.selectAll("circle.node")
.data(nodes)
.enter().insert("circle", "circle.cursor")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 4.5)
.call(force.drag);
}
</script>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment