Skip to content

Instantly share code, notes, and snippets.

@lee00678
Created November 17, 2015 21:14
Show Gist options
  • Save lee00678/9666580dce617b2b46e3 to your computer and use it in GitHub Desktop.
Save lee00678/9666580dce617b2b46e3 to your computer and use it in GitHub Desktop.
US State Geo Map
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>US State projection</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
}
h1 {
font-family: sans-serif;
font-size: 20px;
padding-left: 30px;
padding-top: 0px;
}
h2 {
font-family: sans-serif;
font-size: 12px;
padding-left: 30px;
font-weight: normal;
padding-top: 10px;
}
svg {
background-color: white;
}
#container {
width: 800px;
margin-left: auto;
margin-right: auto;
margin-top: 60px;
padding: 50px;
background-color: white;
border: 1px solid #ccc;
/*box-shadow: 3px 3px 5px 6px #ccc;*/
}
</style>
</head>
<body>
<div id="container">
<h1>Unite States Geo Map</h1>
</div>
<script type="text/javascript">
//Width and height
var w = 500;
var h = 300;
//Define map projection
var projection = d3.geo.albersUsa()
.translate([ w/2, h/2 ])
.scale([ 600 ]);
//Define path generator
var path = d3.geo.path()
.projection(projection);
//Create SVG
var svg = d3.select("#container")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load in GeoJSON data
d3.json("usstate.json", function(json) {
//Bind data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style({fill: "steelblue", stroke: "#eee"});
});
</script>
</body>
</html>
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