forked from shawnbot/topogram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
459 lines (408 loc) · 14.5 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html>
<html>
<head>
<title>World scaled by number of documents in Web of Science by Authors Living There</title>
<meta charset="utf-8">
<meta property="og:image" content="isi_articles_cartogram.png">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="lib/colorbrewer.js"></script>
<script src="lib/topojson.js"></script>
<script src="cartogram.js"></script>
<style type="text/css">
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
padding: 0;
margin: 0;
}
#container {
width: 800px;
margin: 20px auto;
}
h1 {
font-size: 140%;
margin: 0 0 15px 0;
text-align: center;
}
h2 {
font-size: 160%;
margin: 0 0 10px 0;
}
p {
margin: 0 0 10px;
}
form, form > * {
margin: 0;
}
#map-container {
width: 800px;
height: 500px;
text-align: center;
position: relative;
margin: 20px 0;
}
#map {
display: block;
position: absolute;
background: #fff;
width: 100%;
height: 100%;
margin: 0;
}
path.country {
stroke: #666;
stroke-width: .5;
}
path.country:hover {
stroke: #000;
}
form {
font-size: 120%;
}
select {
font-size: inherit;
}
#placeholder {
position: absolute;
z-index: -1;
display: block;
left: 0;
top: 0;
}
#theyear {
line-height: 0.4em;
border: 1px;
font-size: 4em;
position: absolute;
top: 40px;
right: 0px;
}
#about {
width: 600px;
font-size: .9em;
}
#credits {
position: absolute;
bottom: 10px;
right: 0px;
font-size: 1em;
}
</style>
</head>
<body>
<div id="container">
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<h1>World scaled by number of documents in Web of Science by Authors Living There</h1>
<div id="map-container">
<img id="placeholder" alt="placeholder image for old browsers" src="isi_articles_cartogram.png">
<svg id="map"></svg>
<div id="theyear">
<span></span><br />
<input style="position:relative; width:110px" type="range" min="1990" max="2011" value="1990">
</div>
<div id="credits">Created by <a href="https://twitter.com/intent/follow?screen_name=juancommander">@juancommander</a> using <a href="http://d3js.org">d3.js</a> and <a href="https://github.com/shawnbot/d3-cartogram/">cartogram.js</a>. CC-BY J.P. Alperin</div>
</div>
<form>
<p>
<!--<label>Scale by <select id="field"></select></label>-->
<!--<label>in <select id="year"></select></label>-->
<!--<span id="status"></span>-->
</p>
</form>
<div id="about">
<h2>About</h2>
<p>
This map was produced by Juan Pablo Alperin (<a href="https://twitter.com/intent/follow?screen_name=juancommander">@juancommander</a>) using
<a href="http://d3js.org">d3.js</a> and <a href="https://github.com/shawnbot/d3-cartogram/">cartogram.js</a>.
It was inspired by the out of date map over at <a href="http://worldmapper.org/display.php?selected=205">worldmapper.org</a>.
Unfortunately, not much has changed. Due to differences in the implementations, the two maps cannot be directly
compared to one another.
</p>
<p>
Juan Pablo is a PhD Candidate at Stanford University and a researcher with the <a href="http://pkp.sfu.ca">Public Knowledge Project</a>.
</p>
<p>
This work was carried out with the support of the IDRC-funded project,
<a href="http://flacso.org.br/oa/calidad-en-la-comunicacion-cientifica-abierta-de-america-latina/?lang=en">Quality in the Open Scholarly Communication of Latin America</a>.
The data are from Thomson Reuters and were collected by the Mimir Project conducted at Stanford University by Daniel A. McFarland, Dan Jurafsky, Chris Manning, and Woody Powell,
which was generously funded by the Office of the President at Stanford University and by the National Science Foundation [Award 0835614]. </p>
<p>
The code (including most of this page) was adapted from the example provided by
<a href="https://github.com/shawnbot/d3-cartogram/">shawnbot</a>.
You can see the source code over on <a href="https://github.com/jalperin/d3-cartogram">Juan's github</a>.
</p>
</div>
<script>
// hide the form if the browser doesn't do SVG,
// (then just let everything else fail)
if (!document.createElementNS) {
document.getElementsByTagName("form")[0].style.display = "none";
}
// field definitions from:
// <http://www.census.gov/popest/data/national/totals/2011/files/NST-EST2011-alldata.pdf>
var percent = (function() {
var fmt = d3.format(".2f");
return function(n) { return fmt(n) + "%"; };
})(),
years = [1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011],
fields = [
// {name: "(no scale)", id: "none"},
{name: "Number of Articles in ISI", id: "num_articles", key: "num_articles%d", years: years}
// {name: "Population", id: "num_articles2001", key: "num_articles2001", format: "+,"},
// {name: "% FB users", id: "p_fb_users", key: "p_fb_users", format: percent},
// {name: "% of Articles", id: "p_int_users", key: "p_int_users", format: percent},
// {name: "% Internet users", id: "p_int_users", key: "p_int_users", format: percent},
// {name: "% FB users of Internet Users", id: "p_fb_users_of_int_users", key: "p_fb_users_of_int_users", format: percent}
],
fieldsById = d3.nest()
.key(function(d) { return d.id; })
.rollup(function(d) { return d[0]; })
.map(fields),
field = fields[0],
year = years[0],
colors = colorbrewer.RdYlBu[3]
.reverse()
.map(function(rgb) { return d3.hsl(rgb); });
var body = d3.select("body"),
stat = d3.select("#status");
//var fieldSelect = d3.select("#field")
// .on("change", function(e) {
// field = fields[this.selectedIndex];
// location.hash = "#" + [field.id, year].join("/");
// });
//
//fieldSelect.selectAll("option")
// .data(fields)
// .enter()
// .append("option")
// .attr("value", function(d) { return d.id; })
// .text(function(d) { return d.name; });
//var yearSelect = d3.select("#year")
// .on("change", function(e) {
// year = years[this.selectedIndex];
// location.hash = "#" + [field.id, year].join("/");
// });
//
//yearSelect.selectAll("option")
// .data(years)
// .enter()
// .append("option")
// .attr("value", function(y) { return y; })
// .text(function(y) { return y; })
d3.select("input[type=range]").on("change", function() { year = this.value; update(); });
d3.select("input[type=range]").on("click", function() { clearInterval(intervalId); });
var intervalId = setInterval(function(){
if (year < d3.max(years)) {
year = year + 1;
update()
} else {
setInterval(intervalId);
}
},2000);
var map = d3.select("#map"),
zoom = d3.behavior.zoom()
.translate([-38, 32])
.scale(.94)
.scaleExtent([0.5, 10.0])
.on("zoom", updateZoom),
layer = map.append("g")
.attr("id", "layer"),
countries = layer.append("g")
.attr("id", "countries")
.selectAll("path");
// map.call(zoom);
updateZoom();
function updateZoom() {
var scale = zoom.scale();
layer.attr("transform",
"translate(" + zoom.translate() + ") " +
"scale(" + [scale, scale] + ")");
}
var proj = d3.geo.equirectangular(),
topology,
geometries,
rawData,
dataById = {},
carto = d3.cartogram()
.projection(proj)
.properties(function(d) {
if (d.id in dataById) {
return dataById[d.id];
} else {
var tmp = d3.keys(dataById[d3.keys(dataById)[0]]);
var ret = {"numcode": d.id};
tmp.forEach(function(i){ret[i] = 0});
return ret;
}
})
.value(function(d) {
return +d.properties[field];
});
//window.onhashchange = function() {
// parseHash();
//};
var segmentized = location.search === "?segmentized",
url = ["data",
segmentized ? "world-segmentized.topojson" : "world-110m.json"
].join("/");
d3.json(url, function(topo) {
topology = topo;
geometries = topology.objects.countries.geometries;
d3.csv("data/isi_article_counts.csv", function(data) {
rawData = data;
dataById = d3.nest()
.key(function(d) { return d.numcode; })
.rollup(function(d) { return d[0]; })
.map(data);
init();
});
});
function init() {
var features = carto.features(topology, geometries),
path = d3.geo.path()
.projection(proj);
countries = countries.data(features)
.enter()
.append("path")
.attr("class", "country")
.attr("id", function(d) {
return d.id;
})
.attr("name", function(d) {
return d.properties.country;
})
.attr("fill", "#fafafa")
.attr("d", path);
countries.append("title");
// parseHash();
}
function reset() {
stat.text("");
// body.classed("updating", false);
var features = carto.features(topology, geometries),
path = d3.geo.path()
.projection(proj);
countries.data(features)
.transition()
.duration(750)
.ease("linear")
.attr("fill", "#fafafa")
.attr("d", path);
countries.select("title")
.text(function(d) {
return d.id;
});
}
function update() {
var start = Date.now();
// body.classed("updating", true);
var key = field.key.replace("%d", year),
fmt = (typeof field.format === "function")
? field.format
: d3.format(field.format || ","),
value = function(d) {
return +d.properties[key];
},
values = countries.data()
.map(value)
.filter(function(n) {
return !isNaN(n);
})
.sort(d3.ascending),
lo = values[0],
hi = values[values.length - 1];
var color = d3.scale.linear()
.range(colors)
.domain(lo < 0
? [lo, 0, hi]
: [lo, d3.mean(values), hi]);
// normalize the scale to positive numbers
var scale = d3.scale.linear()
.domain([lo, hi])
.range([1, 1000]);
// tell the cartogram to use the scaled values
carto.value(function(d) {
return scale(value(d));
});
// generate the new features, pre-projected
var features = carto(topology, geometries).features;
// update the data
countries.data(features)
.select("title")
.text(function(d) {
return [d.properties.country, fmt(value(d))].join(": ");
});
countries.transition()
.duration(750)
.ease("linear")
.attr("fill", function(d) {
return color(value(d));
})
.attr("d", carto.path);
// var delta = (Date.now() - start) / 1000;
// stat.text(["calculated in", delta.toFixed(1), "seconds"].join(" "));
// body.classed("updating", false);
d3.select("input[type=range]")[0][0].value=year;
d3.select("#theyear > span")[0][0].innerHTML=year;
}
var deferredUpdate = (function() {
var timeout;
return function() {
var args = arguments;
clearTimeout(timeout);
stat.text("calculating...");
return timeout = setTimeout(function() {
update.apply(null, arguments);
}, 10);
};
})();
//var hashish = d3.selectAll("a.hashish")
// .datum(function() {
// return this.href;
// });
//
//function parseHash() {
// var parts = location.hash.substr(1).split("/"),
// desiredFieldId = parts[0],
// desiredYear = +parts[1];
//
// field = fieldsById[desiredFieldId] || fields[0];
// year = (years.indexOf(desiredYear) > -1) ? desiredYear : years[0];
//
// fieldSelect.property("selectedIndex", fields.indexOf(field));
//
// if (field.id === "none") {
//
// yearSelect.attr("disabled", "disabled");
// reset();
//
// } else {
//
// if (field.years) {
// if (field.years.indexOf(year) === -1) {
// year = field.years[0];
// }
// yearSelect.selectAll("option")
// .attr("disabled", function(y) {
// return (field.years.indexOf(y) === -1) ? "disabled" : null;
// });
// } else {
// yearSelect.selectAll("option")
// .attr("disabled", null);
// }
//
// yearSelect
// .property("selectedIndex", years.indexOf(year))
// .attr("disabled", null);
//
// deferredUpdate();
// location.replace("#" + [field.id, year].join("/"));
//
// hashish.attr("href", function(href) {
// return href + location.hash;
// });
// }
//}
</script>
</body>
</html>