Skip to content

Instantly share code, notes, and snippets.

@EvertLagerberg
Last active October 26, 2016 20:11
Show Gist options
  • Save EvertLagerberg/24075c7b27d093bdd623bc974d28bff6 to your computer and use it in GitHub Desktop.
Save EvertLagerberg/24075c7b27d093bdd623bc974d28bff6 to your computer and use it in GitHub Desktop.
Brazil
license: mit
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.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.feature {
fill: #ccc;
}
.mesh {
fill: none;
stroke: #fff;
stroke-width: .5px;
stroke-linejoin: round;
}
.outline {
fill: #ddd;
stroke: #000;
stroke-width: 1.5px;
}
svg {
border:1px solid black
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 300,
height = 300;
var projection = d3.geo.mercator();
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("brazil.json", function(error, us) {
if (error) throw error;
var state = topojson.feature(us, us.objects.subunits);
//state = states.features.filter(function(d) { return d.id === 34; })[0];
console.log(projection);
projection
.scale(1)
.translate([0, 0]);
var b = path.bounds(state)
b.s = b[0][1];
b.n = b[1][1];
b.w = b[0][0];
b.e = b[1][0];
b.height = Math.abs(b.n - b.s);
b.width = Math.abs(b.e - b.w);
s = .9 / Math.max(b.width / width, (b.height / height));
t = [(width - s * (b.e + b.w)) / 2, (height - s * (b.n + b.s)) / 2];
projection
.scale(s)
.translate(t);
svg.append("path")
.datum(topojson.mesh(us, us.objects.subunits, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
svg.append("path")
.datum(state)
.attr("class", "outline")
.attr("d", path);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment