forked from kcarnold/pcmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
179 lines (156 loc) · 5.72 KB
/
index.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<meta charset="utf-8">
<style>
/* 292929,ABABAB,3F6B38,48A1A8,87563B, */
body {
background: #292929;
}
.states path {
fill: #FFF7FB;
stroke: #292929;
transition: fill 1s ease;
-webkit-transition: fill 1s ease;
}
.countries path {
fill: #FFF7FB;
stroke: #292929;
transition: fill 1s ease;
-webkit-transition: fill 1s ease;
/*stroke-dasharray: 1, 1;*/
/*stroke-width: 0.5px;*/
}
</style>
<body>
<script src="d3.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script src="underscore.js"></script>
<script>
var width = 960, height = 500;
var svg = d3.select('body').append('svg')
.attr('width', width).attr('height', height);
var projection = d3.geo.mercator().scale(850).translate([600, 350]);
var path = d3.geo.path()
.projection(projection);
var statePath = d3.geo.path().projection(
d3.geo.albersUsa().scale(600).translate([300, 150]));
queue()
.defer(d3.json, 'countries.topo.json')
.defer(d3.json, 'states.topo.json')
.defer(d3.csv, 'volunteer_countries.csv')
.await(ready);
function sum(arr) {
return _.reduce(arr, function(a, b) { return (+a) + (+b); }, 0);
}
function kvMap(obj, fn) {
var res = {};
_.forEach(obj, function(val, key) {
var tmp = fn(key, val);
res[tmp[0]] = tmp[1];
});
return res;
}
function ready(error, world, states_topo, volunteer_countries) {
// volunteer_countries is:
// post_cntry_num, post_cntry_name, hs_state, count
id2name = {};
name2id = {};
countries = topojson.object(world, world.objects.countries).geometries;
countries.forEach(function(d) {
id2name[d.id] = d.properties.name;
name2id[d.properties.name.toLowerCase()] = d.id;
});
_.extend(name2id, {
'solomon islands': 'SLB', // Solomon Islands
'car': 'CAF', // Central African Republic
'png': 'PNG', // Papua New Guinea
'zaire': 'COD', // Democratic Republic of the Congo
'dominican republic': 'DOM',
"cook islands": "cook islands",
"cote d ivoire": "cote d ivoire",
"czech":"czech",
"east timor": "east timor",
"eastern caribbean": "eastern caribbean",
"equatorial guinea": "equatorial guinea",
"guinea bissau":"guinea bissau",
"korea south":"korea south",
"leeward islands":"leeward islands",
"marshall islands":"marshall islands",
"russia far east":"russia far east",
"russia western":"russia western",
"sao tome":"sao tome",
"sav":"sav",
"tuvalu":"tuvalu",
"windward islands":"windward islands"
});
faileds = {};
function pcNameToId(name) {
name = name.toLowerCase();
if (_.has(name2id, name)) {
return name2id[name];
}
faileds[name] = name;
}
country_to_state = d3.nest()
.key(function(d) { return pcNameToId(d.post_cntry_name); })
.key(function(d) { return d.hs_state; })
.rollup(function(entries) { return sum(_.pluck(entries, 'count')); })
.map(volunteer_countries);
state_to_country = d3.nest()
.key(function(d) { return d.hs_state; })
.key(function(d) { return pcNameToId(d.post_cntry_name); })
.rollup(function(entries) { return sum(_.pluck(entries, 'count')); })
.map(volunteer_countries);
var total_from_country = kvMap(country_to_state, function(country, states) {
return [country, sum(_.values(states))];
});
var total_from_state = kvMap(state_to_country, function(state, countries) {
return [state, sum(_.values(countries))];
});
var total_total = sum(_.values(total_from_country));
window.total_from_state = total_from_state;
function setStateHighlights(activeCountry) {
if (!_.has(country_to_state, activeCountry)) activeCountry = null;
var scale = d3.scale.linear()
.domain([0, 1])
.range(['#F7FCFD', '#00441B']);
var byState = activeCountry ? country_to_state[activeCountry] : total_from_state;
d3.selectAll('.states path').style('fill', function(d) {
var total = activeCountry ? total_from_country[activeCountry] / 50 : total_total / 100;
return scale(Math.min(1, byState[d.id] / total));
});
}
function setCountryHighlights(activeState) {
var total = activeState ? total_from_state[activeState] : total_total;
var scale = d3.scale.linear()
.domain([0, 1])
.range(['#FFF7FB', '#023858']);
var byCountry = activeState ? state_to_country[activeState] : total_from_country;
d3.selectAll('.countries path').style('fill', function(d) { return scale(Math.min(1, byCountry[d.id] / (total/100))); });
}
var countries_g = svg.append('g').attr('class', 'countries PuBu');
countries_g.selectAll('path').data(countries)
.enter().append('svg:path')
.attr('d', path).attr('id', function(d) { return 'country-'+d.id; })
.on('mouseenter', function(d) {
setStateHighlights(d.id);
})
.on('mouseleave', function(d) {
setStateHighlights();
});
var states = topojson.object(states_topo, states_topo.objects.states).geometries;
var states_g = svg.append('g').attr('class', 'states PuBu');
states_g.selectAll('path').data(states)
.enter().append('svg:path')
.attr('d', statePath).attr('id', function(d) { return 'state-'+d.id; })
.on('mouseenter', function(d) {
setCountryHighlights(d.id);
})
.on('mouseleave', function(d) {
setCountryHighlights();
});
setStateHighlights();
setCountryHighlights();
}
</script>
</body>