D3删除dbclick

时间:2017-09-23 22:33:47

标签: javascript d3.js draggable geometry removeall

我正在使用Bostock的Circle Dragging I和Will的D3 Mouse Event所以我可以点击svg创建一个圆圈,并且它们都是可拖动的。这是有效的,虽然如果我在创建圆圈时双击会有问题,有时拖动圆圈会导致它们四处跳跃。

但主要问题是我希望能够双击一个圆圈并使其消失,但也会将其从数据中删除。

绘制圆圈时,我添加了一个调用函数的dbclick事件

  function removeElement(d) {
    // need to remove this object from data
    d3.select(this)
      .exit()
      .remove();
  }

创建新圆时也会调用此函数。

此功能不会删除圈子,这样做的正确方法是什么? 单击一次做什么和双击做其他事情之间是否存在冲突?

var svg = d3.select("svg"),
  width = +svg.attr("width"),
  height = +svg.attr("height"),
  radius = 32;

var data = [{
    x: 100,
    y: 200
  },
  {
    x: 200,
    y: 300
  },
  {
    x: 300,
    y: 200
  },
  {
    x: 400,
    y: 300
  }
];

var xScale = d3.scaleLinear()
  .domain([0, d3.max(data, function(d) {
    return d.x_pos
  })]).range([0, width]);

svg.selectAll("circle")
  .data(data)
  .enter().append("circle")
  .attr("cx", function(d) {
    return d.x;
  })
  .attr("cy", function(d) {
    return d.y;
  })
  .attr("r", radius)
  .style("fill", "lightblue")
  .attr('id', function(d, i) {
    return 'rect_' + i;
  })
  .call(d3.drag()
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended))
  .on("dblclick", removeElement());

svg.on("click", function() {
  var coords = d3.mouse(this);

  var newData = {
    x: d3.event.x,
    y: d3.event.y
  };

  data.push(newData);

  svg.selectAll("circle") // For new circle, go through the update process
    .data(data)
    .enter()
    .append("circle")
    .attr("cx", function(d) {
      return d.x;
    })
    .attr("cy", function(d) {
      return d.y;
    })
    .attr("r", radius)
    .style("fill", "red")
    .attr('id', function(d, i) {
      return 'circle_' + i;
    })
    .call(d3.drag()
      .on("start", dragstarted)
      .on("drag", dragged)
      .on("end", dragended))
    .on("dblclick", removeElement());
})

function dragstarted(d) {
  d3.select(this).raise().classed("active", true);
}

function dragged(d) {
  d3.select(this)
    .attr("cx", d.x = d3.event.x)
    .attr("cy", d.y = d3.event.y);
}

function dragended(d) {
  d3.select(this)
    .classed("active", false);
}

function removeElement(d) {
  // need to remove this object from data
  d3.select(this)
    .exit()
    .remove();
}
.active {
  stroke: #000;
  stroke-width: 2px;
}
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>

1 个答案:

答案 0 :(得分:2)

您的代码面临的最大问题是双击即可发出咔嗒声。但是,由于您具体询问了如何删除圆圈,因此此答案仅处理该问题。

您删除圈子的代码有两个问题。

首先,这......

.on("dblclick", removeElement())

...会立即调用removeElement并返回其值(顺便说一下,它是undefined)。这不是你想要的。

相反,这样做:

.on("dblclick", removeElement) 

以下是:

.on("dbclick", function(d){
    removeElement(d);
}

这样,removeElement只会在用户点击圈子时调用,而不是立即调用。

第二个问题是:

d3.select(this).exit().remove();

由于仍有与该圈子相关的数据,您的退出&#34;选择是空的。

而不是那样,它应该是:

d3.select(this).remove();

以下是包含这些更改的代码:

&#13;
&#13;
var svg = d3.select("svg"),
  width = +svg.attr("width"),
  height = +svg.attr("height"),
  radius = 32;

var data = [{
    x: 100,
    y: 200
  },
  {
    x: 200,
    y: 300
  },
  {
    x: 300,
    y: 200
  },
  {
    x: 400,
    y: 300
  }
];

var xScale = d3.scaleLinear()
  .domain([0, d3.max(data, function(d) {
    return d.x_pos
  })]).range([0, width]);

svg.selectAll("circle")
  .data(data)
  .enter().append("circle")
  .attr("cx", function(d) {
    return d.x;
  })
  .attr("cy", function(d) {
    return d.y;
  })
  .attr("r", radius)
  .style("fill", "lightblue")
  .attr('id', function(d, i) {
    return 'rect_' + i;
  })
  .call(d3.drag()
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended))
  .on("dblclick", removeElement);



function dragstarted(d) {
  d3.select(this).raise().classed("active", true);
}

function dragged(d) {
  d3.select(this)
    .attr("cx", d.x = d3.event.x)
    .attr("cy", d.y = d3.event.y);
}

function dragended(d) {
  d3.select(this)
    .classed("active", false);
}

function removeElement(d) {
  // need to remove this object from data
  d3.select(this)
    .remove();
}
&#13;
.active {
  stroke: #000;
  stroke-width: 2px;
}
&#13;
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

PS:我删除了SVG上的点击以创建圈子。由于该问题(区分点击与双击)非常复杂,因此可能值得一个新的,分开的问题。

相关问题