【发布时间】:2016-12-14 18:44:03
【问题描述】:
有没有办法将 OpenStreetmap Leaflet.js 地图与 d3.js 对象结合起来,以捕获 d3 对象上的“鼠标悬停”工具提示?在以下示例中,当鼠标经过蓝色圆圈时,我想创建一个控制台“ok”事件:
<!DOCTYPE html>
<html>
<head>
<title>d3.js with leaflet.js</title>
<script src="http://d3js.org/d3.v4.min.js">
</script>
<script src="https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.js">
</script>
<link rel="stylesheet" href="https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.css">
</head>
<body>
<div id="map" style="width: 1350px; height: 662px"></div>
<script type="text/javascript">
var radius = 8;
var map = L.map('map').setView([53.69, -1.14], 14);
mapLink = '<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© ' + mapLink + ' Contributors',
maxZoom: 18,
}).addTo(map);
/* Initialize the SVG layer */
L.svg().addTo(map);
/* We simply pick up the SVG from the map object */
var svg = d3.select("#map").select("svg")
, g = svg.append("g");
var data = [{
"node": "interesting",
"x": 641,
"y": 295
}]
var feature = g.selectAll("circle").data(data).enter().append("svg:circle").style("fill", "steelblue").attr("r", 20).attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
}).on("mouseover", function(d) {
console.log("ook" + d.node);
});
</script>
</body>
</html>
在调试中,鼠标事件似乎在传单代码中被捕获,而不是传递给 d3。非常感谢您的帮助或建议
【问题讨论】:
-
当数据发生变化时,D3 是一个很棒的包。您是否使用 d3 将 svg svg 元素添加到 Leaflet?您希望使用的数据的格式/大小是什么?
-
计划(例如)是使用 d3 转换在多组点的传单地图上提供工具提示。显然这里的数据集很小,但使用 d3 和不带工具提示的传单的工作系统有超过 50 万个点存储在 json 和 csv 中
标签: javascript d3.js leaflet mouseover