Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js - JSON data array is binding the same array element to everything

I am loading a GeoJSON data file that contains an array of objects, each object containing the vector information for a different country's outline. The same array element is being bound to every DOM element. I have come across this scope issue before in JavaScript but every change I made caused nothing to load.

I attached a jsfiddle. I use an example data file which seems to take a couple seconds to load.

My code from the jsfiddle looks like:

$(document).ready(function() {
  d3.json(
    "https://raw.githubusercontent.com/datasets/geo-boundaries-world-110m/master/countries.geojson",
    function(error, data) {
      var myGeoJSON = data.features;

      for (i = 0; i < myGeoJSON.length; i++) {
        var path = d3.geo.path();
        var width = 960;
        var height = 600;
        var svg = d3.select("body").append("svg")
          .attr("width", width)
          .attr("height", height);

        svg.selectAll("path")
          .data(data.features)
          .enter().append("path")
          .attr("d",path)
          .attr("fill","#3e429a");
      }
    }
  );
});
like image 321
Four_lo Avatar asked Oct 02 '15 08:10

Four_lo


1 Answers

You don't need to loop through the array of features. Your data file is a FeatureCollection, which D3 can draw, either as a single path element:

svg.append("path").datum(data).attr("d", d3.geo.path());

or as separate path elements, one for each feature (country):

svg.selectAll("path").data(data.features)
  .enter().append("path").attr("d", d3.geo.path())

By default D3 uses the projection d3.geo.albersUsa() which brings Hawaii to Mexico and Alaska to just outside the tropics. You can switch to the equirectangular projection to see the whole world:

d3.json(
  "https://raw.githubusercontent.com/datasets/geo-boundaries-world-110m/6cf0946a0f4d027d21686e8402f19b524c4f6385/countries.geojson",
  function(error, data) {
    var projection = d3.geo.equirectangular();
    var path = d3.geo.path().projection(projection);
    var width = 960;
    var height = 600;
    var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

    svg.selectAll("path")
      .data(data.features)
      .enter().append("path")
      .attr("d", path)
      .attr("fill", "#3e429a");
  }
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Epilogue: I initially tried to use the more common Mercator projection, only to find that it cannot handle Antarctica. D3 draws the shape for the continent, then fills the rest of the oceans instead.

In the same D3 forum thread, the author of D3 mentioned a bug in the TopoJSON tool used to generate maps, so it might be an issue with the data file you use. If you prefer Mercator, you might need to work with the maintainer of geo-boundaries-world-110m to fix the data file, or just exclude Antarctica from your maps.

Demo of Antarctica in Mercator:

d3.json(
  "https://raw.githubusercontent.com/datasets/geo-boundaries-world-110m/6cf0946a0f4d027d21686e8402f19b524c4f6385/countries.geojson",
  function(error, data) {
    var projection = d3.geo.mercator();
    var path = d3.geo.path().projection(projection);
    var width = 960;
    var height = 600;
    var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

    var antarctica = data.features.splice(6, 1);
    // now "data" has the rest of the world
    svg.selectAll("path")
      .data(antarctica)
      .enter().append("path")
      .attr("d", path)
      .attr("stroke", "red").attr("stroke-width", 10)
      // thick borders so we can see the odd paths
      .attr("fill", "#3e429a");
  }
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
like image 135
approxiblue Avatar answered Sep 21 '22 21:09

approxiblue