In response to a Stack Overflow question, Tree drawing orientation.
-
-
Save ziad-saab/721b8a787527ffaf7ede to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"children": [ | |
{ | |
"children": [ | |
{}, | |
{} | |
] | |
} | |
] | |
} |
This file contains hidden or 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> | |
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.line() | |
.x(function(d) { return d.x; }) | |
.y(function(d) { return d.y; }) | |
.interpolate("basis")); | |
// 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