Skip to content

Instantly share code, notes, and snippets.

@Andrew-Reid
Last active May 26, 2017 19:32
Show Gist options
  • Save Andrew-Reid/759e0101223c1f029c26b56ca7ad7759 to your computer and use it in GitHub Desktop.
Save Andrew-Reid/759e0101223c1f029c26b56ca7ad7759 to your computer and use it in GitHub Desktop.
Geo.transform d3v3 example
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.6.0/d3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/2.2.0/topojson.js"></script>
</head>
<body>
<script>
var width = 900;
var height = 450;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
function scale (scaleFactor,width,height,offsetX,offsetY) {
return d3.geoTransform({
point: function(x, y) {
this.stream.point( (x - width/2) * scaleFactor + width/2 - offsetX , - (y - height/2) * scaleFactor + height/2 + offsetY);
}
});
}
d3.json("sa-all.json", function (error, sa){
var path = d3.geoPath().projection(scale(0.04,width,height,150,225))
svg.append("g")
.selectAll("path")
.data(sa.features)
.enter().append("path")
.attr("fill", "gray")
.attr("d", path);
});
</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