Skip to content

Instantly share code, notes, and snippets.

@phil-pedruco
Last active December 24, 2015 11:38
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 phil-pedruco/6791875 to your computer and use it in GitHub Desktop.
Save phil-pedruco/6791875 to your computer and use it in GitHub Desktop.
Map of german districts
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>
<html>
<meta charset="utf-8">
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script type="text/javascript">
var width = 960;
var height = 500;
var canvas = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
d3.json("germany.json", function(data) {
//jeder gemeinde ein path
var group = canvas.selectAll("g")
.data(data.features) //array in geojson heißt "features"
.enter()
.append("g")
// now: data is bound
//earth not flat --> map on flat surface --> projection notwendig
//standard: mercator
var projection = d3.geo.mercator()
.scale(50000)
.center([13.439235,48.830666]) // centers map at given coordinates
.translate([width / 2, height / 2]); // translate map to svg
//path generator
var path = d3.geo.path().projection(projection); //hand projection to projection generator --> how to translate coordinate to pixels
//append to path to each "g" element
var areas = group.append("path")
.attr("d", path) //data comes from path generator
.attr("class", "area") //CSS
.attr("fill", "steelblue");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment