运行 D3 更新/进入/退出时,如何在新退出期间忽略已退出的元素?

Lea*_*ple 5 transition updates exit d3.js

每个人都知道更新/进入/退出模式:

function update(data) {

  // DATA JOIN
  // Join new data with old elements, if any.
  var text = svg.selectAll("text")
      .data(data);

  // UPDATE
  // Update old elements as needed.
  text.attr("class", "update");

  // ENTER
  // Create new elements as needed.
  text.enter().append("text")
      .attr("class", "enter")
      .attr("x", function(d, i) { return i * 32; })
      .attr("dy", ".35em");

  // ENTER + UPDATE
  // Appending to the enter selection expands the update selection to include
  // entering elements; so, operations on the update selection after appending to
  // the enter selection will apply to both entering and updating nodes.
  text.text(function(d) { return d; });

  // EXIT
  // Remove old elements as needed.
  text.exit().transition().duration(2000).attr('transform',
    rotation = 25;
    return 'translate(' + d.x + ',' + d.y + ') rotate('+ rotation +' 30 30)';
  }).remove();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

如果我在退出仍在发生时运行此函数(在 2000 毫秒转换窗口期间),如何防止在已经进行exit转换的元素上调用转换。

换句话说,当进行原始选择时,它包括已经处于该exit().transition()状态的元素。因此,如果这些元素没有完成它们的转换 和 ,则第二次在这些元素上调用退出转换removed()。

我需要做一个过滤器吗?有测试in transition while exiting吗?

mgr*_*ham 5

D3.js:停止转换被中断? 说给退出元素一个特殊的类,然后使用它只允许新退出的元素进入退出转换

.exit()
     .filter(function(d,i) {
        return !d3.select(this).classed("exiting");
     })
     .classed("exiting", true)
     .transition()
     ...
Run Code Online (Sandbox Code Playgroud)

一个更大的问题是在您第二次点击时重新引入的任何数据,而这些数据仍受第一次退出转换的影响。在您的示例中,这将消失您想要保留的元素,因为退出转换完成并对该元素调用删除。过去为了解决这个问题,我在我的更新中添加了即时(零延迟,零持续时间)转换并输入选择以覆盖/写入退出选择,如果他们还没有自己的转换(如果有更好的方法)有人告诉我)。

这导致了另一点:.classed("exiting", false)在输入和更新中设置,因此在 'exiting'>'re-introduced'>'exiting again' 元素的情况下,.exiting 类未设置,退出转换被激活。