A modified version of d3.geo.albersUsa
that includes Puerto Rico.
forked from mbostock's block: AlbersUSA + PR
xxxxxxxxxx
<meta charset="utf-8">
<style>
rect {
fill: none;
pointer-events: all;
}
.sphere {
fill: none;
stroke: #ccc;
shape-rendering: crispEdges;
}
.graticule {
fill: none;
stroke: #777;
stroke-opacity: .2;
}
.mesh {
fill: none;
stroke: #000;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="albers-usa-pr.js"></script>
<script>
var width = 960,
height = 500;
var albersUsaPrProj = albersUsaPr()
.scale(1070)
.translate([width / 2, height / 2]);
var yScale = d3.scale.linear()
.domain([0,height])
.range([0.25,1]);
var desktopProj = d3.geo.transform({
point: function(x,y) {
this.stream.point((x - 0.5*width) * yScale(y) + (0.5*width), y);
}
});
var combinedProj = {
stream: function(s) {
return albersUsaPrProj.stream(desktopProj.stream(s));
}
};
var path = d3.geo.path()
.projection(combinedProj);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var graticule = d3.geo.graticule()
.step([2, 2]);
svg.append("rect")
.attr("width", width)
.attr("height", height);
svg.append("path")
.datum({type: "Sphere"})
.attr("class", "sphere")
.attr("d", path);
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
svg.on("mousemove", function() {
console.log(projection.invert(d3.mouse(this)));
});
d3.json("https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/us.json", function(error, us) {
if (error) throw error;
svg.append("path")
.datum(topojson.mesh(us))
.attr("class", "mesh")
.attr("d", path);
});
</script>
https://d3js.org/d3.v3.min.js
https://d3js.org/topojson.v1.min.js