D3 - Mouseover rect width












1















I have a Stackblitz here - https://stackblitz.com/edit/ng-graph-tooltip?file=src%2Fapp%2Fapp.component.html



I have a mouseover event when hovering over the bars



I trying to get the width and x pos of the bar when over it so I can position the tooltip



How can I get the width and x pos when over the bar.



Is this a problem with 'this' in Angular



d3.selectAll('.bar-opacity').on("mouseover", (d:any) => {

const xPos = d3.select(this).attr('x');
const rectWidth = d3.select(this).attr('width');

console.log(xPos)

let html = ''
html += '<div class="">'+d.color+'</div>'
html += '<div class="tooltip-block">'+d.day+'</div>'

d3.select('.chart-tooltip')
.style("left", d3.event.pageX + 5 + "px")
.style("top", d3.event.pageY - 25 + "px")
.html(html)

d3.select('.chart-tooltip').style("display", null)
})

.on("mouseout", ()=>{
d3.select('.chart-tooltip').style("display", "none")
.transition()
.duration(500)
})









share|improve this question























  • First thing I noticed - Do not use arrow function in D3 event handlers. Try using it as a normal function to use this.

    – CRayen
    Nov 20 '18 at 12:48











  • This works here but in my actual code I'm using variables outside this function like this.margin.top in this example this doesn't work when the function is like that - stackblitz.com/edit/…

    – ttmt
    Nov 20 '18 at 13:13











  • What is the problem? Using a normal function like you did in the stackblitz allows you to successfully get the width and pos...

    – Zim
    Nov 20 '18 at 13:23













  • Using a normal function I cant access things like this.margin.top. I'll post a separate question

    – ttmt
    Nov 20 '18 at 13:27











  • As explained here: stackoverflow.com/questions/53204937/… you can use an arrow function with the third argument of the callback (datum,index,elements) => { const coords = d3.mouse(elements[index]); };

    – David Lemon
    Nov 20 '18 at 14:03
















1















I have a Stackblitz here - https://stackblitz.com/edit/ng-graph-tooltip?file=src%2Fapp%2Fapp.component.html



I have a mouseover event when hovering over the bars



I trying to get the width and x pos of the bar when over it so I can position the tooltip



How can I get the width and x pos when over the bar.



Is this a problem with 'this' in Angular



d3.selectAll('.bar-opacity').on("mouseover", (d:any) => {

const xPos = d3.select(this).attr('x');
const rectWidth = d3.select(this).attr('width');

console.log(xPos)

let html = ''
html += '<div class="">'+d.color+'</div>'
html += '<div class="tooltip-block">'+d.day+'</div>'

d3.select('.chart-tooltip')
.style("left", d3.event.pageX + 5 + "px")
.style("top", d3.event.pageY - 25 + "px")
.html(html)

d3.select('.chart-tooltip').style("display", null)
})

.on("mouseout", ()=>{
d3.select('.chart-tooltip').style("display", "none")
.transition()
.duration(500)
})









share|improve this question























  • First thing I noticed - Do not use arrow function in D3 event handlers. Try using it as a normal function to use this.

    – CRayen
    Nov 20 '18 at 12:48











  • This works here but in my actual code I'm using variables outside this function like this.margin.top in this example this doesn't work when the function is like that - stackblitz.com/edit/…

    – ttmt
    Nov 20 '18 at 13:13











  • What is the problem? Using a normal function like you did in the stackblitz allows you to successfully get the width and pos...

    – Zim
    Nov 20 '18 at 13:23













  • Using a normal function I cant access things like this.margin.top. I'll post a separate question

    – ttmt
    Nov 20 '18 at 13:27











  • As explained here: stackoverflow.com/questions/53204937/… you can use an arrow function with the third argument of the callback (datum,index,elements) => { const coords = d3.mouse(elements[index]); };

    – David Lemon
    Nov 20 '18 at 14:03














1












1








1








I have a Stackblitz here - https://stackblitz.com/edit/ng-graph-tooltip?file=src%2Fapp%2Fapp.component.html



I have a mouseover event when hovering over the bars



I trying to get the width and x pos of the bar when over it so I can position the tooltip



How can I get the width and x pos when over the bar.



Is this a problem with 'this' in Angular



d3.selectAll('.bar-opacity').on("mouseover", (d:any) => {

const xPos = d3.select(this).attr('x');
const rectWidth = d3.select(this).attr('width');

console.log(xPos)

let html = ''
html += '<div class="">'+d.color+'</div>'
html += '<div class="tooltip-block">'+d.day+'</div>'

d3.select('.chart-tooltip')
.style("left", d3.event.pageX + 5 + "px")
.style("top", d3.event.pageY - 25 + "px")
.html(html)

d3.select('.chart-tooltip').style("display", null)
})

.on("mouseout", ()=>{
d3.select('.chart-tooltip').style("display", "none")
.transition()
.duration(500)
})









share|improve this question














I have a Stackblitz here - https://stackblitz.com/edit/ng-graph-tooltip?file=src%2Fapp%2Fapp.component.html



I have a mouseover event when hovering over the bars



I trying to get the width and x pos of the bar when over it so I can position the tooltip



How can I get the width and x pos when over the bar.



Is this a problem with 'this' in Angular



d3.selectAll('.bar-opacity').on("mouseover", (d:any) => {

const xPos = d3.select(this).attr('x');
const rectWidth = d3.select(this).attr('width');

console.log(xPos)

let html = ''
html += '<div class="">'+d.color+'</div>'
html += '<div class="tooltip-block">'+d.day+'</div>'

d3.select('.chart-tooltip')
.style("left", d3.event.pageX + 5 + "px")
.style("top", d3.event.pageY - 25 + "px")
.html(html)

d3.select('.chart-tooltip').style("display", null)
})

.on("mouseout", ()=>{
d3.select('.chart-tooltip').style("display", "none")
.transition()
.duration(500)
})






javascript angular d3.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 12:31









ttmtttmt

2,0671149101




2,0671149101













  • First thing I noticed - Do not use arrow function in D3 event handlers. Try using it as a normal function to use this.

    – CRayen
    Nov 20 '18 at 12:48











  • This works here but in my actual code I'm using variables outside this function like this.margin.top in this example this doesn't work when the function is like that - stackblitz.com/edit/…

    – ttmt
    Nov 20 '18 at 13:13











  • What is the problem? Using a normal function like you did in the stackblitz allows you to successfully get the width and pos...

    – Zim
    Nov 20 '18 at 13:23













  • Using a normal function I cant access things like this.margin.top. I'll post a separate question

    – ttmt
    Nov 20 '18 at 13:27











  • As explained here: stackoverflow.com/questions/53204937/… you can use an arrow function with the third argument of the callback (datum,index,elements) => { const coords = d3.mouse(elements[index]); };

    – David Lemon
    Nov 20 '18 at 14:03



















  • First thing I noticed - Do not use arrow function in D3 event handlers. Try using it as a normal function to use this.

    – CRayen
    Nov 20 '18 at 12:48











  • This works here but in my actual code I'm using variables outside this function like this.margin.top in this example this doesn't work when the function is like that - stackblitz.com/edit/…

    – ttmt
    Nov 20 '18 at 13:13











  • What is the problem? Using a normal function like you did in the stackblitz allows you to successfully get the width and pos...

    – Zim
    Nov 20 '18 at 13:23













  • Using a normal function I cant access things like this.margin.top. I'll post a separate question

    – ttmt
    Nov 20 '18 at 13:27











  • As explained here: stackoverflow.com/questions/53204937/… you can use an arrow function with the third argument of the callback (datum,index,elements) => { const coords = d3.mouse(elements[index]); };

    – David Lemon
    Nov 20 '18 at 14:03

















First thing I noticed - Do not use arrow function in D3 event handlers. Try using it as a normal function to use this.

– CRayen
Nov 20 '18 at 12:48





First thing I noticed - Do not use arrow function in D3 event handlers. Try using it as a normal function to use this.

– CRayen
Nov 20 '18 at 12:48













This works here but in my actual code I'm using variables outside this function like this.margin.top in this example this doesn't work when the function is like that - stackblitz.com/edit/…

– ttmt
Nov 20 '18 at 13:13





This works here but in my actual code I'm using variables outside this function like this.margin.top in this example this doesn't work when the function is like that - stackblitz.com/edit/…

– ttmt
Nov 20 '18 at 13:13













What is the problem? Using a normal function like you did in the stackblitz allows you to successfully get the width and pos...

– Zim
Nov 20 '18 at 13:23







What is the problem? Using a normal function like you did in the stackblitz allows you to successfully get the width and pos...

– Zim
Nov 20 '18 at 13:23















Using a normal function I cant access things like this.margin.top. I'll post a separate question

– ttmt
Nov 20 '18 at 13:27





Using a normal function I cant access things like this.margin.top. I'll post a separate question

– ttmt
Nov 20 '18 at 13:27













As explained here: stackoverflow.com/questions/53204937/… you can use an arrow function with the third argument of the callback (datum,index,elements) => { const coords = d3.mouse(elements[index]); };

– David Lemon
Nov 20 '18 at 14:03





As explained here: stackoverflow.com/questions/53204937/… you can use an arrow function with the third argument of the callback (datum,index,elements) => { const coords = d3.mouse(elements[index]); };

– David Lemon
Nov 20 '18 at 14:03












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393052%2fd3-mouseover-rect-width%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393052%2fd3-mouseover-rect-width%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]