我试图让树节点排成一行(路径),我无法解决。
这是一个从上到下的树,我试图翻到左到右的树,但我认为计算结束了。
任何帮助都会非常感激。
请参阅Plunker中的截图和代码。 http://plnkr.co/edit/9pxJLz?p=preview
var margin = {
top: 20,
right: 120,
bottom: 20,
left: 120
},
width = 960 - margin.right - margin.left,
height = 100 - margin.top - margin.bottom;
var i = 0,
duration = 750,
rectW = 120,
rectH = 30;
var tree = d3.layout.tree().nodeSize([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.y, d.x];
});
var accountSvg = d3.select("body").append("svg")
.attr("width", 1000)
.attr("height", 1000)
.call(zm = d3.behavior.zoom().scaleExtent([0.5, 3]).on("zoom", redraw)).on("dblclick.zoom", null)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.left + ")");
d3.json("flare.json", function(error, flare) {
if (error) throw error;
root = flare;
root.x0 = height / 2;
root.y0 = 0;
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
root.children.forEach(collapse);
update(root);
});
//necessary so that zoom knows where to zoom and unzoom from
zm.translate([250, 20]);
d3.select("body").style("height", "455");
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;
});
// Update the nodes…
var node = accountSvg.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("rect")
.attr("width", rectW)
.attr("height", rectH)
.attr("stroke", "black")
.attr("stroke-width", 1)
.style("fill", function(d) {
return d._children ? "lightsteelblue" : "#fff";
});
nodeEnter.append("text")
.attr("x", rectW / 2)
.attr("y", rectH / 2)
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {
return d.name;
});
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeUpdate.select("rect")
.attr("width", rectW)
.attr("height", rectH)
.attr("stroke", "black")
.attr("stroke-width", 1)
.style("fill", function(d) {
return d._children ? "lightsteelblue" : "#fff";
});
nodeUpdate.select("text")
.style("fill-opacity", 1)
.style("fill", '#404080');
// 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("rect")
.attr("width", rectW)
.attr("height", rectH)
.attr("stroke", "black")
.attr("stroke-width", 1);
nodeExit.select("text");
// Update the links…
var link = accountSvg.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", function(d) {
var s = {
x: d.source.x0 + -15,
y: d.source.y0 + 120
}
var t = {
x: d.target.x + 15,
y: d.target.y + 120
}
return diagonal({
source: s,
target: t
})
});
//.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;
});
}
var doubleClickTime = 0;
var threshold = 200;
// Toggle children on click.
function click(d) {
var t0 = new Date();
if (t0 - doubleClickTime > threshold) {
setTimeout(function() {
if (t0 - doubleClickTime > threshold) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
}, threshold);
}
}
// Redraw for zoom
function redraw() {
//console.log("here", d3.event.translate, d3.event.scale);
accountSvg.attr("transform",
"translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
}
答案 0 :(得分:1)
您正在翻译所有链接:
.attr("transform", function (d) {
return "translate(" + rectW + "," + rectH / 2 + ")";
})
不要那样做。而不是那样,改变对角线:
link.transition()
.duration(duration)
.attr("d", function(d) {
var s = {
y: d.source.y + rectW,
x: d.source.x + rectH / 2
};
var t = {
x: d.target.x + rectH / 2,
y: d.target.y
};
return diagonal({
source: s,
target: t
})
});
以下是更新的plunker:http://plnkr.co/edit/LnuoQY7R0tDWR4EnW1Rg?p=preview