for http://blog.infographics.tw/2015/04/d3js-with-global-sphere-map/
Last active
March 31, 2016 17:29
-
-
Save infographicstw/bfdaf2a075225043c967 to your computer and use it in GitHub Desktop.
Globe Visualization Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, | |
body { | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
padding: 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
})); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment