【问题标题】:Mapping an arc in D3 using d3.geo.path()使用 d3.geo.path() 在 D3 中映射弧
【发布时间】:2016-01-25 10:46:12
【问题描述】:

我正在尝试在美国地图上绘制连接两点的弧线。 我用来制作美国地图的代码是

var path = d3.geo.path()
    .projection(projection);

var graticule = d3.geo.graticule()
    .extent([[-98 - 45, 38 - 45], [-98 + 45, 38 + 45]])
    .step([5, 5]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);

queue()
.defer(d3.json,'us.json')
.await(makeMyMap);



function makeMyMap(error, us) {
  if (error) throw error;

  svg.insert("path", ".graticule")
      .datum(topojson.feature(us, us.objects.land))
      .attr("class", "land")
      .attr("d", path);

  svg.insert("path", ".graticule")
      .datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b && !(a.id / 1000 ^ b.id / 1000); }))
      .attr("class", "county-boundary")
      .attr("d", path);

  svg.insert("path", ".graticule")
      .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
      .attr("class", "state-boundary")
      .attr("d", path);

  drawPath()

}

function drawPath() {
var route = svg.insert("path", ".graticule")
                   .datum({type: "LineString", coordinates: [[33,-118], [38.6,-78]]})
                   .attr("class", "route")
                   .attr("d", path);
}

目前drawPath() 函数绘制的路径正在某处制作,但我无法在地图上查看。如果我没有在 CSS 中设置fill: none,那么屏幕将变黑,但将其设置为一种颜色只会使画布被该颜色覆盖。

us.json文件用于制作地图,是一个topojson对象。

【问题讨论】:

  • 你能提供一个活生生的例子吗?

标签: javascript d3.js visualization


【解决方案1】:

你搞砸了 LineString 的位置。根据spec 位置指定为[longitude,latitude]。由于纬度值不能超过 90 度,显然您需要切换坐标值的顺序:

.datum({type: "LineString", coordinates: [[-118,33], [-78,38.6]]})

感谢 Mark 的评论,他花时间做出了努力,这也可以在他的 working demo 中找到。

【讨论】:

  • 打败了我。这是一个有效的sample
  • @Mark 我在答案中包含了指向您的演示的链接。希望,你不介意。
  • 哦,我不敢相信我错过了这样的事情。非常感谢!
猜你喜欢
  • 2013-11-08
  • 2016-02-09
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 2021-08-06
相关资源
最近更新 更多