简体   繁体   English

使用D3和角度6绘制美国地图区域

[英]Draw USA map region wise using D3 and angular 6

I need to draw USA Map region wise in D3. 我需要在D3中明智地绘制美国地图区域。 It should be something similar to this. 它应该与此类似。 美国地图区明智

I am taking reference from this USA map which is showing data state wise. 我正在参考这张显示数据状态的美国地图。 And I am able to create this state wise chart using D3 and angular 6. But 我能够使用D3和角度6创建这个状态明智的图表。但是

  • How to get the geojson or topojson for Region wise data. 如何获取geojson或topojson用于区域智能数据。

  • I did not find any chart using D3 for this region wise also. 我也没有找到任何使用D3的图表。

  • Which one will be helpful geoJson or topojson. 哪一个将有用geoJson或topojson。 (As per my understanding and this article it will be topojson.) (根据我的理解和本文 ,它将是topojson。)

  • I am not getting any approach to draw to this, 我没有得到任何方法来吸引这个,

EDIT: I am able to create the region chart using the approach which andrew mentioned. 编辑:我能够使用安德鲁提到的方法创建区域图表。 I want to create states outlines too , but not getting any idea about that. 我也想创建状态轮廓 ,但没有想到这一点。 Below is the approach which i used. 以下是我使用的方法。

const svg = d3
  .select('#statesvg')
  .attr('viewBox', '0 0 960 600')
  .attr('width', this.width)
  .attr('height', this.height);
this.getUSData().subscribe(us => {
  svg
    .selectAll(undefined)
    .data(this.uRegionPaths)
    .enter()
    .append('path')
    .attr('d', datum => {
      const feature = topojson.merge(
        us,
        us.objects.states.geometries.filter(state => {
          return datum.contains.indexOf(this.lookup[state.id]) > -1;
        })
      );

      return path(feature);
    })
    .attr('fill', d => {
      return this.sampleRegionData[d.name].color;
    })
    .attr('stroke', 'white')
    .attr('stroke-width', 3);
});

And it looks somewhat similar to this. 它看起来有点类似于此。 few states are missing as of now.(that is not an issue,) 到目前为止,很少有州失踪。(这不是问题)

区域明智的图表,截至目前很少有州缺失

Topojson is converted to geojson when passed to d3.geoPath - you can use either. 当传递给d3.geoPath时,Topojson被转换为geojson - 您可以使用其中任何一个。 However, as the topojson API exposes a merge method, topojson has a key advantage: we can use the topojson us.json file in your example to draw your map. 但是,由于topojson API公开了一个合并方法,topojson有一个关键优势:我们可以使用示例中的topojson us.json文件来绘制地图。

To draw a collection of states as one feature, we just need to merge them into one feature. 要将状态集合绘制为一个特征,我们只需将它们合并为一个特征。 To do so we use: 为此,我们使用:

topojson.merge(us, us.objects.states.geometries.filter( /* some test */ )

Where we test each state to see if it belongs in the region at hand. 我们在哪里测试每个州,看它是否属于手头的区域。 This line will return a geojson feature containing the merged topojson features. 该行将返回包含合并的topojson特征的geojson特征。 We can then pass that to d3.geoPath . 然后我们可以将它传递给d3.geoPath

To work with the us.json file linked to in your example, we need to consider how (and if) the states are identified in the data. 要使用链接到示例中的us.json文件,我们需要考虑如何(以及如果)在数据中标识状态。

The us.json file doesn't come with an accompanying description of its meta data, but I can say that the id of each state is a numerical FIPS code. us.json文件没有随附的元数据描述,但我可以说每个状态的id是一个数字FIPS代码。 So, unless we want to specify the regions numerically, we need to translate between number and abbreviation (or some other recognizable form). 因此,除非我们想要在数字上指定区域,否则我们需要在数字和缩写(或其他一些可识别的形式)之间进行转换。 I'll just use a simple object to get abbreviation from FIPS number: 我只是使用一个简单的对象来获取FIPS编号的缩写:

var lookup = {
    "53" : "WA",
    "41" : "OR",
    "6" : "CA",
    // ...
}

And now we can specify regions, say like: 现在我们可以指定区域,例如:

var regions = [
  {"name": "northwest", "contains":[ "WA","OR","CA" ] }
  // ...
];

Now we can draw our regions: 现在我们可以绘制我们的区域:

var lookup = {
    "53" : "WA",
    "41" : "OR",
    "6" : "CA",
    // ...
}

var regions = [
  {"name": "northwest", "contains":[ "WA","OR","CA" ] }
  // ...
];

d3.json("us.json").then(function(us) {
  svg.selectAll(null)
    .data(regions)
    .enter()
    .append("path")
    .attr("d", function(region) {
      var feature = topojson.merge(us, us.objects.states.geometries.filter(function(state) { 
         return region.contains.indexOf(lookup[state.id]) > -1; 
       }))
       return path(feature);
    })
});

If you want state outlines too - just draw the states in addition to the regions. 如果你也想要状态轮廓 - 除了区域之外只绘制状态。 Here's the west coast using the above method, to draw additional regions, just add them to the regions array (while also adding necessary state codes to the lookup object). 这里是使用上述方法的西海岸 ,绘制其他区域,只需将它们添加到区域数组(同时还向查找对象添加必要的状态代码)。

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

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