Skip to content

Instantly share code, notes, and snippets.

@pbogden
Last active September 10, 2015 16:52
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 pbogden/8c6bcd912f9edebab99d to your computer and use it in GitHub Desktop.
Save pbogden/8c6bcd912f9edebab99d to your computer and use it in GitHub Desktop.
umbc countries

Template for countries.

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.
<!DOCTYPE html>
<meta charset="utf-8">
<title>umbc countries</title>
<style>
body {
position: absolute;
margin: 0px;
}
svg {
background-color: #4682b4;
}
.info {
font-family: sans-serif;
color: #000;
position: absolute;
top: 450px;
left: 50px;
}
path {
fill: #555555;
stroke: #aaaaaa;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<body>
<script>
var width = 960, height = 500;
var data; // declare a global variable
var layer = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on("mousemove", mousemoved);
var info = d3.select("body").append("div")
.attr("class", "info");
var projection = d3.geo.mercator();
var path = d3.geo.path()
.projection(projection);
// If you use this URL, you won't need your own copy of the data.
var url = "https://gist.githubusercontent.com/pbogden/8c6bcd912f9edebab99d/raw/countries.json"
d3.json(url, plotCountries);
function plotCountries(error, json) {
// Create array of GeoJSON features -- this defines the global variable "data"
data = json.objects.ne_50m_admin_0_countries.geometries
.map(function(d) { return topojson.feature(json, d); });
// Put your code here
// Plot the features
layer.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d", path);
}
function mousemoved() {
info.text(formatLocation(projection.invert(d3.mouse(this)), projection.scale()));
}
function formatLocation(p, k) {
var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f");
return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " "
+ (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment