Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从D3中的列表中删除节点并重置布局_Javascript_Arrays_D3.js_Collision Detection - Fatal编程技术网

Javascript 从D3中的列表中删除节点并重置布局

Javascript 从D3中的列表中删除节点并重置布局,javascript,arrays,d3.js,collision-detection,Javascript,Arrays,D3.js,Collision Detection,我正在尝试编辑在上提供的碰撞检测程序 我试图编辑代码,使其在每个节点上都有标签,我可以通过停止动画并单击要删除的节点来删除节点。以下是编辑后的代码的外观: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <script type="text/javascript" src="d

我正在尝试编辑在上提供的碰撞检测程序

我试图编辑代码,使其在每个节点上都有标签,我可以通过停止动画并单击要删除的节点来删除节点。以下是编辑后的代码的外观:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <script type="text/javascript" src="d3/d3.js"></script>
    <script type="text/javascript" src="d3/d3.geom.js"></script>
    <script type="text/javascript" src="d3/d3.layout.js"></script>
 <script type="text/javascript" src="d3/d3.min.js"></script>
    <link type="text/css" rel="stylesheet" href="style.css"/>
    <style type="text/css">

circle {
  stroke: #000;
  stroke-opacity: .5;
}

    </style>
  </head>
  <body>
    <div id="body">
      <div id="footer">
        Collision Detection
        <div class="hint">move the mouse to repel nodes</div>
      </div>
    </div>
    <script type="text/javascript">

var width = 1280,
    height = 800;

var labels = [
    "hello",
    "goodbye",
    "purple",
    "blue",
    "green",
    "pink",
    "yellow",
    "white",
    "black",
    "brown",
    "cat",
    "dog",
    "bird",
    "snake",
    "turtle"];

var i = 0;

var nodes = d3.range(200).map(function() { return {radius: Math.random() * 12 + 4, label: labels[Math.floor(Math.random() * 15 )]}; }),
    root = nodes[0],
    color = d3.scale.category10();

root.radius = 0;
root.fixed = true;

var force = d3.layout.force()
    .gravity(0.05)
    .charge(function(d, i) { return i ? 0 : -2000; })
    .nodes(nodes)
    .size([width, height]);

force.start();

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

g = svg.selectAll("circle")
    .data(nodes.slice(1))
  .enter().append("g")

    g.append('circle')
    .attr('cx', 0)
    .attr('cy', 0)
    .attr("r", function(d) { return d.radius; })
    .style("fill", function(d, i) { return color(i % 20); })

    g.append('text')
    .text(function(d){return d.label})
    .attr('x', -15)
    .attr('y', 5)

force.on("tick", function(e) {
  var q = d3.geom.quadtree(nodes),
      i = 0,
      n = nodes.length;

  while (++i < n) q.visit(collide(nodes[i]));

  svg.selectAll("g")
  .attr('transform', function(d){return "translate("+d.x+","+d.y+")"})
});

svg.on("mousemove", function() {
  var p1 = d3.mouse(this);
  root.px = p1[0];
  root.py = p1[1];
  //force.resume();
});

svg.on("click", function() {
    if(force.alpha()) {
    force.stop();
    } else {
    force.resume();
    }
});

svg.selectAll("g").on("click", function() {
    d3.event.stopPropagation();
    this.remove();
    //nodes.removeChild(this.remove());
    //var deleteNode = this.selection.node();

  force.on("tick", function(x) {
    var a = d3.geom.quadtree(nodes),
        b = 0,
        c = nodes.length;

    while(++b < c) a.visit(collide(nodes[b]));

    //svg.selectAll("g")
    //.attr('transform', function(d){return "translate("+d.x+"."+d.y+")"})
  });

});


function collide(node) {
  var r = node.radius + 16,
      nx1 = node.x - r,
      nx2 = node.x + r,
      ny1 = node.y - r,
      ny2 = node.y + r;
  return function(quad, x1, y1, x2, y2) {
    if (quad.point && (quad.point !== node)) {
      var x = node.x - quad.point.x,
          y = node.y - quad.point.y,
          l = Math.sqrt(x * x + y * y),
          r = node.radius + quad.point.radius;
      if (l < r) {
        l = (l - r) / l * .5;
        node.x -= x *= l;
        node.y -= y *= l;
        quad.point.x += x;
        quad.point.y += y;
      }
    }
    return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
  };
}
    </script>
  </body>
</html>

圈{
行程:#000;
笔画不透明度:.5;
}
碰撞检测
移动鼠标以排斥节点
可变宽度=1280,
高度=800;
变量标签=[
“你好”,
“再见”,
“紫色”,
“蓝色”,
“绿色”,
“粉红”,
“黄色”,
“白色”,
“黑色”,
“棕色”,
“猫”,
“狗”,
“鸟”,
“蛇”,
“海龟”];
var i=0;
var nodes=d3.range(200).map(函数(){return{radius:Math.random()*12+4,label:labels[Math.floor(Math.random()*15)]};}),
根=节点[0],
颜色=d3.scale.category10();
根半径=0;
root.fixed=true;
var-force=d3.layout.force()
.重力(0.05)
.charge(函数(d,i){返回i?0:-2000;})
.节点(节点)
.尺寸([宽度、高度]);
force.start();
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
g=svg。选择全部(“圆形”)
.数据(节点.切片(1))
.enter().append(“g”)
g、 追加('圆')
.attr('cx',0)
.attr('cy',0)
.attr(“r”,函数(d){返回d.radius;})
.style(“填充”,函数(d,i){返回颜色(i%20);})
g、 追加('文本')
.text(函数(d){return d.label})
.attr('x',-15)
.attr('y',5)
强制开启(“勾选”),功能(e){
var q=d3.几何四叉树(节点),
i=0,
n=节点长度;
而(++inx2 | x2ny2 | y2
我遇到的问题是从节点数组中删除节点并重置布局。现在它隐藏了实际的圆和标签,但是当动画重新启动时,它仍然有一个节点没有实际删除的点

svg.selectAll("g").on("click", function() {
        d3.event.stopPropagation();
        this.remove();
        //nodes.removeChild(this.remove());
        //var deleteNode = this.selection.node();
        //nodes.removeChild(deleteNode);

      force.on("tick", function(x) {
        var a = d3.geom.quadtree(nodes),
            b = 0,
            c = nodes.length;

        while(++b < c) a.visit(collide(nodes[b]));

        //svg.selectAll("g")
        //.attr('transform', function(d){return "translate("+d.x+"."+d.y+")"})
      });

    });
svg.selectAll(“g”)。单击(“click”,function()){
d3.event.stopPropagation();
这个。删除();
//nodes.removeChild(this.remove());
//var deleteNode=this.selection.node();
//nodes.removeChild(deleteNode);
强制开启(“勾选”),功能(x){
变量a=d3.几何四叉树(节点),
b=0,
c=节点长度;
而(++b

如您所见,我使用了这个.remove()来隐藏节点,但它实际上并没有删除节点。下面的代码是我为了删除节点然后重置布局而尝试的其他东西,但是没有一个成功。我对D3不是很熟悉,我很难理解到底发生了什么,也很难真正从D3中的数组中删除节点。

删除节点后重新启动布局

svg.selectAll("g").on("click", function() {
    d3.event.stopPropagation();
    this.remove();
    force.resume();//restart the layout
});
单击svg将停止勾号单击它将再次启动勾号布局

svg.on("click", function() {
    if(force.alpha()) {
    force.stop();
    } else {
    force.resume();//start the layout
    }
});
工作代码


希望这有帮助

上述答案几乎就在那里。删除元素,但不从数据中删除它。因此,我编辑了小提琴并添加了以下内容:

svg.selectAll("g").on("click", function(d) {
    d3.event.stopPropagation();
    this.remove();
    force.resume();

    nodes.splice(d.index,1) //this deletes the node from the data at the nodes index
     console.log(nodes.length)
});

更新的fiddle:

有点晚了,但你还没有完全解决它。节点将从DOM中删除,但不会从数据中删除。所以我在你的答案中添加了我自己的答案:))希望它能帮助其他人找到bug+1:)