miguelfg
miguelfg

Reputation: 1524

d3.js map shows empty

this is driving me nuts, i don't know where is my mistake, in the json file (doesn't look like), code loading the map, or even the colors applied. i already compare with the noob d3.js world example and that works to me, i just replace the coordinates, the json map file, and the tag for my areas to be shown, and voilá, doesn't work, hehe.

Here is my html + js:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
  stroke: green;
  stroke-width: 0.25px;
  fill: grey;
}
</style>
<body>
<!--<script src="http://d3js.org/d3.v3.min.js"></script>-->
<!--<script src="http://d3js.org/topojson.v0.min.js"></script>-->
<script type="text/javascript" src="js/d3.v3.js"></script>
<script src="js/topojson.v0.min.js"></script>
<script>
var width = 960,
    height = 500;

var projection = d3.geo.mercator()
        .center([-3.5,40.5])
        .scale(40000)
//    .scale(900)
//    .rotate([-180,0]);

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

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

var g = svg.append("g");


//d3.json("maps/world-110m2.json", function(error, topology) {
//d3.json("maps/prov_37.json", function(error, topology) {
//d3.json("maps/prov_40.json", function(error, topology) {
d3.json("maps/prov_28.json", function(error, topology) {
    g.selectAll("path")
//      .data(topojson.object(topology, topology.objects.countries)
//      .data(topojson.object(topology, topology.objects.prov_40)
//      .data(topojson.object(topology, topology.objects.prov_37)
      .data(topojson.object(topology, topology.objects.prov_28)
          .geometries)
    .enter()
      .append("path")
      .attr("d", path)
});

var zoom = d3.behavior.zoom()
    .on("zoom",function() {
        g.attr("transform","translate("+
            d3.event.translate.join(",")+")scale("+d3.event.scale+")");
        g.selectAll("path")
            .attr("d", path.projection(projection));
});

svg.call(zoom)
</script>
</body>
</html>

well, my json files that i tried in the code you can download them from here (and also the html file) : test files

Notice that each of my json maps has differents IDs or tags for the areas to show, so every time i switch json map in the HTML code I switch for the right ID/tag. For example the ID for the map "prov_28_topo.json" is "prov_28":

},
"objects": {
    "prov_28": {
        "type": "GeometryCollection",
        "geometries": [{

Upvotes: 0

Views: 1340

Answers (1)

Andy Thornton
Andy Thornton

Reputation: 638

I only looked at one of your json files, prov_28_topo.json, and it wasn't valid JSON.

You had {"type":"Topology","transform":{"scale":[0.0007357596319631963,0.0004301291039103907],"translate":[-4.57907617,38.02244023]},"objects":{"prov_28:{"type"

It should be:

{"type":"Topology","transform":{"scale":[0.0007357596319631963,0.0004301291039103907],"translate":[-4.57907617,38.02244023]},"objects":{"prov_28":{"type"

Its the quotation after _28. Changing that made everything work.

Upvotes: 1

Related Questions