javascript - 工具提示在我的热图中给出了错误的值
问题描述
我试图在我的热图上创建一个工具栏。热图和工具提示正在工作,但工具提示给了我错误的值。Tooltip 的输出总是给我这个值:
“确切的值是:未定义”
同样在调试控制台中,它给了我一条消息:
“rbug/1173575,非 JS 模块文件已弃用。”
这里有什么问题?
var tooltip = d3.select("#container")
.append("div")
.style("position", "absolute")
.style("visibility", "hidden")
.style("background", "#ffffff")
svg.selectAll()
.data(mapData.data, function(d) {return d.group +':'+ d.variable;})
.enter()
.append("rect")
.attr("x", function(d) { return x(d.variable) })
.attr("y", function(d) { return y(d.group) })
.attr("width", x.bandwidth() )
.attr("height", y.bandwidth() )
.style("fill", function(d) { return colour(d.value)})
.on("mouseover", function(d){
return tooltip.style("visibility", "visible");
})
.on("mousemove", function(d){
tooltip.html("The exact value is: " + d.value)
return tooltip.style("top", (d3.pointer(this)[1]) + "px").style("left",(d3.pointer(this)[0]+30) + "px");
})
.on("mouseleave", function(d){
return tooltip.style("visibility", "hidden");
})
<script src="https://d3js.org/d3.v6.js"></script>
<div id="container"> </id>
解决方案
代替:
svg.selectAll()
.data(mapData.data, function(d) {return d.group +':'+ d.variable;})
.enter()
.append("rect")
.attr("x", function(d) { return x(d.variable) })
.attr("y", function(d) { return y(d.group) })
.attr("width", x.bandwidth() )
.attr("height", y.bandwidth() )
.style("fill", function(d) { return colour(d.value)})
.on("mouseover", function(d){
return tooltip.style("visibility", "visible");
})
.on("mousemove", function(d){
tooltip.html("The exact value is: " + d.value)
return tooltip.style("top", (d3.pointer(this)[1]) + "px").style("left",(d3.pointer(this)[0]+30) + "px");
})
.on("mouseleave", function(d){
return tooltip.style("visibility", "hidden");
})
和:
svg.selectAll("rect")
.on("mouseenter", () => tooltip.style("visibility", "visible"))
.on("mousemove", (e, d) => {
tooltip.html("The exact value is: " + d.value);
tooltip
.style("top", `${e.layerY + 24}px`)
.style("left", `${e.layerX + 24}px`);
})
.on("mouseleave", () => tooltip.style("visibility", "hidden"))
推荐阅读
- python - Python:无法在 Windows 的子进程中终止子进程和使用 subprocess.Popen() 执行的命令
- c# - Xamarin API Json 列表到 ListView
- kotlin - 在kotlin中将lambda传递给joinToString?
- iis - IIS 重写模块:更改 set-cookie 路径规则
- javascript - 函数只返回第一个值
- r - 如何在 kaggle 上提交 rmd?
- javascript - 当 d3.js 中的单元格大小太小时删除文本
- javascript - 如果密码错误,Node 的加密解密是否总是抛出错误?
- javascript - 将多个观察者添加到 this.listener 到 React 组件类
- react-native - 是否可以将 React Navigation 与 snap.expo.io 一起使用?