javascript - 在 D3.js 中使用数据连接处理元素的删除

标签 javascript d3.js

我在图表上有一组圆圈,其中包含以下数据并使用日期作为每个圆圈的键:

data = [
 {date: '2012-01-01', amount: 100 },
 {date: '2012-01-02', amount: 200 },
 {date: '2012-01-03', amount: 300 },
 {date: '2012-01-04', amount: 400 },
 {date: '2012-01-05', amount: 500 }
]

var circles = container.selectAll("g.circles")
    .data(data, function(d){ return d.date; });

// ENTER
circles.enter().append("circle")
  .attr("class","live")
  .attr("cy", function(d){return yScale(y(d,i)); })
  .attr("cx", function(d){return xScale(x(d,i)); })
.transition(500)
  .delay(function(d,i){return i*50;})
  .style("display",function(d){ return y(d,i) === null ? 'none' : 'inline'; })
  .attr("r", radius - 3 )
  .attr("fill", "#f7f7f7")
  .attr('clip-path','url(#rpm-clip2)')
  .each("end", events);

// TRANSITION
d3.transition(circles)
  .attr("cy", function(d){return yScale(y(d,i)); })
  .attr("cx", function(d){return xScale(x(d,i)); });

然后我使用以下数据集重新加载数据:

data = [
 {date: '2012-01-01', amount: 200 },
 {date: '2012-01-02', amount: 300 },
 {date: '2012-01-03', amount: 400 },
]

您会注意到,该数据集较短。我希望删除缺失的日期,但更新中并未触及它们。有什么想法吗?

最佳答案

您必须明确删除它们。 .exit() 集合包含所有没有数据的节点:

circles.exit().remove();

https://github.com/mbostock/d3/wiki/Selections#wiki-exit

关于javascript - 在 D3.js 中使用数据连接处理元素的删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12907619/

相关文章:

javascript - 使用 appendChild 将 iframe 移动到其他父级时如何防止 iframe 重新加载

javascript - D3 : Is it possible to prevent object to exit boundaries?

javascript - javascript中的数学到svg坐标转换

javascript - D3.js:缩放 svg 时错过刻度线

D3.js饼图: how to automatically create and assign gradual scale of colors based on my own categorization

javascript - 为此添加一些动态文本。在 native react 中

javascript - 选择器仅在检查/选择元素后工作

javascript - 使用整数列表的密度图

javascript - 使用服务在 Controller 之间共享数据 (AngularJS)

javascript - 同一页面上多个 d3 图表的问题