Skip to content

Commit

Permalink
Add slides for today's talk.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 21, 2011
1 parent 661d8f7 commit 2661082
Show file tree
Hide file tree
Showing 62 changed files with 13,016 additions and 0 deletions.
5 changes: 5 additions & 0 deletions talk/20110921/blank.html
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<html>
<body>
</body>
</html>
84 changes: 84 additions & 0 deletions talk/20110921/bounding.html
@@ -0,0 +1,84 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Force Layouts - Bounding Box</title>
<script type="text/javascript" src="d3/d3.js"></script>
<script type="text/javascript" src="d3/d3.geom.js"></script>
<script type="text/javascript" src="d3/d3.layout.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">

circle {
stroke-width: 1.5px;
}

line {
stroke: rgba(32,32,32,.3);
}

</style>
</head>
<body>
<div id="body">
<div id="chart"></div>
<div id="header">bounding box</div>
</div>
<script type="text/javascript">

var w = 1280,
h = 800,
r = 6,
z = d3.scale.category20c();

var force = d3.layout.force()
.gravity(0.06)
.charge(-150)
.linkDistance(40)
.size([w *= 2 / 3, h *= 2 / 3]);

var svg = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + w / 4 + "," + h / 3 + ")");

svg.append("svg:rect")
.attr("width", w)
.attr("height", h)
.style("fill", "none")
.style("stroke", "#000");

d3.json("miserables.json", function(json) {
var link = svg.selectAll("line")
.data(json.links)
.enter().append("svg:line");

var node = svg.selectAll("circle")
.data(json.nodes)
.enter().append("svg:circle")
.attr("r", r - .75)
.style("fill", function(d) { return z(d.group); })
.style("stroke", function(d) { return d3.rgb(z(d.group)).darker(); })
.call(force.drag);

force
.nodes(json.nodes)
.links(json.links)
.on("tick", tick)
.start();

function tick() {
node.attr("cx", function(d) { return d.x = Math.max(r, Math.min(w - r, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(r, Math.min(h - r, d.y)); });

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; });
}
});

</script>
</body>
</html>
118 changes: 118 additions & 0 deletions talk/20110921/builder.html
@@ -0,0 +1,118 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Force Layouts</title>
<script type="text/javascript" src="d3/d3.js"></script>
<script type="text/javascript" src="d3/d3.geom.js"></script>
<script type="text/javascript" src="d3/d3.layout.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">

rect {
fill: #fff;
}

.cursor {
fill: none;
stroke: brown;
pointer-events: none;
}

.link {
stroke: #999;
}

</style>
</head>
<body>
<div id="body">
<div id="chart"></div>
<div id="header">
graph builder
</div>
</div>
<script type="text/javascript">

var w = 1280,
h = 800;

var force = d3.layout.force()
.charge(-80)
.linkDistance(25)
.size([w, h]);

var nodes = force.nodes(),
links = force.links();

var svg = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);

svg.append("svg:rect")
.attr("width", w)
.attr("height", h);

var cursor = svg.append("svg: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.svg.mouse(this) + ")");
});

svg.on("mousedown", function() {
d3.event.preventDefault();
});

svg.on("click", function() {
var point = d3.svg.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() {
svg.selectAll("line.link")
.data(links)
.enter().insert("svg:line", "circle.node")
.attr("class", "link");

svg.selectAll("circle.node")
.data(nodes)
.enter().insert("svg:circle", "circle.cursor")
.attr("class", "node")
.attr("r", 5)
.call(force.drag);

force.start();
}

</script>
</body>
</html>
138 changes: 138 additions & 0 deletions talk/20110921/collapsible.html
@@ -0,0 +1,138 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Force Layouts</title>
<script type="text/javascript" src="d3/d3.js"></script>
<script type="text/javascript" src="d3/d3.geom.js"></script>
<script type="text/javascript" src="d3/d3.layout.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">

circle.node {
cursor: pointer;
stroke: #000;
stroke-width: .5px;
}

line.link {
fill: none;
stroke: #9ecae1;
stroke-width: 1.5px;
}

</style>
</head>
<body>
<div id="body">
<div id="chart"></div>
<div id="header">
collapsible tree
</div>
</div>
<script type="text/javascript">

var w = 1280,
h = 800,
node,
link,
root;

var force = d3.layout.force()
.on("tick", tick)
.size([w, h]);

var svg = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);

d3.json("flare.json", function(json) {
root = json;
update();
});

function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);

// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();

// Update the links.
link = svg.selectAll("line")
.data(links, function(d) { return d.target.id; });

link.enter().insert("svg:line", ".node")
.attr("class", "link");

link.exit().remove();

// Update the nodes.
node = svg.selectAll("circle.node")
.data(nodes, function(d) { return d.id; });

node.transition()
.attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; })
.style("fill", color);

node.enter().append("svg:circle")
.attr("class", "node")
.attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; })
.style("fill", color)
.on("click", click)
.call(force.drag);

node.sort(function(a, b) { return (!b.children - !a.children) || (b.size - a.size); });

node.exit().remove();
}

function tick() {
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; });

node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}

// Color leaf nodes orange, and packages white or blue.
function color(d) {
return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c";
}

// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update();
}

// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;

function recurse(node) {
if (!node.id) node.id = ++i;
nodes.push(node);
return node.children
? node.size = node.children.reduce(function(p, v) { return p + recurse(v); }, 0)
: node.size;
}

recurse(root);
return nodes;
}

</script>
</body>
</html>

0 comments on commit 2661082

Please sign in to comment.