[英]arcTween donut transition doesn't work in angular 4 when updating the chart
这是我的更新方法,它将新数据作为输入
public updateChart(mockData?: any) {
this.path.data(this.pie(this.dataset))
.transition()
.duration(750)
.attrTween('d', <any> this.arcTween);}`
这是我的arcTween方法:
public arcTween(a: any): any {
let i = d3.interpolate(this.current, a);
this.current = i(0);
return function (t) {
return this.arc(i(t));
};
}
调用上述指定方法时出现问题:core.es5.js:1084错误TypeError:无法设置未定义的属性'arc'
仔细检查表明,“ this”也未定义。
我该如何解决这个问题? 似乎d3.js不适合角度4:S
我尝试了很多解决方法,但是一切都会导致此错误
我认为您遇到了范围问题,您可能可以这样解决:
public arcTween(a: any): any {
let i = d3.interpolate(this.current, a);
let that = this;
this.current = i(0);
return function (t) {
return that.arc(i(t));
};
}
如果在这样的内部函数中使用, this
将超出范围-会丢失上下文。 一个解决方法是s.alem如何更改代码以添加一个let that = this;
然后用that
里面的功能- that
将继续持有的价值this
。 尝试修复它的另一种方法是将其变成箭头函数:
(伪代码,我没有尝试过,但是您会明白的)
public arcTween(a: any): any {
let i = d3.interpolate(this.current, a);
this.current = i(0);
return t() => {
return this.arc(i(t)); //this will be as expected
};
}
另一种方法是,如果这是写在对象上的方法,则可以通过该对象调用它。 因此,如果arcTween
是animation
对象上的方法,则可以使用
animation.arcTween()
它会保持它的this
完整。
希望其中之一可以解决范围问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.