Skip to content

Instantly share code, notes, and snippets.

@git-ashish
Forked from mbostock/.block
Last active August 29, 2015 14:06
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 git-ashish/c5a7f549e920c8a64204 to your computer and use it in GitHub Desktop.
Save git-ashish/c5a7f549e920c8a64204 to your computer and use it in GitHub Desktop.
{
"children": [
{
"children": [
{},
{}
]
}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #000;
}
.border {
fill: none;
shape-rendering: crispEdges;
stroke: #aaa;
}
.node {
stroke: #fff;
}
</style>
<body>
<script src="http://d3js.org/d3.v2.js?2.9.6"></script>
<script>
var margin = {top: 140, right: 10, bottom: 140, left: 10},
width = 240 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var orientations = {
"top-to-bottom": {
size: [width, height],
x: function(d) { return d.x; },
y: function(d) { return d.y; }
},
"right-to-left": {
size: [height, width],
x: function(d) { return width - d.y; },
y: function(d) { return d.x; }
},
"bottom-to-top": {
size: [width, height],
x: function(d) { return d.x; },
y: function(d) { return height - d.y; }
},
"left-to-right": {
size: [height, width],
x: function(d) { return d.y; },
y: function(d) { return d.x; }
}
};
var svg = d3.select("body").selectAll("svg")
.data(d3.entries(orientations))
.enter().append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("rect")
.attr("width", width)
.attr("height", height)
.attr("class", "border");
svg.append("text")
.attr("x", 6)
.attr("y", 6)
.attr("dy", ".71em")
.text(function(d) { return d.key; });
d3.json("graph.json", function(root) {
svg.each(function(orientation) {
var svg = d3.select(this),
o = orientation.value;
// Compute the layout.
var tree = d3.layout.tree().size(o.size),
nodes = tree.nodes(root),
links = tree.links(nodes);
// Create the link lines.
svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", d3.svg.diagonal().projection(function(d) { return [o.x(d), o.y(d)]; }));
// Create the node circles.
svg.selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 4.5)
.attr("cx", o.x)
.attr("cy", o.y);
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment