我使用geoNaturalEarth1()显示了here在d3中创建了一张世界地图。我使用这个投影的世界geojson地图来获取地图,如下面的代码所示。然而,这表明在没有边界的情况下漂浮在太空中的国家。我想在地图投影周围画一个边框,使它看起来更像地图。如投影图像所示,边界将是平坦的顶部/底部,弯曲的边。这是可能的,我怎么能去做呢?
var projection = d3.geoNaturalEarth1()
.translate([w/2, h/2])
.scale(247)
.center([0,0]);
var path = d3.geoPath().projection(projection);
d3.json('map.geojson').then(function(world) {
svg.selectAll(".emissions_path")
.data(world.features)
.enter().append("path")
.attr('fill', '#fff')
.attr("d", path)
.style('stroke', 'black')
.style('stroke-width', '0.5px');
您可以向路径生成器提供类型为Sphere
的geojson:
还支持Sphere类型,这对于渲染地球轮廓很有用;球体没有坐标。 (docs)
这看起来像:
var outline = {type:"Sphere"}
它可以直接传递给路径生成器:
var context = d3.select("canvas").node().getContext("2d"),
projection = d3.geoNaturalEarth1()
.scale(70)
.translate([200,100])
path = d3.geoPath()
.context(context)
.projection(projection);
d3.json("https://unpkg.com/world-atlas@1/world/110m.json", function(error, world) {
if (error) throw error;
context.beginPath();
context.fillStyle = "lightgreen";
path(topojson.feature(world, world.objects.land));
context.fill();
context.beginPath();
context.strokeStyle = "#ccc";
path({type: "Sphere"})
context.stroke();
});
<canvas width="500" height="300"></canvas>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
另外,还有d3.geoGraticule,它允许定期绘制经络和平行线:
var context = d3.select("canvas").node().getContext("2d"),
projection = d3.geoNaturalEarth1()
.scale(70)
.translate([200,100])
path = d3.geoPath()
.context(context)
.projection(projection);
d3.json("https://unpkg.com/world-atlas@1/world/110m.json", function(error, world) {
if (error) throw error;
context.beginPath();
context.fillStyle = "lightgreen";
path(topojson.feature(world, world.objects.land));
context.fill();
context.beginPath();
context.strokeStyle = "#eee";
path(d3.geoGraticule10())
context.stroke();
context.beginPath();
context.strokeStyle = "#000";
path({type:"Sphere"})
context.stroke();
});
<canvas width="500" height="300"></canvas>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>