0

我在 d3 中为美国制作了一张地图。我正在为每个州塑造美国的形象。当我在任何状态下移动鼠标时,它会告诉我该形状的名称。现在我希望如果我点击任何状态,我应该被移动到其他页面。我该怎么做?这可能吗,实际上我想在单击该州时显示每个州的地图。我应该如何移动到其他页面?

显示美国的地图代码是

  <script type="text/javascript">
      var w = 1560;
      var h = 900;
      var proj = d3.geo.mercator();
      var path = d3.geo.path().projection(proj);
      var t = proj.translate(); // the projection's default translation
      var s = proj.scale() // the projection's default scale

      var map = d3.select("#vis").append("svg:svg")
        .attr("width", w)
        .attr("height", h)
        .call(d3.behavior.zoom().on("zoom", redraw));

      var axes = map.append("svg:g").attr("id", "axes");

      var xAxis = axes.append("svg:line")
        .attr("x1", t[0])
        .attr("y1", 0)
        .attr("x2", t[0])
        .attr("y2", h);

      var yAxis = axes.append("svg:line")
        .attr("x1", 0)
        .attr("y1", t[1])
        .attr("x2", w)
        .attr("y2", t[1]);

      var uk = map.append("svg:g").attr("id", "uk");
 d3.json("tryusa.json", function (json) {
          uk.selectAll("path")
          .data(json.features)
        .enter().append("svg:path")
         .attr("d", path)
        .append("svg:title")
        .text(function (d) { return d.properties.name; })

      });
      svg.selectAll(".subunit")
    .data(topojson.object(uk, uk.objects.subunits).geometries)
  .enter().append("path")
    .attr("class", function (d) { return "subunit " + d.id; })
    .attr("d", path);

      function redraw() {
          var tx = t[0] * d3.event.scale + d3.event.translate[0];
          var ty = t[1] * d3.event.scale + d3.event.translate[1];
          proj.translate([tx, ty]);

          // now we determine the projection's new scale, but there's a problem:
          // the map doesn't 'zoom onto the mouse point'
          proj.scale(s * d3.event.scale);

          // redraw the map
          uk.selectAll("path").attr("d", path);

          // redraw the x axis
          xAxis.attr("x1", tx).attr("x2", tx);

          // redraw the y axis
          yAxis.attr("y1", ty).attr("y2", ty);
      }
  </script>

我有一个名为 tryusa.json 的 json 文件。我正在通过这个 json 文件显示地图。我想移动到其他页面?我应该如何进行?是否可以 ?还有其他方法吗?我使用的平台是 C#。请帮帮我。我对d3真的很陌生。

4

1 回答 1

1

考虑到您在评论中提供的代码:

d3.json("tryusa.json", function (json) { 
    uk.selectAll("path")
    .data(json.features) 
    .enter().
    .append("svg:path")
    .attr("d", path)
    .attr("Response.Redirect", "Default.aspx")
    .append("svg:title")
    .text(function (d) { return d.properties.name; })
    .on("click", function () { window.open("Default3.aspx") }) 
});

因此,在这里您将on侦听器添加到svg:title元素。因此,您必须单击工具提示或类似的东西。所以,我认为你应该把on监听器放在路径上:

d3.json("tryusa.json", function (json) { 
    uk.selectAll("path")
    .data(json.features) 
    .enter().
    .append("svg:path")
    .attr("d", path)
    .attr("Response.Redirect", "Default.aspx")
    .on("click", function () { window.open("Default3.aspx") })
    .append("svg:title")
    .text(function (d) { return d.properties.name; }) 
});

更准确地说,后面的一切append都会被封装在这个标签内。

于 2013-03-20T02:07:18.017 回答