Skip to content

Instantly share code, notes, and snippets.

@alper
Created January 12, 2012 15:38
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 alper/1601164 to your computer and use it in GitHub Desktop.
Save alper/1601164 to your computer and use it in GitHub Desktop.
D3 onload
window.onload = function() {
console.log('Loaded..');
var w = 960;
var h = 2200;
var cluster = d3.layout.cluster()
.size([h, w - 160])
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var vis = d3.select("#viz").append("svg")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("tranform", "translate(40, 0)");
d3.json("tree.json", function(json) {
console.log(json);
var nodes = cluster.nodes(json);
console.log(nodes);
console.log(cluster.links(nodes));
var link = vis.selectAll("path.link")
.data(cluster.links(nodes))
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = vis.selectAll("g.node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.attr("text-anchor", function(d) { return d.children ? "end":"start"; })
.text(function(d) { return d.name; });
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment