D3.js (D3)数据驱动文档--转换重复

D3.js (D3)数据驱动文档--转换重复,d3.js,transition,data-driven,D3.js,Transition,Data Driven,我已经找了3个多小时了,现在正试图找到一种无限期链接转换的方法 我唯一的解决方案是将代码包装到函数中,然后使用setInterval重复调用该函数或等待转换“end”事件 示例一班轮: d3.selectAll('circle').data([1,2,3]).enter().append('circle').attr('cy':function(d){return d * 100},'cx':function(){Math.random() * window.innerWidth},'r':'1

我已经找了3个多小时了,现在正试图找到一种无限期链接转换的方法

我唯一的解决方案是将代码包装到函数中,然后使用setInterval重复调用该函数或等待转换“end”事件

示例一班轮:

d3.selectAll('circle').data([1,2,3]).enter().append('circle').attr('cy':function(d){return d * 100},'cx':function(){Math.random() * window.innerWidth},'r':'10px')

//sets initial locations for circles that are created to match data array

.transition().attr('cy':function(){Math.random() * window.innerHeight},'cx':function(){Math.random() * window.innerWidth}})
.transition().attr('cy':function(){Math.random() * window.innerHeight},'cx':function(){Math.random() * window.innerWidth}})
.transition().attr('cy':function(){Math.random() * window.innerHeight},'cx':function(){Math.random() * window.innerWidth}})
.transition().attr('cy':function(){Math.random() * window.innerHeight},'cx':function(){Math.random() * window.innerWidth}})
.transition().attr('cy':function(){Math.random() * window.innerHeight},'cx':function(){Math.random() * window.innerWidth}})

//I'm looking for something that can repeat the transition without using setInterval)

无论如何,我认为您必须将转换设置包装到一个函数中,以便可以递归地调用它。然而,@Jon S.是对的,您可以使用转换的“结束”事件而不是单独的计时器

复杂的是
转换中的回调函数。每个(“end”,callback)
都是为数组中的每个元素调用的(正如方法名称所示)。一个简单的检查可以确保递归只发生一次,对于第一个元素,并且不会无限期地分支

下面是一个例子:

关键代码:

var moving = false;

function move(selection) {

    moving = true;
    selection.transition().duration(5000).ease("sin-in-out")
        .attr("cx", function(){return Math.random()*width;})
        .attr("cy", function(){return Math.random()*height;})
        //.call(move); //stack overflow error!
        .each("end", function(d,i){ 
            if (!i) circles.call(move); //only run for i==0
        });

}
从注释中可以看出,我尝试使用转换的
.call()
方法(对整个转换对象调用一次函数),但目前无法将调用延迟到转换结束,因此,重复的子转换调用被添加到队列中,直到控制台抛出一个错误,后面有一个巨大的堆栈跟踪。奇怪的是,它看起来并没有什么问题,因为所有排队的转换仍在顺利进行——但它们最终会用完转换


each/end方法——在原始选择上调用递归函数——替换完成的转换,而不是链接到它,因此它可以无限期地继续,而不会消耗越来越多的资源。

等待转换结束事件有什么错,然后调用另一个转换?我想在不离开命令链的情况下调用同一个转换。我希望能够每隔几秒钟调用最后N个步骤(无论是转换还是数据更改时某个数据对象到DOM元素的重复绑定)是的,我知道我可以这样做,但我希望避免函数声明,而不仅仅是“setInterval(move,1000)”那么,在这种情况下,您的问题的答案是否定的,转换是不可能的。过渡是为了从一种状态过渡到另一种状态而设计的,而不是为了不确定的动画。也许如果你更新了问题,用更具体的例子说明为什么你认为函数调用不起作用,那么我们可以帮助你实现它。