d3美国州地图带标记,缩放变换问题

8

我已经根据以下示例创建了一个带有美国州的d3地图:

http://bl.ocks.org/mbostock/4699541

并按照以下SO问题添加了标记:

Put markers to a map generated with topoJSON and d3.js

问题在于,在缩放时,地图标记保持不变。我相信我需要将它们转换为新位置,但不确定如何实现。

enter image description here

var width = 900,
  height = 500,
  active = d3.select(null);

var projection = d3.geo.albersUsa()
  .scale(1000)
  .translate([width / 2, height / 2]);

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

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

svg.append("rect")
  .attr("class", "background")
  .attr("width", width)
  .attr("height", height)
  .on("click", reset);

var g = svg.append("g")
  .style("stroke-width", "1.5px");

d3.json("/files/d3-geo/us.json", function(error, us) {
  if (error) { throw error; }

  g.selectAll("path")
    .data(topojson.feature(us, us.objects.states).features)
    .enter().append("path")
    .attr("d", path)
    .attr("class", function(item) {
      return window.US_STATES[item.id].water_authorities > 0 ? 'avail' : 'unavail';
    })
    .on("click", clicked);

  g.append("path")
    .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
    .attr("class", "mesh")
    .attr("d", path);
});

d3.json('/files/coordinates.json', function(error, coords) {
  if (error) { throw error; }

  svg.selectAll(".mark")
    .data(coords)
    .enter()
    .append("image")
    .attr('class','mark')
    .attr('width', 20)
    .attr('height', 20)
    .attr("xlink:href",'assets/gmap_red.png')
    .attr("transform", function(d) {
      return "translate(" + projection([d[1],d[0]]) + ")";
    });
});

function clicked(d) {
  if (active.node() === this) { return reset(); }
  if (window.US_STATES[d.id].water_authorities === 0) { return; }

  active.classed("active", false);
  active = d3.select(this).classed("active", true);

  var bounds = path.bounds(d),
    dx = bounds[1][0] - bounds[0][0],
    dy = bounds[1][1] - bounds[0][1],
    x = (bounds[0][0] + bounds[1][0]) / 2,
    y = (bounds[0][1] + bounds[1][1]) / 2,
    scale = .9 / Math.max(dx / width, dy / height),
    translate = [width / 2 - scale * x, height / 2 - scale * y];

  g.transition()
    .duration(750)
    .style("stroke-width", 1.5 / scale + "px")
    .attr("transform", "translate(" + translate + ")scale(" + scale + ")");
}

function reset() {
  active.classed("active", false);
  active = d3.select(null);

  rebatesTable.clear().draw();

  g.transition()
    .duration(750)
    .style("stroke-width", "1.5px")
    .attr("transform", "");
}

我会将投影更改为缩放,然后使用更改后的投影重新计算标记的位置。 - Lars Kotthoff
1个回答

14

步骤一

将组内所有点添加到SVG之外。 这将确保标记点与主组一起移动。

  g.selectAll(".mark")//adding mark in the group
    .data(marks)
    .enter()
    .append("image")
    .attr('class', 'mark')
    .attr('width', 20)
    .attr('height', 20)
    .attr("xlink:href", 'https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/24x24/DrawingPin1_Blue.png')
    .attr("transform", function(d) {
      return "translate(" + projection([d.long, d.lat]) + ")";
    });

步骤2

取消主组的缩放效果,否则标记会变得过大。

  g.selectAll(".mark")
  .transition()
    .duration(750)
    .attr("transform", function(d) {
      var t = d3.transform(d3.select(this).attr("transform")).translate;//maintain aold marker translate 
      return "translate(" + t[0] +","+ t[1] + ")scale("+1/scale+")";//inverse the scale of parent
    });        

步骤3

在缩小时,标记将缩放回 1。

  g.selectAll(".mark")
    .attr("transform", function(d) {
      var t = d3.transform(d3.select(this).attr("transform")).translate;
      console.log(t)
      return "translate(" + t[0] +","+ t[1] + ")scale("+1+")";
    });   

这里有一个相关的IT技术示例代码:链接

希望这能帮到你!


1
非常出色的答案,谢谢!我对第三步进行了小改进,添加了.transform().duration(750)(在.attr(...)之前),这样在缩小时就像放大一样添加了相同类型的缓动效果。 - Troy
clicked 函数内计算了翻译,您需要相应地更新它,以使标记位于中心。 - Cyril Cherian
你能更具体地说明一下在你的示例代码中110-116行的更改,这将允许针在缩放时保持在状态形状的相同位置吗?谢谢。 - Nathan
在110-116行,我反转了比例尺,只是为了标记。原因:标记位于具有路径的组内。我们正在缩放该组。因此,标记图像自然也会缩放。因此,我将1/比例尺赋予引脚,以使其不会放大。您可以注释这些行以查看效果。 - Cyril Cherian
3
问题发布在stackoverflow上:https://dev59.com/MZvga4cB1Zd3GeqPyz2N,还有一个答案:http://jsbin.com/barekeyaca/edit?html,output。 - Nathan
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接