问题
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