Skip to content

Instantly share code, notes, and snippets.

@jmuyskens
Last active December 18, 2015 00:28
Show Gist options
  • Save jmuyskens/5696641 to your computer and use it in GitHub Desktop.
Save jmuyskens/5696641 to your computer and use it in GitHub Desktop.
randomly colored united states
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script src="random-color-states.js"></script>
</body>
</html>
var width = 1160,
height = 960;
var projection = d3.geo.albersUsa()
var path = d3.geo.path()
.projection(projection)
.pointRadius(2);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
d3.json("usa.json", function(error, us) {
svg.selectAll(".state")
.data(topojson.object(us, us.objects.states).geometries)
.enter().append("path")
.attr("class", function(d) { return "state " + d.id; })
.attr("d", path)
.style("fill", function() {
return "hsl(" + Math.random() * 360 + ",100%,50%)";
});
});
Display the source blob
Display the rendered blob
Raw
Loading
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