我正在使用d3
可折叠树,我从中复制的代码:
https://bl.ocks.org/mbostock/4339083
我正在尝试为我的React
网络应用程序容纳它并对其进行更改,以便我可以自己将节点附加到任何树节点。它正在附加节点,正确创建链接,除了节点位置的文本没有更改并且节点在单击它们时没有折叠/展开。所以,这是我认为彼此非常相关的两个主要问题。这是我正在做的事情:
在里面componentDidUpdate
我正在调用创建和更新树形图的函数:
componentDidUpdate() {
this.updateGeneTreeChart(this.getRoot());
}
当我选择(使用基本 html select
)节点值将下一个节点附加到,并在文本字段中输入值然后点击按钮时,会发生更新。因此,我将节点设置为附加到新节点的文本。
updateGeneTreeChart
是:
updateGeneTreeChart = (source) => {
const svg = d3.select(this.node);
//svg.selectAll("*").remove();
const margin = {top: 20, right: 120, bottom: 20, left: 120};
const width = 960 - margin.right - margin.left;
const height = 800 - margin.top - margin.bottom;
let i = 0;
const duration = 750;
let root = this.getRoot();
let tree = d3.layout.tree()
.size([height, width]);
let diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
let nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
console.log("root.children");
console.log(root);
console.log(root.children);
/*if (this.state.selectedGeneArr.length) {
root.children.forEach(this.collapse, this);
}*/
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 180; });
// Update the nodes…
let 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.
let nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("click", this.clickNode);
node.attr("text", function(d) {
return d.name;
});
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeEnter.append("text")
.attr("x", function(d) { return d.children || d._children ? -10 : 10; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
let nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.select("circle")
.attr("r", 4.5)
.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.
let 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…
let 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;
});
}
getRoot()
函数只是获取我用来构建树的更新对象的深层副本:
getRoot = () => {
const margin = {top: 20, right: 120, bottom: 20, left: 120},
width = 960 - margin.right - margin.left,
height = 800 - margin.top - margin.bottom;
let root = JSON.parse(JSON.stringify(this.state.geneTreeJSON));
root.x0 = height / 2;
root.y0 = 0;
return root;
}
geneTreeJSON
是我正在更新的实际对象。我确信它已正确更新,它的结构是正确的,其中的nodes
对象updateGeneTreeChart
是正确的:
但是,所有新连接的节点要么没有文本,要么有根节点的文本(在这种特殊情况下 - Il4ra
)。svg
如果我在每次更新开始时删除其中的所有内容,它实际上会在节点处绘制正确的文本。所以,不知何故,文本没有更新。我尝试执行以下操作:
node.attr("text", function(d) {
return d.name;
});
或者
nodeEnter.attr("text", function(d) {
return d.name;
});
不工作。然后我尝试打印出传递给节点的内容:
nodeEnter.append("text")
.attr("x", function(d) { return d.children || d._children ? -10 : 10; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.text(function(d) {
console.log("nodeEnter");
console.log(d);
return d.name; })
.style("fill-opacity", 1e-6);
并且d
是根节点,子节点不打印出来。所以,我想这是至少应该解决文本问题的地方,但我不知道如何解决。任何建议将不胜感激。