简体   繁体   English

将地理形状缩放到D3中的相似大小

[英]Scaling geographic shapes to a similar size in D3

I'm using D3's world-countries.json file to create a mercator map of world countries, which I'll then bind to some data for a non-contiguous cartogram. 我正在使用D3的world-countries.json文件创建世界国家的墨卡托地图,然后将其绑定到一些非连续制图的数据。 Alas, the much larger sizes of Canada, the US, Australia, etc. mean that one unit for those countries is the spatial equivalent of several units for, say, Malta. las,加拿大,美国,澳大利亚等国的国土面积大得多,这意味着这些国家的一个单位在空间上相当于马耳他的几个单位。

What I think I need to do is normalize the geojson shapes, such that Canada and Malta are the same size when starting out. 我想我需要做的是对geojson形状进行规范化,以便在开始时,加拿大和马耳他的尺寸相同。

Any idea how I'd do that? 知道我会怎么做吗?

Thanks! 谢谢!

Update: I've tried explicitly setting the width and height of all the paths to a small integer, but that seems to just get overridden by the transform later. 更新:我已经尝试将所有路径的宽度和高度显式设置为一个小整数,但是稍后似乎会被转换覆盖。 Code follows: 代码如下:

// Our projection.
var xy = d3.geo.mercator(),
    path = d3.geo.path().projection(xy);

var states = d3.select("body")
  .append("svg")
  .append("g")
    .attr("id", "states");

function by_number() {

 function compute_by_number(collection, countries) {

    //update
     var shapes = states
            .selectAll("path")
              .data(collection.features, function(d){ return d.properties.name; });
    //enter       
         shapes.enter().append("path")
              .attr("d", path)
              .attr("width", 5) //Trying to set width here; seems to have no effect.
              .attr("height", 5) //Trying to set height here; seems to have no effect.
              .attr("transform", function(d) { //This works.
                var centroid = path.centroid(d),
                x = centroid[0],
                y = centroid[1];
                return "translate(" + x + "," + y + ")"
                + "scale(" + Math.sqrt(countries[d.properties.name] || 0) + ")"
                + "translate(" + -x + "," + -y + ")";
              })
            .append("title")
              .text(function(d) { return d.properties.name; });
    //exit
 }

 d3.text("../data/country_totals.csv", function(csvtext){
    var data = d3.csv.parse(csvtext);
    var countries = [];
    for (var i = 0; i < data.length; i++) {
        var countryName = data[i].country.charAt(0).toUpperCase() + data[i].country.slice(1).toLowerCase();
        countries[countryName] = data[i].total;
    }

    if (typeof window.country_json === "undefined") {
        d3.json("../data/world-countries.json", function(collection) {          
            window.country_json = collection;
            compute_by_number(collection, countries);           


        });
    } else {
            collection = window.country_json;
            compute_by_number(collection, countries);
    }

  });
} //end by_number

by_number();

You might be able to use the helper function I posted here: https://gist.github.com/1756257 您也许可以使用我在此处发布的帮助器功能: https : //gist.github.com/1756257

This scales a projection to fit a given GeoJSON object into a given bounding box. 这将缩放投影以使给定的GeoJSON对象适合给定的边界框。 One advantage of scaling the projection, rather than using a transform to scale the whole path, is that strokes can be consistent across maps. 缩放投影而不是使用变换来缩放整个路径的优点之一是,笔划在地图上可以保持一致。

Another, simpler option might be to: 另一个更简单的选择可能是:

  1. Project the paths; 投影路径;
  2. Use path.getBBox() to get the bounding box for each ( .getBBox() is a native SVG function, not a D3 method) 使用path.getBBox()获取每个对象的边界框( .getBBox()是本机SVG函数,而不是D3方法)
  3. Set a transform on the path, similar to how you do it now, to scale and translate the path to fit your bounding box. 与现在类似,在路径上设置变换以缩放和转换路径以适合您的边界框。

This is a bit simpler, as it doesn't involve projections, but you'll need to scale the stroke by the inverse ( 1/scale ) to keep them consistent (and therefore you won't be able to set stroke values with CSS). 这有点简单,因为它不涉及投影,但是您需要按反比例( 1/scale )缩放笔划以保持一致(因此您将无法使用CSS设置笔划值)。 It also requires actually rendering the path first, then scaling it - this might affect performance for complex geometries. 它还需要首先实际渲染路径,然后进行缩放-这可能会影响复杂几何的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM