Skip to content

Instantly share code, notes, and snippets.

@gka
Last active April 20, 2018 10:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gka/f2aabf4a516e16a5190f25fd1923406f to your computer and use it in GitHub Desktop.
Save gka/f2aabf4a516e16a5190f25fd1923406f to your computer and use it in GitHub Desktop.
d3.geo.statePlane
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
fill: #fafafa;
stroke: lightgray;
transition: fill 0.5s ease-in-out;
}
path.selected {
fill: #D4CECE;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min.js"></script>
<script src="//cdn.rawgit.com/gka/d3-geo-state-plane/master/state-plane.js"></script>
<script>
var width = 960,
height = 500,
margin = 50;
var svg = d3.select("body").append("svg")
.attr("width", width).attr("height", height)
.append('g').attr('transform', 'translate('+[margin,margin]+')');
width -= margin * 2; height -= margin * 2;
d3.json("us-states.json", function(error, us) {
if (error) throw error;
var proj = d3.geo.albersUsa()
.translate([width*0.5,height*0.5])
.scale(width*1.2);
var path = d3.geo.path().projection(proj);
var features = topojson.feature(us, us.objects.states).features
.filter(function(d) {
return d.properties.postal;
});
var state = svg.selectAll('path')
.data(features)
.enter()
.append('path')
.attr('d', path);
var shuffle = d3.shuffle(d3.range(features.length)),
cur_i = 0;
setInterval(function() {
var randomState = features[cur_i++ % features.length];
proj = d3.geo.statePlane(randomState.properties.postal, width, height);
path.projection(proj);
state.classed('selected', function(d) {
return d == randomState;
})
.transition()
.duration(750)
.attr('d', path);
}, 2000);
});
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment