Skip to content

Instantly share code, notes, and snippets.

@infographicstw
Last active March 31, 2016 17:29
Show Gist options
  • Save infographicstw/bfdaf2a075225043c967 to your computer and use it in GitHub Desktop.
Save infographicstw/bfdaf2a075225043c967 to your computer and use it in GitHub Desktop.
Globe Visualization Example
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
<!DOCTYPE html><html lang="en"><head prefix="og: http://ogp.me/ns#"><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>template</title><!-- libraries--><script type="text/javascript" src="//d3js.org/d3.v3.min.js"></script><!-- custom files--><link rel="stylesheet" type="text/css" href="index.css"></head><body><svg id="svg" width="800px" height="600px" viewBox="0 0 800 600" preserveAspectRatio="xMidYMid"></svg><script type="text/javascript" src="http://d3js.org/topojson.v1.min.js"></script><script type="text/javascript" src="index.js"></script></body></html>
d3.json("world.json", function(world) {
var projection = d3.geo.orthographic().scale(245).translate([400,300]).clipAngle(90);
var path = d3.geo.path().projection(projection);
var countries = topojson.feature(world, world.objects.countries).features;
var color = d3.scale.category20();
console.log(countries);
var polygon = d3.select("#svg").selectAll("path").data(countries)
.enter().append("path").attr({"d":path,"fill":function(d){return color(d.id);}});
d3.select("#svg").call(d3.behavior.drag()
.origin(function() {
r = projection.rotate();
return {x: r[0], y: -r[1]};
})
.on("drag", function() {
rotate = projection.rotate();
projection.rotate([d3.event.x, -d3.event.y, rotate[2]]);
d3.select("#svg").selectAll("path").attr("d", path);
}));
});
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment