Skip to content

Instantly share code, notes, and snippets.

@kaz-a
Last active October 5, 2016 16:03
Show Gist options
  • Select an option

  • Save kaz-a/78763824d536dec8bfea to your computer and use it in GitHub Desktop.

Select an option

Save kaz-a/78763824d536dec8bfea to your computer and use it in GitHub Desktop.
US Map
<!DOCTYPE html>
<meta charset="utf-8">
<style type="text/css">
body {
margin: 60px auto;
width: 1000px;
font-family: arial, sans;
}
.subunit.USB {
fill: #ddc;
}
.place-label {
fill: #000;
fill-opacity: .5;
font-size: 8px;
text-anchor: middle;
}
.place-label:hover {
fill: red;
fill-opacity: 1;
font-size: 1em;
font-weight: 500;
}
.place {
fill: #000;
fill-opacity: .7;
}
</style>
<body>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script type="text/javascript">
var width = 1000,
height = 600;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("us.json", function(error, us) {
if (error) return console.error(error);
// console.log(us);
var subunits = topojson.feature(us, us.objects.subunits);
var projection = d3.geo.albers()
.scale(1000)
.center([0, 35])
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection)
.pointRadius(2);
svg.append("path")
.datum(subunits)
.attr("d", path);
svg.selectAll(".subunit")
.data(topojson.feature(us, us.objects.subunits).features)
.enter().append("path")
.attr("class", function(d) { return "subunit " + d.id; })
.attr("d", path);
svg.append("path")
.datum(topojson.feature(us, us.objects.places))
.attr("d", path)
.attr("class", "place");
svg.selectAll(".place-label")
.data(topojson.feature(us, us.objects.places).features)
.enter().append("text")
.attr("class", "place-label")
.attr("transform", function(d) { return "translate(" + projection(d.geometry.coordinates) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.properties.name; });
svg.selectAll(".place-label")
.attr("x", function(d) { return d.geometry.coordinates[0] > -1 ? 6 : -6; })
.style("text-anchor", function(d) { return d.geometry.coordinates[0] > -1 ? "start" : "end"; });
});
</script>
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.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment