使用书中的示例D3提示和技巧我制作了一个垂直折叠/展开的树状图/ TreeMap 。
see what I did here! (the problem)
到目前为止,一切都很好。
...但是。
Q1:我希望 map 开始折叠,即仅显示第一个节点。
(我寻找有关堆栈溢出的答案(几乎找到了它,但它使用耀斑图表,并且我无法让更改后的代码在我的图表上工作。)
Q2:节点从左下角飞出,但我希望它们以第一个节点为原点。摆弄各种设置,没有任何帮助。
Q3:我希望节点“kwaliteit”成为指向另一个页面的超链接。
有人有答案吗?
这是我的代码:
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var treeData = [
{
"name": "jaar",
"parent": "null",
"children": [
{
"name": "2014",
"parent": "jaar",
"children": [
{
"name": "kwaliteit",
"parent": "2014"
},
{
"name": "geen data",
"parent": "2014"
}
]
},
{
"name": "2015 (geen data)",
"parent": "jaar"
}
]
}
];
// ************** 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,
duration = 750,
root;
var tree = d3.layout.tree()
.size([width, height]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.x, d.y]; });
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];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameElement).style("height", "500px");
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 * 100; });
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("click", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeEnter.append("text")
.attr("y", function(d) {
return d.children || d._children ? -18 : 18; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.name; })
.style("fill-opacity", 1);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
nodeUpdate.select("circle")
.attr("r", 10)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
</script>
最佳答案
问题1:我希望 map 开始折叠,即仅显示第一个节点
最简单的方法就是重命名所有 children
属性并将其命名为 _children
var treeData = [
{
"name": "jaar",
"parent": "null",
"_children": [
{
"name": "2014",
"parent": "jaar",
"_children": [
{
"name": "kwaliteit",
"parent": "2014"
},
{
"name": "geen data",
"parent": "2014"
}
]
},
{
"name": "2015 (geen data)",
"parent": "jaar"
}
]
}
];
<小时/>
问题2:节点从左下角飞出,但我希望它们以第一个节点为原点。
只需在 enter()
中交换 x0 和 y0 值
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "translate(" + source.x0 + "," + source.y0 + ")";
})
.on("click", click);
<小时/>
问题3:我希望节点“kwaliteit”成为一个超链接
附加一个 a
并通过查看文本来设置超链接(或者,您也可以将其放入数据结构中)
.append("a")
.attr("href", function (d) { if (d.name === "kwaliteit") return "myurl" })
如果您无法在 Chrome 中使用它,只需使用它即可
.on("click", function (d) { if (d.name === "kwaliteit") window.location.href = "myurl" })
<小时/>
fiddle - http://jsfiddle.net/gaa4v3gu/
关于javascript - 垂直折叠树形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625119/