D3 albersUsa投影功能返回null [英] D3 albersUsa projection funtion return null
问题描述
我是D3的新手,我正在尝试在地图上设置点.
I'm pretty new to D3 and I'm trying to set point on a map.
使用此代码创建投影时,我感到很困惑:
I'm confused as I created a projection with this code:
var projection = d3.geo
.albersUsa()
.scale(500)
.translate([el.clientWidth / 2, el.clientHeight / 2]);
我使用此投影来绘制地图,并且效果很好.
I use this projection to draw a map and it works fine.
但是无论何时我调用projection([10, 20])
,它都会返回null
,无论我传入的值是什么.
But then whenever I call projection([10, 20])
it returns null
whichever values I'm passing in.
我的错误是什么?
推荐答案
来自文档
[…] 如果指定的位置没有定义的投影位置,例如当该位置在投影的剪切范围之外时,则可能返回null.
[…] May return null if the specified location has no defined projected position, such as when the location is outside the clipping bounds of the projection.
Albers USA投影仅在其边界内定义,对于定义明确的区域之外的坐标将产生null
.
The Albers USA projection is defined only within its borders and will yield null
for coordinates outside the well-defined area.
请参见使用无效的[10,20]
和有效点[-110,40]
调用projection(location)
的以下比较:
See this comparison of calls to projection(location)
using [10,20]
, which is not valid, and [-110,40]
, which is a valid point:
var projection = d3.geo
.albersUsa()
.scale(500)
.translate([960, 500]);
d3.selectAll("p")
.data([[10,20],[-110,40]])
.enter().append("p")
.text(function(d) { return d + ": " + projection(d); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
这篇关于D3 albersUsa投影功能返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!