簡體   English   中英

帶有 React 的 D3 可折疊樹:不更新節點

[英]D3 collapsible tree with React: not updating nodes

我正在使用d3可折疊樹,我從中復制的代碼:

https://bl.ocks.org/mbostock/4339083

我正在嘗試將它用於我的React Web 應用程序並對其進行更改,以便我可以自己將節點附加到任何樹節點。 它正在附加節點,正確創建鏈接,除了節點位置的文本未更改並且節點在單擊時不會折疊/展開。 所以,這是我認為彼此非常相關的兩個主要問題。 這是我在做什么:

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是我正在更新的實際對象。 我確信它被正確更新,它的結構是正確的, updateGeneTreeChart中的nodes對象是正確的:

在此處輸入圖片說明

但是,所有新附加的節點要么沒有文本,要么具有根節點的文本(在這種特殊情況下 - 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是根節點,子節點沒有打印出來。 所以,我想這是至少應該解決文本問題的地方,但我不知道如何解決。 任何建議將不勝感激。

我能夠通過添加代碼來修復節點的文本:

// Update the nodes…
let node = svg.selectAll("g.node")
            .data(nodes, function(d) { return d.id || (d.id = ++i); });

---->    node.select("text")
           .data(nodes)
           .text(function(d) {
                return d.name; });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM