以下网址将使用d3获取面向垂直的树。
How to get the vertically-oriented tree using d3.js
我按照下面的回答:
http://i.stack.imgur.com/EDsDY.png
我的要求是在链接中间的每个链接中使用标记以相反的顺序绘制上面的一个。请为此要求建议正确的有效解决方案。
<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript" src="d3.v2.min.js"></script>
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
<script>
var margin = {top: 100, right: 50, bottom: 100, left: 50},
width = 2200 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var tree = d3.layout.tree()
.separation(function(a, b) { return a.parent === b.parent ? 1 : 1.2; })
.children(function(d) { return d.parents; })
.size([width, height]);
var svg = d3.select("body")
.attr("bgcolor", "white")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var nodes = tree.nodes(getJsonData());
var node = svg.selectAll(".node")
.data(nodes)
.enter()
.append("g");
node.append("rect")
.attr("width", 145)
.attr("height", 80)
.attr("fill", "#DFF0F0")
.attr("stroke", "black")
.attr("stroke-width", "1")
.attr("x", function(d) { return d.x - 70; })
.attr("y", function(d) { return height - d.y - 40; });
node.append("text")
.attr("font-size", "7px")
.attr("font-weight", "bold")
.attr("fill", "#181A1A")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return height - d.y - 15; })
.style("text-anchor", "middle")
.text(function(d) { return d.accountId; });
node.append("text")
.attr("font-size", "8px")
.attr("fill", "blue")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return 10 + height - d.y; })
.style("text-anchor", "middle")
.text(function(d) { return d.branchName; });
node.append("text")
.attr("font-size", "7px")
.attr("fill", "grey")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return 25 + height - d.y; })
.style("text-anchor", "middle")
.text(function(d) { return d.currency; });
node.append("text")
.attr("font-size", "6px")
.attr("fill", "grey")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return 35 + height - d.y; })
.style("text-anchor", "middle")
.text(function(d) { return d.country; });
node.append("text")
.attr("font-size", "12px")
.attr("font-weight", "bold")
.attr("fill", "#181A1A")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return 7 + height - d.y; })
.style("text-anchor", "middle")
.text(function(d) { return d.finIns; });
node.append("text")
.attr("font-size", "11px")
.attr("font-weight", "bold")
.attr("fill", "#181A1A")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return 7 + height - d.y; })
.style("text-anchor", "middle")
.text(function(d) { return d.customerName; });
var link = svg.selectAll(".link")
.data(tree.links(nodes))
.enter()
.insert("path", "g")
.attr("fill", "none")
.attr("stroke", "#000")
.attr("stroke", "#000")
.attr("shape-rendering", "crispEdges")
.attr("d", connect);
function connect(d, i) {
return "M" + d.source.x + "," + (height - d.source.y)
+ "V" + (height - (3*d.source.y + 4*d.target.y)/7)
+ "H" + d.target.x
+ "V" + (height - d.target.y);
};
function getJsonData() {
return {
"customerName" : "ACC",
parents: [
{"finIns" : "child",
parents: [
{
"accountId": "024",
"branchName": "AMSTERDAM",
"currency": "USD",
"country": "Netherlands"
},
{
"accountId": "000001",
"branchName": "RR, N.A.",
"currency": "USD",
"country": "United States"
}
]},]
};
};
</script>
</body>
</html>
答案 0 :(得分:1)
要执行此操作,您需要反转所有y
坐标 - 也就是说,您当前有height - d.y
之类的坐标,您应该d.y
。对于标记,您可以使用SVG line markers。
完整示例here。