Xianping Li A learner

Interactive global map with D3.js

Global map

Inspired by these posts (this, this and this), I create a spherical map as follow:

Original codes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
	var width=600,height=600
	var projection=d3.geo.orthographic()
		.scale(300)
		.clipAngle(90)
		.translate([width/2,height/2])

	var path=d3.geo.path()
		.projection(projection)

	var svg=d3.select('#globalMap')
		.append('svg')
		.attr({
			width:width,
			height:height,
			id:'sphericalMap'
		})
		.on("mousedown", mousedown)
	    .on("mousemove", mousemove)
	    .on("mouseup", mouseup)
	    .style({
            display:'block',
            margin:'0 auto',
            'background-color':'#dadaeb'
        })

	var g=svg.append('g')

	var tooltip=d3.select('#globalMap')
		.append('div')
		.attr('class','tooltip')

	var url='https://raw.githubusercontent.com/altitudelabs/wanderawetest/master/app/map_data/world-topo-min.json';
	d3.json(url,function(error,world) {
		var countries=topojson.feature(world,world.objects.countries).features
		var neighbors=topojson.neighbors(world.objects.countries.geometries)
		var graticule=d3.geo.graticule()
		var color=d3.scale.category10()

		g.append('path')
			.datum(graticule)
			.attr({
				'd':path,
				class:'graticule'
			})
			.style({
				fill:'none',
				stroke:'#777',
				'stroke-width':'0.5px',
				'stroke-opacity':0.4
			})

		g.selectAll('.country')
			.data(countries)
			.enter()
			.append('path')
			.attr({
				d:path,
				fill:function(d,i) {
					return color(d.color=d3.max(neighbors[i],
						function (n) {return countries[n].color;})+1|0)
				},
				opacity:0.7
			})
			.on('mousemove',function(d,i) {
				var mouse=d3.mouse(svg.node()).map(function(d) {return parseInt(d)})

				d3.select(this).attr({
					'stroke-width':2,
					stroke:'white'
				})

				tooltip.attr('style','left:'+
					(mouse[0]+document.getElementById('sphericalMap').offsetLeft+10)+'px;top:'+
					(mouse[1]+document.getElementById('sphericalMap').offsetTop-15)+'px')
					.style({
						position:'absolute',
						background:'white',
						'font-size':'14px'
					})
					.html(d.properties.name)		
			})
			.on('mouseout',function(d) {
				d3.select(this).attr({
					'stroke-width':0,
					stroke:'none'
				})
				tooltip.style('display','none')
			})			
	})
	
	// Functions listed below for spherical rotation are copied from http://bl.ocks.org/patricksurry/5721459.
	function trackballAngles(pt) {  	  
	  var r = projection.scale();
	  var c = projection.translate();
	  var x = pt[0] - c[0], y = - (pt[1] - c[1]), ss = x*x + y*y;

	  var z = r*r > 2 * ss ? Math.sqrt(r*r - ss) : r*r / 2 / Math.sqrt(ss);  

	  var lambda = Math.atan2(x, z) * 180 / Math.PI; 
	  var phi = Math.atan2(y, z) * 180 / Math.PI
	  return [lambda, phi];
	}
	
	function composedRotation(λ, ϕ, γ, δλ, δϕ) {
	    λ = Math.PI / 180 * λ;
	    ϕ = Math.PI / 180 * ϕ;
	    γ = Math.PI / 180 * γ;
	    δλ = Math.PI / 180 * δλ;
	    δϕ = Math.PI / 180 * δϕ;
	    
	    var sλ = Math.sin(λ), sϕ = Math.sin(ϕ), sγ = Math.sin(γ), 
	        sδλ = Math.sin(δλ), sδϕ = Math.sin(δϕ),
	        cλ = Math.cos(λ), cϕ = Math.cos(ϕ), cγ = Math.cos(γ), 
	        cδλ = Math.cos(δλ), cδϕ = Math.cos(δϕ);

	    var m00 = -sδλ * sλ * cϕ + (sγ * sλ * sϕ + cγ * cλ) * cδλ,
	            m01 = -sγ * cδλ * cϕ - sδλ * sϕ,
	                m02 = sδλ * cλ * cϕ - (sγ * sϕ * cλ - sλ * cγ) * cδλ,
	        m10 = - sδϕ * sλ * cδλ * cϕ - (sγ * sλ * sϕ + cγ * cλ) * sδλ * sδϕ - (sλ * sϕ * cγ - sγ * cλ) * cδϕ,
	            m11 = sδλ * sδϕ * sγ * cϕ - sδϕ * sϕ * cδλ + cδϕ * cγ * cϕ,
	                 m12 = sδϕ * cδλ * cλ * cϕ + (sγ * sϕ * cλ - sλ * cγ) * sδλ * sδϕ + (sϕ * cγ * cλ + sγ * sλ) * cδϕ,
	        m20 = - sλ * cδλ * cδϕ * cϕ - (sγ * sλ * sϕ + cγ * cλ) * sδλ * cδϕ + (sλ * sϕ * cγ - sγ * cλ) * sδϕ,
	            m21 = sδλ * sγ * cδϕ * cϕ - sδϕ * cγ * cϕ - sϕ * cδλ * cδϕ,
	                 m22 = cδλ * cδϕ * cλ * cϕ + (sγ * sϕ * cλ - sλ * cγ) * sδλ * cδϕ - (sϕ * cγ * cλ + sγ * sλ) * sδϕ;
	                 
	    if (m01 != 0 || m11 != 0) {
	         γ_ = Math.atan2(-m01, m11);
	         ϕ_ = Math.atan2(-m21, Math.sin(γ_) == 0 ? m11 / Math.cos(γ_) : - m01 / Math.sin(γ_));
	         λ_ = Math.atan2(-m20, m22);
	    } else {
	         γ_ = Math.atan2(m10, m00) - m21 * λ;
	         ϕ_ = - m21 * Math.PI / 2;
	         λ_ = λ;       
	    }
	    
	    return([λ_ * 180 / Math.PI, ϕ_ * 180 / Math.PI, γ_ * 180 / Math.PI]);
	}
	    
	var m0 = null,
	    o0;
	  
	function mousedown() {  
	  m0 = trackballAngles(d3.mouse(svg[0][0]));
	  o0 = projection.rotate();
	  d3.event.preventDefault();
	}

	function mousemove() {
	  if (m0) { 
	    var m1 = trackballAngles(d3.mouse(svg[0][0]));	    
	    o1 = composedRotation(o0[0], o0[1], o0[2], m1[0] - m0[0], m1[1] - m0[1])	   
	    projection.rotate(o1);	    	    
	    svg.selectAll("path").attr("d", path); 
	  }
	}

	function mouseup() {
	  if (m0) {
	    mousemove();
	    m0 = null;
	  }
	}
</script>