admin管理员组文章数量:1389779
I am working with a d3 scatterplot and I want my tooltip to appear beside a dot when I mouse over that dot. I know this is very simple but I don't know where I have gone wrong! This is the relevant code.It throws this error:
Uncaught TypeError: Cannot read property 'pageX' of null
If anyone could point me in the right direction I would be very grateful as I am new to d3! Thanks in advance.
// add the tooltip area to the webpage
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function mouseHandler (d) {
d3.json("connection3.php?paperID="+d.ID, function(error,data) {
var authorList = ""
data.forEach(function(d) {
authorList = authorList + d.AUTHOR;
console.log(authorList);
})
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(authorList)
.style("left",(d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
}
css:
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
background: lightsteelblue;
}
I am working with a d3 scatterplot and I want my tooltip to appear beside a dot when I mouse over that dot. I know this is very simple but I don't know where I have gone wrong! This is the relevant code.It throws this error:
Uncaught TypeError: Cannot read property 'pageX' of null
If anyone could point me in the right direction I would be very grateful as I am new to d3! Thanks in advance.
// add the tooltip area to the webpage
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function mouseHandler (d) {
d3.json("connection3.php?paperID="+d.ID, function(error,data) {
var authorList = ""
data.forEach(function(d) {
authorList = authorList + d.AUTHOR;
console.log(authorList);
})
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(authorList)
.style("left",(d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
}
css:
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
background: lightsteelblue;
}
Share Improve this question edited Mar 4, 2016 at 10:28 Paul Smith asked Mar 4, 2016 at 10:21 Paul SmithPaul Smith 1872 silver badges13 bronze badges 1- Can you reproduce it in codepen or jsfiddle? It will be easy to debug. – Thomas Sebastian Commented Mar 4, 2016 at 10:48
1 Answer
Reset to default 7The lines:
.style("left",(d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
would work well if they were directly inside the mousehandler function. However, they are within a callback function for d3.json
, so it would explain why d3.event
is no longer defined.
So try to save the values of pageX
and pageY
to local variables before calling d3.json.
function mouseHandler (d) {
var pageX=d3.event.pageX;
var pageY=d3.event.pageY;
d3.json("connection3.php?paperID="+d.ID, function(error,data) {
var authorList = ""
data.forEach(function(d) {
authorList = authorList + d.AUTHOR;
console.log(authorList);
})
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(authorList)
.style("left",(pageX) + "px")
.style("top", (pageY - 28) + "px");
})
}
本文标签: javascriptd3js Tooltip positioning not workingStack Overflow
版权声明:本文标题:javascript - d3.js Tooltip positioning not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744582849a2614028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论