我希望使用D3 v4创建以下图表,任何人都可以通过提供相关的源链接或指针来帮助我。
var treeData = {
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [
{
"name": "Son of A",
"parent": "Level 2: A"
},
{
"name": "Daughter of A",
"parent": "Level 2: A"
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level"
}
]
};
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(60,0)");
var tree = d3.cluster()
.size([height, width - 160]);
var stratify = d3.stratify()
.parentId(function (d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
var root = d3.hierarchy(treeData);
tree(root);
var link = g.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function (d) {
return "M" + d.y + "," + d.x
+ "C" + (d.parent.y + 100) + "," + d.x
+ " " + (d.parent.y + 100) + "," + d.parent.x
+ " " + d.parent.y + "," + d.parent.x;
});
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function (d) { console.log("1: ", d); return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function (d) {
console.log("2: ", d);
return "translate(" + d.y + "," + d.x + ")";
})
node.append("rect")
.attr("width", 100)
.attr("height", 50)
.attr("x", -50)
.attr("y", -25)
.attr("fill", '#79a4f6');
node.append("text")
.attr("y", 30)
.attr("x", 50)
.style("text-anchor", function (d) { return d.children ? "end" : "start"; })
.text(function (d) {
return d.data.name;
});
我需要以下指针: