Javascript d3:flare.json中的树布局-如何仅包括一些节点?

Javascript d3:flare.json中的树布局-如何仅包括一些节点?,javascript,d3.js,Javascript,D3.js,请看 它从flare.json加载数据,并以树布局显示节点。我想做以下工作: 1.编写一个函数,根据某些条件仅显示部分节点,然后重新加载树。 2.编写一个函数来添加一些节点并重新加载树。 3.编写一个函数来删除一些节点并重新加载树 我需要从外部调用这些函数 d3.json(“/d/4063550/flare.json”),函数(错误,flare) 在这个样本中 请让我知道怎么做。我尝试了.children.splice方法并再次调用了upload(root)。但它不起作用 以下是守则的相关部分:

请看 它从flare.json加载数据,并以树布局显示节点。我想做以下工作: 1.编写一个函数,根据某些条件仅显示部分节点,然后重新加载树。 2.编写一个函数来添加一些节点并重新加载树。 3.编写一个函数来删除一些节点并重新加载树

我需要从外部调用这些函数 d3.json(“/d/4063550/flare.json”),函数(错误,flare) 在这个样本中

请让我知道怎么做。我尝试了.children.splice方法并再次调用了upload(root)。但它不起作用

以下是守则的相关部分:

d3.json("/d/4063550/flare.json", function(error, flare) {
  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);
});

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);

  // 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 + ")"; })

    nodeEnter.append("circle")
  .attr("r", 1e-6)
  .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

   // Transition nodes to their new position.
  var 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"; });

   // 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);

  // 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;
      });
}

我对你的代码做了一点修改,它对我很有用:

    d3.json("flare.json", function(json) {
      root = json;
      root.fixed = true;
      root.x = w / 2;
      root.y = h / 2 - 80;
      console.log("haha");
      firstTimeRun();
      update();
    });

    function firstTimeRun(){
      flatten(root).forEach(collapse);  
    }
    function collapse(d) {
    if (d.children) {
       d._children = d.children;
       d.children = null;
    }
  }

Avinash-请也发布您的示例数据。数据是动态生成的,或者是静态数据?对于树结构中的任何json数据,我都面临这个问题。我的问题是-如何删除某些节点并重新绘制树?请查看我发布的url链接。可能会有帮助。@Lars,该问题的答案告诉如何折叠节点e(虽然问题是如何删除)。我的问题是-示例代码(其url我已经提到)使用d3.json加载数据。加载时,它会显示整个树。但我想做以下操作-在d3.json中,我只想在内存中加载树。稍后,我想删除(而不仅仅是折叠)一些节点,然后显示树。我怎么做?这个问题和答案告诉你如何添加节点。