[英]arcTween donut transition doesn't work in angular 4 when updating the chart
This is my update method, which is takes the new data as an input 这是我的更新方法,它将新数据作为输入
public updateChart(mockData?: any) {
this.path.data(this.pie(this.dataset))
.transition()
.duration(750)
.attrTween('d', <any> this.arcTween);}`
and this is my arcTween method: 这是我的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));
};
}
Problem when calling the specified method above: core.es5.js:1084 ERROR TypeError: Cannot set property 'arc' of undefined 调用上述指定方法时出现问题:core.es5.js:1084错误TypeError:无法设置未定义的属性'arc'
a closer inspection shows that also "this" is also undefined. 仔细检查表明,“ this”也未定义。
How can i fix this issue? 我该如何解决这个问题? It seems that d3.js is not suitable for angular 4 :S
似乎d3.js不适合角度4:S
i tried a lot of workarounds, but everything leads to this error 我尝试了很多解决方法,但是一切都会导致此错误
I think you are having a scope problem which you probably can solve like this: 我认为您遇到了范围问题,您可能可以这样解决:
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
becomes out of scope if used inside an internal function like this - it loses context. 如果在这样的内部函数中使用,
this
将超出范围-会丢失上下文。 One fix is how s.alem has changed the code to add a let that = this;
一个解决方法是s.alem如何更改代码以添加一个
let that = this;
and then use that
inside the function - that
will continue to hold the value of this
. 然后用
that
里面的功能- that
将继续持有的价值this
。 Another way to try to fix it is to turn it into an arrow function: 尝试修复它的另一种方法是将其变成箭头函数:
(pseudocode, I have not tried this, but you'll get the idea) (伪代码,我没有尝试过,但是您会明白的)
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
};
}
Another way is if this is a method written on an object, you can call it via that object. 另一种方法是,如果这是写在对象上的方法,则可以通过该对象调用它。 So if
arcTween
is a method on the animation
object, you could call it with 因此,如果
arcTween
是animation
对象上的方法,则可以使用
animation.arcTween()
and it will keep its this
intact. 它会保持它的
this
完整。
Hopefully one of those fixes the scope issue. 希望其中之一可以解决范围问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.