我最近使用D3制作了一些地图,并且在路径无法正确关闭方面遇到了一些问题,如下面的第一个示例所示.我认为这是围绕-90度纬度精度的某种数据问题,但是我不确定如何验证这种怀疑.有趣的是,当我在页面上包含d3-geo模块的最新版本时,地图将按预期方式渲染(请参见下面的第二个示例).这里发生了什么?
var svg = d3.select("#map").style("border","0px solid #000000");
var width = 600;
var height = 300;
var proj = d3.geoEquirectangular().scale(95).translate([300,150]);
var path = d3.geoPath(proj);
//countries from natural earth dataset
var url = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_0_countries.geojson";
d3.json(url, function(err, json){
//draw paths once
svg.selectAll("path").data(json.features).enter().append("path")
.attr("d", path).attr("stroke","#00aaff");
});
<script src="https://d3js.org/d3.v4.min.js"></script>
<h2>Fig. 1 Multiple paths not closed properly</h2>
<svg width="600px" height="300px" id="map"></svg>
<p>Uses D3 Version 4.13.0</p>
<p>GeoJson data source: <a href="https://github.com/nvkelso/natural-earth-vector/tree/master/geojson">https://github.com/nvkelso/natural-earth-vector/tree/master/geojson</a></p>
var svg = d3.select("#map").style("border","0px solid #000000");
var width = 600;
var height = 300;
var proj = d3.geoEquirectangular().scale(95).translate([300,150]);
var path = d3.geoPath(proj);
//countries from natural earth dataset
var url = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_0_countries.geojson";
d3.json(url, function(err, json){
//draw paths once
svg.selectAll("path").data(json.features).enter().append("path")
.attr("d", path).attr("stroke","#00aaff");
});
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-geo.v1.min.js"></script>
<h2>Fig. 2 Map renders as expected</h2>
<p>The only difference from above is the inclusion of <a href="https://d3js.org/d3-geo.v1.min.js">https://d3js.org/d3-geo.v1.min.js</a></p>
<svg width="600px" height="300px" id="map"></svg>
<p>Uses D3 Version 4.13.0</p>
<p>GeoJson data source: <a href="https://github.com/nvkelso/natural-earth-vector/tree/master/geojson">https://github.com/nvkelso/natural-earth-vector/tree/master/geojson</a></p>
解决方法:
我相信您所引用的issue on github与d3如何引用多边形有关.该问题可能导致在某些投影上与南极重叠的多边形倒置.
d3-geo.v1.min.js的当前版本位于https://d3js.org/d3-geo.v1.min.js,版本为1.10,已于3月发布.此版本实现了此修复程序. d3v4(4.13)的最新版本于1月29日发布,因此不包含此修复程序,因此发现了该行为.