I have appended 5 rect
s on my SVG and tried to bind mouseOver in each element which will alter their color fill individually when hovered.
Problem:
How do you call this.d3
within calling element's function?
I tried this.d3.select(this)
but it will of course reference the current Angular Component
I tried this.d3.select(this)
but this
keyword references the current Angular Component
As mentioned by @yurzui, this
doesn't refer to the Angular Component but instead refers to the element itself.
private d3: D3;
foo(): void {
let x = 0;
for (let i = 0; i < 5; i ++) {
this.svg.append('rect')
.attr('x', x)
.attr('y', 100)
.styles({
'width' : 100,
'height' : 100,
'fill' : '#2196F3'
})
.on('mouseover', this.bar);
x += 110;
}
}
bar(d, i) {
// Change the color of the hovered rect
// this is referencing the current element
// unable to call this.d3 since this is referencing the element
this.d3.select(this)
.attr('fill', 'red');
}