Created
December 14, 2014 02:12
-
-
Save dem42/62296989abd820f8cb6b to your computer and use it in GitHub Desktop.
This file contains 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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"/> | |
<title>D3 tree</title> | |
<style> | |
.line { | |
stroke: blue; | |
stroke-width: 1.5px; | |
fill: white; | |
} | |
circle { | |
fill: red; | |
} | |
</style> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
</head> | |
<body> | |
<script> | |
"use strict" | |
var svg = d3.select("body").append("svg").attr("width",400).attr("height", 1000).append("g").attr("transform", "translate(200,50)"); | |
var data = []; | |
for (var i=1;i<=8;i++) { | |
data.push(i); | |
}; | |
var tree = d3.layout.tree() | |
.children(function(d) { | |
if (d.v >= data.length / 2) { | |
return []; | |
} | |
return [{v: data[2*d.v - 1]}, {v: data[2*d.v]}]; | |
}) | |
.nodeSize([80, 80]) | |
var nodes = tree.nodes({v: 1}) | |
console.log(nodes); | |
svg.selectAll(".links") | |
.data(tree.links(nodes)) | |
.enter() | |
.append("path") | |
.attr("class", "line") | |
.attr("d", d3.svg.diagonal()) | |
var node_gs = | |
svg.selectAll(".nodes") | |
.data(nodes) | |
.enter() | |
.append("g") | |
.attr("transform", function(d) { return "translate(" + d.x + " " + d.y + ")";}) | |
node_gs.append("circle").attr("r", 25) | |
node_gs.append("text").text(function(d) { return d.v; }) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment