d3.js可缩放的地图在开始时太小

时间:2015-10-07 08:59:37

标签: javascript d3.js

我试图修改由mbostock提供的适合欧洲的zoomable map美国的例子。

在我能够得到它某种的时候,它在开始时一直很小!

我已经看过所有尝试在开始时(即缩放之前)更改演示文稿尺寸的内容都要更大,但我还是无法弄明白。怎么做到这一点?

所有代码均为here

但实际上,它只是以下短文件和eu.json文件:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
  fill: none;
  pointer-events: all;
}
#states {
  fill: #aaa;
}
#states .active {
  fill: orange;
}
#state-borders {
  fill: none;
  stroke: #fff;
  stroke-width: 0.5px;
  stroke-linejoin: round;
  stroke-linecap: round;
  pointer-events: none;
}
</style>
<body>
<div id="map"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<script>
var width = 960;
var height = 500;
var centered;
var projection = d3.geo.mercator();
var path = d3.geo.path()
    .projection(projection);
var svg = d3.select("#map")
    .append("svg")
    .attr("width", width)
    .attr("height", height);
svg.append("rect")
    .attr("class", "background")
    .attr("width", width)
    .attr("height", height)
    .on("click", clicked);
var g = svg.append("g");
d3.json("eu.json", function(error, data) {
  if (error) throw error;
  g.append("g")
      .attr("id", "states")
    .selectAll("path")
      .data(topojson.feature(data, data.objects.europe).features)
    .enter().append("path")
      .attr("d", path)
      .on("click", clicked);
  g.append("path")
      .datum(topojson.mesh(data, data.objects.europe, function(a, b) { return a !== b; }))
      .attr("id", "state-borders")
      .attr("d", path);
});
function clicked(d) {
  var x, y, k;
  if (d && centered !== d) {
    var centroid = path.centroid(d);
    x = centroid[0];
    y = centroid[1];
    k = 4;
    centered = d;
  } else {
    x = width / 2;
    y = height / 2;
    k = 1;
    centered = null;
  }
  g.selectAll("path")
      .classed("active", centered && function(d) { return d === centered; });
  g.transition()
      .duration(750)
      .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
      .style("stroke-width", 1.5 / k + "px");
}
</script>  

0 个答案:

没有答案