Accepted answer
  • Have you ever considered to connect your projection and path generator?

    var path = d3.geoPath(projection);
  • Also set the path in the resize method. Label the 2 path elements'#nation').attr('d', path(topojson.feature(us, us.objects.nation)));'#nation2').attr('d', path(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })));
  • the onload in the body has to go

  • there are no tags with class land or state

    //'.land').attr('d', path);
    //'.state').attr('d', path);
  • why call projection size and translate if you call fitSize

Here is the script code

var width = parseInt('#mapContainer').style('width')) - 50
  ,height = width/960*600;

var svg ="svg")
  .attr("width", width + 50)

var defs ="defs");

d3.json("", function(error, us) {
  if (error) throw error;

  var topoFeatureStates = topojson.feature(us, us.objects.states);
  var projection = d3.geoIdentity()
      .fitSize([width, height], topoFeatureStates);

  var path = d3.geoPath(projection);

      .datum(topojson.feature(us, us.objects.nation))
      .attr("id", "nation")
      .attr("d", path);

      .attr("xlink:href", "#nation")
      .attr("fill-opacity", 0.2)
      .attr("filter", "url(#blur)");

      .attr("xlink:href", "#nation")
      .attr("fill", "#fff");

      .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
      .attr("fill", "none")
      .attr("stroke", "#777")
      .attr("stroke-width", 0.70)
      .attr("d", path);

 // function to resize map 
  function resize() {
      // adjust things when the window size changes
      width = parseInt('#mapContainer').style('width')) - 50;
      height = width/960*600;
      svg.attr('width', width + 50).attr('height', height);
      // update projection
          .fitSize([width, height], topoFeatureStates);

      // resize the map
      svg.selectAll('path').attr('d', path);

  // resize event"resize", resize);


Wondering why the path in the resize did not I work had a second look at the responsive example. It attaches the result of topojson as datum to the path.

Modified the code to do the same.

Related Query

More Query from same tag