2

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.

<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>
Dawid
  • 165
  • 4

1 Answers1

0

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

Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55
  • Thank you! Do you know why I can render the USA map though without the projection? – Dawid Jul 12 '16 at 10:46
  • The reason is because when you do `d3.geo.path();` the projection is null. (For the case of USA)The feature values are such that you don't need to zoom, so no need to translate it. I have also edited my answer hope it explains better. – Cyril Cherian Jul 12 '16 at 11:03