我有以下代码,我正在尝试将文字附加到我的链接上(我需要在链接上代表国家/地区名称),请注意该文本嵌入在json
列表中,并且这就是为什么我认为我无法进入我的链接:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text { font: 12px sans-serif; }
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var treeData = [
{"name":["1"],"weights":["17284"],"variableNames":["Country name"],"criteria":
{"levels":["","Brazil","Canada","France","Germany","Spain","Turkey","United Kingdom","United States"]},
"children":[{"name":["2"],"weights":["5920"],"variableNames":["Country name"],
"criteria":{"levels":["","Brazil","Canada","France","Germany","Spain","Turkey","United Kingdom","United States"]},
"children":[{"name":["3 "],"weights":["604"],"prediction":["0.256"]},{"name":["4 "],"weights":["5316"],"prediction":["0.231"]}],"prediction":["0.233"]},{"name":["5"],"weights":["11364"],
"variableNames":["Country name"],"criteria":{"levels":["","Brazil","Canada","France","Germany","Spain","Turkey","United Kingdom","United States"]},
"children":[{"name":["6 "],"weights":["10029"],"prediction":["0.153"]},{"name":["7 "],"weights":["1335"],
"prediction":["0.053"]}],"prediction":["0.141"]}],"prediction":["0.173"]}
];
// ************** Generate the tree diagram *****************
var margin = {top: 20, right: 120, bottom: 20, left: 120},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 180; });
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")"; });
nodeEnter.append("circle")
.attr("r", 10)
.style("fill", "#fff");
nodeEnter.append("text")
.attr("x", function(d) {
return d.children || d._children ? -13 : 13; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.name; })
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
link.append("text")
.attr("x", function(d) {
return d.children || d._children ? -13 : 13; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.source.criteria.levels; }) // also tried d.source.criteria
.style("fill-opacity", 1);
}
</script>
</body>
</html>
我试图从其他问题中获得帮助:Adding text labels to force directed graph links in d3.js
但是找不到任何有嵌入列表的问题。
先谢谢!!
答案 0 :(得分:1)
关键是您可以像节点一样定义链接设置,即构建包含g
和path
的{{1}}。在您的示例中,text
分别在text
元素中结束。
所以这是关键:
path
(我在这里和那里也改变了一点......不是我不确定你想要显示什么文本)