Javascript ion担心逻辑部分不适用于第二次选择,因为如前所述,我希望实现这一点作为我的最终结果,但是的,你的线索更有用,所以+1 var $container = $('#map'); var width = $container.innerWidth(); var

Javascript ion担心逻辑部分不适用于第二次选择,因为如前所述,我希望实现这一点作为我的最终结果,但是的,你的线索更有用,所以+1 var $container = $('#map'); var width = $container.innerWidth(); var,javascript,d3.js,Javascript,D3.js,ion担心逻辑部分不适用于第二次选择,因为如前所述,我希望实现这一点作为我的最终结果,但是的,你的线索更有用,所以+1 var $container = $('#map'); var width = $container.innerWidth(); var height = $container.innerHeight(); var projection = d3.geo.equirectangular() .scale(100)

ion担心逻辑部分不适用于第二次选择,因为如前所述,我希望实现这一点作为我的最终结果,但是的,你的线索更有用,所以+1
var $container = $('#map');
var width = $container.innerWidth();
var height = $container.innerHeight();
var projection = d3.geo.equirectangular()
                     .scale(100)
                     .translate([width / 2, height / 2]);

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

var map = d3.select('#map')
          .attr('width', width)
          .attr('height', height)
          .classed('worldmap', true);

var originalScale=projection.scale();

var countries = {};
d3.json("Scripts/worldMapCountriesTopo.js", function (error, world) {
    countries = topojson.feature(world, world.objects.countries).features;
    var bounds = d3.geo.bounds(topojson.feature(world, world.objects.countries));
    var g = map.append('g');
    g.selectAll('path')
        .data(countries)
        .enter()
            .append('path')
            .attr('class', function (d) {
                return 'country' + ' ' + d.properties.name.split(' ').join('-');
            })
            .attr('d', defaultPath)
            .attr('stroke-width', '0.2em');

    var scale = projection.scale();
    var hscale = scale * width / (bounds[1][0] - bounds[0][0]);
    var vscale = scale * height / (bounds[1][1] - bounds[0][1]);
    scale = (hscale < vscale) ? hscale : vscale;
    var offset = [width - (bounds[0][0] + bounds[1][0]) / 2,
                    height - (bounds[0][1] + bounds[1][1]) / 2];

    var newProjection = projection.scale(scale).translate(offset);

    defaultPath.projection(newProjection);
    originalScale= projection.scale();
});
var boundingBoxes = [];
for (var i = 0; i < countries.length; i++) {
    for (var j = 0; j < selectedCountries.length; j++) {
        if (d3.select(selectedCountries[j].name.toUpperCase() == countries[i].properties.name.toUpperCase()) {
            boundingBoxes.push(defaultPath.bounds(countries[i]));
        }
    }
}
var left = [], bottom = [], right = [], top = [];
for (var i = 0; i < boundingBoxes.length; i++) {
    left.push(boundingBoxes[i][0][0]);
    bottom.push(boundingBoxes[i][0][1]);
    right.push(boundingBoxes[i][1][0]);
    top.push(boundingBoxes[i][1][1]);
}
var maxBottom=d3.max(bottom, function (d) { return d; });
var maxRight=d3.max(right, function (d) { return d; })
var minLeft=d3.min(left, function (d) { return d; })
var minTop = d3.min(top, function (d) { return d; })

var widthToZoom = maxRight - minLeft;
var heightToZoom = maxBottom - minTop;

var zoomHScale = (originalScale * widthToZoom) / width;
var zoomVScale = (originalScale * heightToZoom) / height;
var zoomScale =(zoomHScale < zoomVScale) ? zoomVScale : zoomHScale; 
var maxBottom=d3.max(bottom, function (d) { return d; });
var minTop = d3.min(top, function (d) { return d; })
...
var heightToZoom = maxBottom - minTop;
var maxTop=d3.max(top, function (d) { return d; });
var minBottom=d3.min(bottom, function (d) { return d; })
...
var heightToZoom = maxTop - minBottom;
   var centroids = [];

    for(var i = 0; i < list.length; i++) {
      centroids.push(this.path.centroid(list[i]));
    }

    if(centroids.length > 1) {
      var bounds = {
        topLeft : d3.min(centroids, function(d) { return d[0];}),
        topRight : d3.max(centroids, function(d) { return d[0];}),
        bottomLeft : d3.min(centroids, function(d) { return d[1];}),
        bottomRight : d3.max(centroids, function(d) { return d[1];}),
      };

      console.log('centroids ',centroids);


      var dx = bounds.topRight - bounds.topLeft,
          dy = bounds.bottomRight - bounds.bottomLeft,
          x = (bounds.topRight + bounds.topLeft) / 2,
          y = (bounds.bottomLeft + bounds.bottomRight ) / 2;

      console.log('calculated bounds ',dx,dy,x,y);

      scale = Math.floor(.9 / Math.max(dx / this.properties.width, dy / this.properties.height)),
      scale = scale < 1 ? 1 : scale;
      scale = scale > 10 ? 10 : scale;
      translate = [this.properties.width / 2 - scale * x, this.properties.height / 2 - scale * y];


    } else {

      var bounds = map.path.bounds(this._.selectedList[0]);
      var dx = bounds[1][0] - bounds[0][0],
          dy = bounds[1][1] - bounds[0][1];

      var max = Math.max((dx  / this.properties.width), (dy / this.properties.height));

      scale = Math.floor(1 / max);
      scale = scale < 1 ? 1 : scale;
      scale = scale > 10 ? 10 : scale;
      translate = [this.properties.width / 2 - scale * centroids[0][0],  this.properties.height / 2 - scale * centroids[0][1]];
    }