xxxxxxxxxx
<meta charset="utf-8">
<style>
.node circle {
fill: #999;
}
.node text {
font: 10px sans-serif;
}
.node--internal circle {
fill: #555;
}
.node--internal text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #555;
stroke-opacity: 0.4;
stroke-width: 1.5px;
}
</style>
<svg width="500" height="400"></svg>
<script src="//d3js.org/d3.v4.js"></script>
<script>
var treeData = {
"name": "1",
"children": [
{"name": "11", "children": [
{"name": "111", "size": 1},
{"name": "112", "size": 1},
{"name": "113", "size": 1},
{"name": "114", "children": [
{"name": "1141", "size": 1},
{"name": "1142", "size": 1}]
}]
},
{"name": "12", "children": [
{"name": "121", "children": [
{"name": "1211", "size": 1},
{"name": "1212", "size": 1}]
},
{"name": "122", "size": 1}]
}]
}
;
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g");
var tree = d3.cluster()
.size([width, height]);
var node = d3.hierarchy(treeData)
/*var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });*/
/*d3.csv("flare.csv", function(error, data) {
if (error) throw error;
var root = stratify(data)
.sort(function(a, b) { return (a.height - b.height) || a.id.localeCompare(b.id); });*/
var root = tree(node);
var link = g.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
return "M" + d.x + "," + d.y
+ "C" + (d.parent.x) + "," + d.y
+ " " + (d.parent.x) + "," + d.parent.y
+ " " + d.parent.x + "," + d.parent.y;
});
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
node.append("circle")
.attr("r", 2.5);
/*node.append("text")
.attr("dy", 3)
.attr("x", function(d) { return d.children ? -8 : 8; })
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1); });*/
//});
</script>
https://d3js.org/d3.v4.js