d3.js - 为什么鼠标悬停事件不适用于 d3.js 投影中的所有状态?
问题描述
我很难理解为什么鼠标悬停事件在我的投影中不一致地发生。我希望突出显示鼠标悬停的状态,该状态在大多数情况下都有效,但对于某些状态,鼠标悬停事件根本不会发生。谁能帮我理解这里的问题是什么?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
</head>
<body>
<script>
const width = 1000;
const height = 600;
const path = d3.geoPath(null);
const svg = d3.select("body")
.append("svg")
.attr("height", height)
.attr("width", width)
.style("display", "block")
.style("margin", "auto");
d3.json("https://d3js.org/us-10m.v1.json").then(data => {
svg.selectAll(".states")
.data(topojson.feature(data, data.objects.states).features)
.join("path")
.attr("d", path)
.style("fill", "none")
.attr("class", "states")
.style("stroke", "black")
.style("stroke-width", "2px")
.style("cursor", "pointer")
.on("mouseover", function(d, i) {
d3.select(this)
.transition(100)
.style("fill", "#6d7899");
})
.on("mouseout", function(d, i) {
d3.select(this)
.transition(100)
.style("fill", "none");
});
});
</script>
</body>
</html>
解决方案
我能够确定问题的根源。您必须将“填充”属性设置为实际值,而不是“无”,鼠标悬停事件才能正确识别投影中的地理边界。
推荐阅读
- oracle - 验证是否有逗号分隔的记录 PL SQL
- asp.net-mvc - Ext.net:如何设置组合框以更改外键字段并通过 AutoAsync() 保存?
- java - 如何从firebase云存储检索数据到片段类,例如也使用MVVM的ProfileFragment.class?
- hibernate - 自定义嵌入主键的自动生成名称
- sql - 参数化动态 where 子句
- reactjs - npm run build 不包括 /public/images/images.png
- rsa - 正确格式化 RSA 签名以从 Google Cloud 进行验证
- python - 离线安装请求且无需 pip
- forms - Symfony 4:JSON 到表单集合
- java - 如何使用完全自定义的有效负载制作 JWT