简体   繁体   English

D3日光下具有更新的数据:多个过渡仍然会捕捉,不平滑

[英]D3 sunburst with updated data: multiple transitions still snap, not smooth

This question builds on Lars Kotthoff's very helpful answer to the problem of transitioning with a D3 sunburst diagram that is based on different JSON data: d3 - sunburst - transition given updated data -- trying to animate, not snap 这个问题基于Lars Kotthoff对基于不同JSON数据的D3朝阳图过渡的非常有帮助的答案: d3-朝阳-给定更新数据的过渡-尝试制作动画,而不是捕捉

I've tried the final fiddle that Lars provided but when there is more than one transition, the animation still fails and we get snapping. 我已经尝试了Lars提供的最后的提琴,但是当存在多个过渡时,动画仍然会失败,并且我们会捕捉到。 The problem can be seen in this updated fiddle that contains a second transition . 包含第二个过渡的更新的小提琴中可以看到问题所在。

From what I can tell, the x0 and dx0 values are not properly stored with the path object when calling the arcTweenUpdate function. 据我所知,调用arcTweenUpdate函数时, x0dx0值未与路径对象正确存储。 When I check what the this object looks like inside arcTweenUpdate function, I get an [object SVGPathElement] at the beginning of the function when this.x0 and this.dx0 are read, and I get an [object Window] when the new values are written later. 当我在arcTweenUpdate函数中检查this对象的外观时,当读取this.x0this.dx0时,在函数的开头得到一个[object SVGPathElement],当新值是以后写。 I'm relatively inexperienced with JS but that seemed like it could point to the problem. 我对JS经验不足,但这似乎可以指出问题所在。

Any help with addressing this and making the above fiddle work for multiple transitions (eg back and forth between the two JSONs) is highly appreciated. 非常感谢您提供任何帮助解决此问题的方法,并且可以使上面的提琴工作多次转换(例如,在两个JSON之间来回转换)。 Thanks! 谢谢!

Well you've spotted a bug in my earlier answer :) 好吧,您在我之前的答案中发现了一个错误:)

The problem is, as you say, that the saved values aren't updated properly. 正如您所说,问题是保存的值没有正确更新。 That's because this inside the callback doesn't refer to the path DOM element anymore. 这是因为this回调中不参照path DOM元素了。 The fix is simple -- save a reference to this in the function at the level above and use that reference: 修复很简单-在以上级别的函数中保存this的引用,并使用该引用:

function arcTweenUpdate(a) {
  var i = d3.interpolate({x: this.x0, dx: this.dx0}, a);
  var that = this;
  return function(t) {
    var b = i(t);
    that.x0 = b.x;
    that.dx0 = b.dx;
    return arc(b);
  };
}

Complete demo here . 在此处完成演示。

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

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