我发现我工作的解决方案。我觉得它仍然是难看的代码,但至少它是有效的。
首先我创建一个unvisible对象在我的数据集的每个数据:
g.data(dat)
.enter()
.append("circle")
.attr("class", function(d,i) { return "default " + d.pn; })
.attr("cx", function(d,i) { return d.x1; })
.attr("cy", function(d,i) { return d.y1; })
.attr("r", function(d,i) { return d.r; })
.style("opacity", 0);
然后我去shedule每个单一元素的过渡:
g.each(function(d,i){
var delay = Math.floor((i+1)/nrOfElements);
//console.log(delay);
d3.select(this)
.transition()
.delay(delay * speed)
.duration(speed + DELAY_OFFSET)
.style("opacity", 100)
.attr("cx", function(d) { return i<dat.length-1 ? dat[i+1].x1 : dat[i].x1; })
.attr("cy", function(d) { return i<dat.length-1 ? dat[i+1].y1 : dat[i].y1; })
.attr("r", function(d) { return i<dat.length-1 ? dat[i+1].r : dat[i].r; })
.style("stroke", "blue") // TODO: setAttributes
.each("end", function() {
// Reset to privious attributes
d3.select(this)
.attr("cx", function(d) { return dat[i].x1; })
.attr("cy", function(d) { return dat[i].y1; })
.attr("r", function(d) { return dat[i].r; })
;
if(i<dat.length-nrOfElements) d3.select(this).style("opacity", 0);
})
;
});
这似乎是一个巨大的努力在编码给我一个相当简单的要求...但至少它工作..
更好的解决方案仍然欢迎!
来源
2013-01-06 09:50:24
KIC
但是如果你这样做,最后的转换将覆盖所有的私人一次,所以只有一个动画可见。 – KIC