【发布时间】:2017-03-22 15:19:04
【问题描述】:
利用 Mike Bostocks 美国县 bl.ock:https://bl.ocks.org/mbostock/4122298。
目标是创建一个 mousemove 函数,该函数在 mousemove 事件期间将突出显示所有选定的县。当前示例仅使用:hover 突出显示并且不保留选择。
<!DOCTYPE html>
<style>
.counties :hover {
fill: red;
}
.county-borders {
fill: none;
stroke: #fff;
stroke-width: 0.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var svg = d3.select("svg");
var path = d3.geoPath();
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("d", path);
svg.append("path")
.attr("class", "county-borders")
.attr("d", path(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b; })));
});
</script>
【问题讨论】:
-
所以你希望你悬停的所有国家都保持红色,对吗?鼠标痕迹?
-
@undko 是的,没错。我以前没有听说过这个词,但这本质上是目标。只要按下鼠标,它就会跟踪鼠标的移动。
-
我应该把“鼠标痕迹”放在引号中,因为这是我个人的说法(甚至不是母语人士:-)
标签: javascript d3.js topojson