Skip to content

Instantly share code, notes, and snippets.

@wanderer
Created May 25, 2014 21: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 wanderer/b50bedfdf9a1a202e2cd to your computer and use it in GitHub Desktop.
Save wanderer/b50bedfdf9a1a202e2cd to your computer and use it in GitHub Desktop.
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
<meta name="description" content="G function from GEB page 137" />
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<body></bod>
var width = 960,
height = 600,
number = 100;
function G(n){
if(n===0){
return 0;
}else{
return n - G(G(n-1));
}
}
function tree(nodes) {
var nodeById = {};
// Index the nodes by id, in case they come out of order.
nodes.forEach(function(d) {
nodeById[d.name] = d;
});
// Lazily compute children.
nodes.forEach(function(d) {
if(String(d.val) != d.name){
var val = nodeById[d.val];
if (val.children){
val.children.push(d);
}else{
val.children = [d];
}
}
});
return nodes[0];
}
dataArray = [];
for(var i=1; i<number; i++ ){
var val = G(i);
dataArray.push({name:String(i), val: val});
}
var data = tree(dataArray);
var cluster = d3.layout.cluster()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
d3.select(self.frameElement).style("height", height + "px");
var nodes = cluster.nodes(data),
links = cluster.links(nodes);
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll(".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)
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.name; });
@wanderer
Copy link
Author

the G function from GEB page 137.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment