score:2

Accepted answer

This is the code I ended on. Removing the scale and the translate hack has the map rendering properly.

enter image description here

function drawQuintiles() {
  var width = 1600;

  d3.json("/data/us-counties.json", function(error, data) {

    var projection = d3.geo.albersUsa();

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

    var tracts = topojson.feature(data, data.objects.counties);

    projection.scale(1).translate([0, 0]);

    var b = path.bounds(tracts);

    var whRatio = ((b[1][0] - b[0][0]) / (b[1][1] - b[0][1]));

    var height = (width / 2) * whRatio;


    var s = .98 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
      t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];

    projection.scale(s).translate(t);


    var canvas = d3.select("#quintiles")
            .append("canvas")
              .attr("class",'canvasarea');
    var context = canvas.node().getContext("2d");


var ratio = window.devicePixelRatio || 1;

d3.select('.canvasarea')
              .attr("width", width ).attr("height", height )
              .style("width", ((width * ratio) ) + "px").style("height", ((height * ratio) ) + "px");


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


    if (error) throw error;

    context.strokeStyle = '#333';

    context.beginPath();

    var strokeWidth = 0.5;

    context.lineWidth = strokeWidth;
    context.lineCap = "round";

    path(topojson.feature(data, data.objects.counties));
    context.stroke();
  });
}

drawQuintiles();

Related Query

More Query from same tag