Center a map in d3 given a geoJSON object

后端 未结 11 2354
慢半拍i
慢半拍i 2020-11-21 22:55

Currently in d3 if you have a geoJSON object that you are going to draw you have to scale it and translate it in order to get it to the size that one wants and translate it

11条回答
  •  误落风尘
    2020-11-21 23:29

    With d3 v4 or v5 its getting way easier!

    var projection = d3.geoMercator().fitSize([width, height], geojson);
    var path = d3.geoPath().projection(projection);
    

    and finally

    g.selectAll('path')
      .data(geojson.features)
      .enter()
      .append('path')
      .attr('d', path)
      .style("fill", "red")
      .style("stroke-width", "1")
      .style("stroke", "black");
    

    Enjoy, Cheers

提交回复
热议问题