D3 Map not rendering Australian topojson file

你离开我真会死。 提交于 2019-12-11 13:37:21

问题


I have a JSON file that d3 map is not rendering an Australia TopoJSON file I have created.

The same code renders an American map just fine. There are no errors in the browser inspector and both maps render fine on online visualisation sites like geojson.io.

I have provided links to the JSON's.

  • Australian TopoJSON Does not work with my code (but does work on geojson.io/#map=4/-27.97/125.22 )
  • American TopoJSON Does work with my code

<html>

<head>
  <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
  <script src="http://d3js.org/topojson.v1.min.js"></script>
  <style>
    path {
      fill: #ccc;
    }
  </style>
</head>

<body>
  <h1>topojson simplified Australia</h1>
  <script>
    var width = window.innerWidth,
      height = window.innerHeight;

    var path = d3.geo.path();

    var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

    d3.json("topo-australia-simplified.json", function(error, topology) {
      if (error) throw error;

      svg.selectAll("path")
        .data(topojson.feature(topology, topology.objects.australia).features)
        .enter().append("path")
        .attr("d", path);
    });
  </script>


</body>

</html>

回答1:


The problem is that you are using:

var path = d3.geo.path();

You have not given the projection:

var projection = d3.geo.mercator()
  .scale(500)//scale it up as per your choice.
  .translate([-900,0]);//translate as it was scaled up.

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

working code here



来源:https://stackoverflow.com/questions/38325752/d3-map-not-rendering-australian-topojson-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!