简体   繁体   English

D3:使用动态名称循环转换

[英]D3: looping transition with dynamic name

I understand you can't really transition events using a javascript loop when it comes to D3. 据我了解,对于D3,您无法真正使用javascript循环来转换事件。 But is there any reason to your knowledge why you cannot use dynamic names (eg dynamic path name)? 但是,据您所知,为什么没有使用动态名称(例如动态路径名称)的理由? For some reason the following won't work (all I am trying to do is to get Japan to 'glow' continuously on the map ...) 出于某种原因,以下操作将不起作用(我要做的就是让日本在地图上不断“发光”……)

Please note: the first bit of the animation works and japan goes red. 请注意:动画的第一部分起作用,而日本变成红色。 The animation then ceases, even though the console correctly yields "#Japan" and loops to infinity. 即使控制台正确产生“ #Japan”并循环到无穷大,动画也将停止。

paz = "#Japan";
glowit(paz)

function glowit(paz){
console.log(paz);
d3.select(paz)
  .transition()
  .style("fill", "red")
  .duration(1000)
  .each("end", function(){
     d3.select(paz)
       .transition()
       .style("fill", "white")
       .duration(1000)
       .each("end", glowit("#" + this.id))
  });
}

Kind regards -and thank you, G. 亲切的问候-谢谢你,G。

A problem may be that you call glowit() instead of giving the function as parameter to each() . 问题可能是您调用 glowit()而不是将函数作为参数提供给each() You need to wrap it in a function: 您需要将其包装在一个函数中:

function glowit(paz){
  console.log(paz);
  d3.select(paz)
    .transition()
    .style("fill", "red")
    .duration(1000)
    .each("end", function(){
       d3.select(paz)
         .transition()
         .style("fill", "white")
         .duration(1000)
         .each("end", function () {
            glowit("#" + this.id);
         });
    });
}

Just to explain why @Joachim told you to wrap glowit inside an anonymous function, when you do each("end", glowit(paz)) , you are executing glowit immediately and passing its result to each . 只是为了解释为什么@Joachim告诉您将glowit包裹在一个匿名函数中,当您执行each("end", glowit(paz)) ,您将立即执行glowit并将其结果传递给each But wrapping it in an anonymous function makes it an argument to each . 但是将其包装在匿名函数中会使它成为each函数的参数。

For instance, if glowit had no arguments, you could simply eliminate the () , and it would not execute immediately. 例如,如果glowit没有参数,则只需消除() ,它就不会立即执行。 This code has the same effect: 此代码具有相同的效果:

glowit();

function glowit(){
  d3.select("#Japan")
  .transition()
  .style("fill", "red")
  .duration(1000)
  .each("end", function(){
    d3.select("#Japan")
    .transition()
    .style("fill", "white")
    .duration(1000)
    .each("end", glowit);//without the parentheses
  });
};

Test it and you'll see. 测试它,您将看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM