Skip to content

Instantly share code, notes, and snippets.

@almccon
Last active December 5, 2017 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almccon/8691ea2e6f7bacb543551cde1652011c to your computer and use it in GitHub Desktop.
Save almccon/8691ea2e6f7bacb543551cde1652011c to your computer and use it in GitHub Desktop.
States with a population less than Queens
license: gpl-3.0
height: 600
border: no
<!DOCTYPE html>
<style>
.counties {
fill: none;
}
.county-highlight {
fill: blue;
}
.states {
fill: lightgray;
}
.states-lowpop {
fill: red;
}
.counties :hover {
fill: red;
}
.county-borders {
fill: none;
stroke: #fff;
stroke-width: 0.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var svg = d3.select("svg");
var path = d3.geoPath();
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
svg.append("g")
.attr("class", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.classed("states-lowpop", function(d) {
return d.id == '02' // Alaska
|| d.id == '10' // Delaware
|| d.id == '15' // Hawaii
|| d.id == '16' // Idaho
|| d.id == '23' // Maine
|| d.id == '30' // Montana
|| d.id == '31' // Nebraska
|| d.id == '33' // New Hampshire
|| d.id == '35' // New Mexico
|| d.id == '38' // North Dakota
|| d.id == '44' // Rhode Island
|| d.id == '46' // South Dakota
|| d.id == '50' // Vermont
|| d.id == '54' // West Virginia
|| d.id == '56' // Wyoming
;
});
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("d", path)
.classed('county-highlight', function(d) {
return d.id == '36081';
});
svg.append("path")
.attr("class", "county-borders")
.attr("d", path(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })));
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment