2

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

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 },
]

您会注意到,这个数据集更短。我希望删除缺少的日期,但在更新中不会触及它们。有任何想法吗?

4

1 回答 1

3

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

circles.exit().remove();

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

于 2012-10-16T04:52:33.680 回答