Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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.js-exit()节不会删除所有数据_Javascript_Svg_D3.js - Fatal编程技术网

Javascript D3.js-exit()节不会删除所有数据

Javascript D3.js-exit()节不会删除所有数据,javascript,svg,d3.js,Javascript,Svg,D3.js,我将和d3 force布局示例合并到以下代码中: <!DOCTYPE html> <meta charset="utf-8"> <style> /*.link { stroke: #000; stroke-width: 1.5px; } .node { fill: #000; stroke: #fff; stroke-width: 1.5px; } .node.a { fill: #1f77b4; } .node.b { fill:

我将和d3 force布局示例合并到以下代码中:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

/*.link {
  stroke: #000;
  stroke-width: 1.5px;
}

.node {
  fill: #000;
  stroke: #fff;
  stroke-width: 1.5px;
}

.node.a { fill: #1f77b4; }
.node.b { fill: #ff7f0e; }
.node.c { fill: #2ca02c; }*/

.link {
  stroke: #ccc;
}

.node text {
  pointer-events: none;
  font: 10px sans-serif;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 500;

var color = d3.scale.category10();

var nodes = [],
    links = [];

var force = d3.layout.force()
    .nodes(nodes)
    .links(links)
    .charge(-400)
    .linkDistance(120)
    .size([width, height])
    .on("tick", tick);

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

var node = svg.selectAll(".node"),
    link = svg.selectAll(".link");

// 1. Add three nodes and three links.
setTimeout(function() {
  var a = {id: "a"}, b = {id: "b"}, c = {id: "c"};
  nodes.push(a, b, c);
  links.push({source: a, target: b}, {source: a, target: c}, {source: b, target: c});
  start();
}, 0);

// 2. Remove node B and associated links.
setTimeout(function() {
  nodes.splice(1, 1); // remove b
  links.shift(); // remove a-b
  links.pop(); // remove b-c
  start();
}, 3000);

// Add node B back.
setTimeout(function() {
  var a = nodes[0], b = {id: "b"}, c = nodes[1];
  nodes.push(b);
  links.push({source: a, target: b}, {source: b, target: c});
  start();
}, 6000);

function start() {
  link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
  //link.enter().insert("line", ".node").attr("class", "link");
  link.enter().append("line").attr("class", "link");
  link.exit().remove();

  node = node.data(force.nodes(), function(d) { return d.id;});
  node.enter().append("g")
    .attr("class", "node")
    .call(force.drag);
  node.append("image")
    .attr("xlink:href", "https://github.com/favicon.ico")
    .attr("x", -8)
    .attr("y", -8)
    .attr("width", 16)
    .attr("height", 16);
  node.append("text")
    .attr("dx", 12)
    .attr("dy", ".35em")
    .text(function (d) {console.log(d.id); return d.id;});
  //node.enter().append("circle").attr("class", function(d) { return "node " + d.id; }).attr("r", 8);
  node.exit().remove();

  force.start();
}

function tick() {
  node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });


  link.attr("x1", function(d) { return d.source.x; })
      .attr("y1", function(d) { return d.source.y; })
      .attr("x2", function(d) { return d.target.x; })
      .attr("y2", function(d) { return d.target.y; });
}

</script>

/*.链接{
行程:#000;
笔划宽度:1.5px;
}
.节点{
填写:#000;
冲程:#fff;
笔划宽度:1.5px;
}
.node.a{fill:#1f77b4;}
.node.b{fill:#ff7f0e;}
.node.c{fill:#2ca02c;}*/
.链接{
冲程:#ccc;
}
.节点文本{
指针事件:无;
字体:10px无衬线;
}
可变宽度=960,
高度=500;
var color=d3.scale.category10();
变量节点=[],
链接=[];
var-force=d3.layout.force()
.节点(节点)
.链接(links)
。收费(-400)
.linkDistance(120)
.尺寸([宽度、高度])
.在(“滴答”,滴答)上;
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var node=svg.selectAll(“.node”),
link=svg.selectAll(“.link”);
// 1. 添加三个节点和三个链接。
setTimeout(函数(){
变量a={id:“a”},b={id:“b”},c={id:“c”};
节点推送(a,b,c);
push({source:a,target:b},{source:a,target:c},{source:b,target:c});
start();
}, 0);
// 2. 删除节点B和相关链接。
setTimeout(函数(){
nodes.splice(1,1);//删除b
links.shift();//删除a-b
links.pop();//删除b-c
start();
}, 3000);
//向后添加节点B。
setTimeout(函数(){
var a=节点[0],b={id:“b”},c=节点[1];
节点推送(b);
push({source:a,target:b},{source:b,target:c});
start();
}, 6000);
函数start(){
link=link.data(force.links(),函数(d){返回d.source.id+“-”+d.target.id;});
//link.enter().insert(“line”,“node”).attr(“class”,“link”);
link.enter().append(“line”).attr(“class”,“link”);
link.exit().remove();
node=node.data(force.nodes(),函数(d){return d.id;});
node.enter().append(“g”)
.attr(“类”、“节点”)
.呼叫(强制拖动);
node.append(“图像”)
.attr(“xlink:href,”https://github.com/favicon.ico")
.attr(“x”,-8)
.attr(“y”,-8)
.attr(“宽度”,16)
.attr(“高度”,16);
node.append(“文本”)
.attr(“dx”,12)
.attr(“dy”,“.35em”)
.text(函数(d){console.log(d.id);返回d.id;});
//node.enter().append(“circle”).attr(“class”,函数(d){return“node”+d.id;}).attr(“r”,8);
node.exit().remove();
force.start();
}
函数tick(){
attr(“transform”,函数(d){return“translate”(“+d.x+”,“+d.y+”)”);});
attr(“x1”,函数(d){返回d.source.x;})
.attr(“y1”,函数(d){返回d.source.y;})
.attr(“x2”,函数(d){返回d.target.x;})
.attr(“y2”,函数(d){返回d.target.y;});
}
在代码执行之后,一些svg“g”节点有多个文本和图像子节点,尽管如我所支持的,它们应该在exit()部分中删除

我怎样才能修好它?谢谢你的帮助

编辑:

文本和图像节点复制示例:

<g class="node" transform="translate(411.8420674597886,245.93226853324168)">
    <image xlink:href="https://github.com/favicon.ico" x="-8" y="-8" width="16" height="16"></image>
    <text dx="12" dy=".35em">a</text>
    <image xlink:href="https://github.com/favicon.ico" x="-8" y="-8" width="16" height="16"></image>
    <text dx="12" dy=".35em">a</text>
    <image xlink:href="https://github.com/favicon.ico" x="-8" y="-8" width="16" height="16"></image>
    <text dx="12" dy=".35em">a</text>
</g>

A.
A.
A.
编辑2: 正如@LarsKotthoff所建议的:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

/*.link {
  stroke: #000;
  stroke-width: 1.5px;
}

.node {
  fill: #000;
  stroke: #fff;
  stroke-width: 1.5px;
}

.node.a { fill: #1f77b4; }
.node.b { fill: #ff7f0e; }
.node.c { fill: #2ca02c; }*/

.link {
  stroke: #ccc;
}

.node text {
  pointer-events: none;
  font: 10px sans-serif;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 500;

var color = d3.scale.category10();

var nodes = [],
    links = [];

var force = d3.layout.force()
    .nodes(nodes)
    .links(links)
    .charge(-400)
    .linkDistance(120)
    .size([width, height])
    .on("tick", tick);

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

var node = svg.selectAll(".node"),
    link = svg.selectAll(".link");

// 1. Add three nodes and three links.
setTimeout(function() {
  var a = {id: "a"}, b = {id: "b"}, c = {id: "c"};
  nodes.push(a, b, c);
  links.push({source: a, target: b}, {source: a, target: c}, {source: b, target: c});
  start();
}, 0);

// 2. Remove node B and associated links.
setTimeout(function() {
  nodes.splice(1, 1); // remove b
  links.shift(); // remove a-b
  links.pop(); // remove b-c
  start();
}, 3000);

// Add node B back.
setTimeout(function() {
  var a = nodes[0], b = {id: "b"}, c = nodes[1];
  nodes.push(b);
  links.push({source: a, target: b}, {source: b, target: c});
  start();
}, 6000);

function start() {
  link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
  //link.enter().insert("line", ".node").attr("class", "link");
  link.enter().append("line").attr("class", "link");
  link.exit().remove();

  node = node.data(force.nodes(), function(d) { return d.id;});
  var appendedG= node.enter().append("g")
    .attr("class", "node")
    .call(force.drag);
  appendedG.append("image")
    .attr("xlink:href", "https://github.com/favicon.ico")
    .attr("x", -8)
    .attr("y", -8)
    .attr("width", 16)
    .attr("height", 16);
  appendedG.append("text")
    .attr("dx", 12)
    .attr("dy", ".35em")
    .text(function (d) {console.log(d.id); return d.id;});
  //node.enter().append("circle").attr("class", function(d) { return "node " + d.id; }).attr("r", 8);
  node.exit().remove();

  force.start();
}

function tick() {
  node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });


  link.attr("x1", function(d) { return d.source.x; })
      .attr("y1", function(d) { return d.source.y; })
      .attr("x2", function(d) { return d.target.x; })
      .attr("y2", function(d) { return d.target.y; });
}

</script>

/*.链接{
行程:#000;
笔划宽度:1.5px;
}
.节点{
填写:#000;
冲程:#fff;
笔划宽度:1.5px;
}
.node.a{fill:#1f77b4;}
.node.b{fill:#ff7f0e;}
.node.c{fill:#2ca02c;}*/
.链接{
冲程:#ccc;
}
.节点文本{
指针事件:无;
字体:10px无衬线;
}
可变宽度=960,
高度=500;
var color=d3.scale.category10();
变量节点=[],
链接=[];
var-force=d3.layout.force()
.节点(节点)
.链接(links)
。收费(-400)
.linkDistance(120)
.尺寸([宽度、高度])
.在(“滴答”,滴答)上;
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var node=svg.selectAll(“.node”),
link=svg.selectAll(“.link”);
// 1. 添加三个节点和三个链接。
setTimeout(函数(){
变量a={id:“a”},b={id:“b”},c={id:“c”};
节点推送(a,b,c);
push({source:a,target:b},{source:a,target:c},{source:b,target:c});
start();
}, 0);
// 2. 删除节点B和相关链接。
setTimeout(函数(){
nodes.splice(1,1);//删除b
links.shift();//删除a-b
links.pop();//删除b-c
start();
}, 3000);
//向后添加节点B。
setTimeout(函数(){
var a=节点[0],b={id:“b”},c=节点[1];
节点推送(b);
push({source:a,target:b},{source:b,target:c});
start();
}, 6000);
函数start(){
link=link.data(force.links(),函数(d){返回d.source.id+“-”+d.target.id;});
//link.enter().insert(“line”,“node”).attr(“class”,“link”);
link.enter().append(“line”).attr(“class”,“link”);
link.exit().remove();
node=node.data(force.nodes(),函数(d){return d.id;});
var appendedG=node.enter().append(“g”)
.attr(“类”、“节点”)
.呼叫(强制拖动);
appendedG.append(“图像”)
.attr(“xlink:href,”https://github.com/favicon.ico")
.attr(“x”,-8)
.attr(“y”,-8)
.attr(“宽度”,16)
.attr(“高度”,16);
appendedG.append(“文本”)
.attr(“dx”,12)
.attr(“dy”,“.35em”)
.text(函数(d){console.log(d.id);返回d.id;});
//node.enter().append(“circle”).attr(“class”,函数(d){return“node”+d.id;}).attr(“r”,8);
node.exit().remove();
force.start();
}
函数tick(){
attr(“transform”,函数(d){return“translate”(“+d.x+”,“+d.y+”)”);});
attr(“x1”,函数(d){返回d.source.x;})
.attr(“y1”,函数(d){返回d.source.y;})
.attr(“x2”,函数(d){返回d.target.x;})
.attr(“y2”,函数(d){返回d.target.y;});
}
试试这个:

node = node.data(force.nodes(), function(d) { return d.id;});
var newNode = node.enter().append("g")
     .attr("class", "node")
     .call(force.drag);
newNode.append("image")
    .attr("xlink:href", "https://github.com/favicon.ico")
    .attr("x", -8)
    .attr("y", -8)
    .attr("width", 16)
    .attr("height", 16);
newNode.append("text")
    .attr("dx", 12)
    .attr("dy", ".35em")
    .text(function (d) {console.log(d.id); return d.id;});
  //node.enter().append("circle").attr("class", function(d) { return "node " + d.id;    }).attr("r", 8);
node.exit().remove();

看起来您在为更新添加
图像
文本
元素,而不是输入选择。你错过了那些
append
s前面的
.enter()
吗?@larskothoff不知道我是否理解正确。当我添加node.enter().append(“图像”)和node.enter().append(“文本”)时,和标记不再存储在标记中。此外,我注意到,即使这些标记是重复的。或者,您可以保存