Skip to content

Instantly share code, notes, and snippets.

@Rainboylvx
Created May 31, 2018 08:46
Show Gist options
  • Save Rainboylvx/8f78a69e8e236411fd074a3c810a3faa to your computer and use it in GitHub Desktop.
Save Rainboylvx/8f78a69e8e236411fd074a3c810a3faa to your computer and use it in GitHub Desktop.
simple tree
license: mit
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 2 columns, instead of 1 in line 1.
id,parent
1
2,1
3,1
6,1
7,1
8,1
9,1
10,1
4,2
5,2
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var g = svg.append("g").attr("transform", function(d) { return "translate(" + 00 + "," + 100 + ")"; })
var tree = d3.tree().size([300,300])
.separation(function(a,b){
return 1;
})
var stratify = d3.stratify().parentId(function(d){
return d.parent
})
d3.csv("data.csv", function(error, data) {
//console.log(data)
var root = stratify(data);
var nodes = tree(root)
var link = g.selectAll(".link")
.data(root.links())
.enter().append("path")
.attr("fill","none")
.attr("stroke","black")
.attr("class", "link")
.attr("d", d3.linkVertical()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; }));
var node = g.selectAll(".node")
.data(root.descendants())
.enter()
.append("g")
.attr("class",".node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
node.append("circle")
.attr("r",10)
.attr("fill","#fff")
.attr("stroke","black")
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment