-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
667 lines (561 loc) · 18 KB
/
Copy pathmain.js
File metadata and controls
667 lines (561 loc) · 18 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
///
///-----------
///
/// TODOs
///
///-----------
///
//maybe todo
//prevent drag panning with left mouse button (would prolly require jvectormap library editing)
//todo
//change color of unselectable countries
//todo
//change color of currently selected countries
//maybe todo
//maybe bugfix
//prevent multiple line drawing on a single country
//todo
//left click to select/deselect country migration lines
//todo
//select all timesteps
//todo bugfix
//setAttribute & toLower colled when deselecting by clicking on lines
//todo
//arrow keys left/right map movement
//todo
//group overlapping pie charts;
//zooming in separates the group into smaller pieces that do not overlap;
//zooming out combines smaller pie charts that would otherwise overlap;
//maybe todo
//animate line drawing
///
///-----------
///
/// Variables and Data Structures
///
///-----------
///
function MigrationDataPoint() {
var source_id = 0;
var target_id = 0;
var weight = 0;
};
function Country() {
var id = 0;//country id as seen in the dataset
var code = "";//country code as used by jVectorMap
var fullName = "";//full country name
var coords = [];//predefined country center (not working)
var element = null;//corresponding html element
//total migration values (all timesteps)
var totalIn = 0;
var totalOut = 0;
var totalPop = 0;
//holds the emigration data for this country (all timesteps)
var countryEmigrationData = [];
//holds the immigration data for this country (all timesteps)
var countryImmigrationData = [];
};
//total migration amount for current time step
var totalMigration = 0;
//maximum individual country migration value
var maxMigrationValue = 0;
//tooltip div
var tooltipDiv = null;
//holds all pie charts
var allPieCharts = [];
//holds all drawn lines..duh
var allLines = [];
//holds the main svg element
var theSVG = null;
//holds the svg map element
var theMap = null;
//holds all the data about all the countries
var countries = [];
//holds migration data
var migrationMatrix = [];
//holds the country names (from the data...)
var nodes = [];
//tracks the current timeStep
var currentTimeStep = 0;
//tracks the svg elements of currently selected countries
var selectedCountryElements = [];
//display emigration lines
var showEmigration = true;
//display immigration lines
var showImmigration = true;
//minimum migration line width
var minLineWidth = 1;
//toggle usage of minimum migration line width
var useMinimumLineWidth = false;
///
///-----------
///
/// Entry Point
///
///-----------
///
function init() {
initData();
addClickListeners();
calculateMigratioValues();
drawPieCharts();
};
///
///-----------
///
/// Visualization Flow Control
///
///-----------
///
function toggleMinimumLineWidth() {
useMinimumLineWidth = !useMinimumLineWidth;
reDrawSelectedCountryLines();
}
function toggleEmigration() {
showEmigration = !showEmigration;
reDrawSelectedCountryLines();
};
function toggleImmigration() {
showImmigration = !showImmigration;
reDrawSelectedCountryLines();
};
function calculateMigratioValues(){
totalMigration = 0;
maxMigrationValue = 0;
for (var c in countries) {
currentTotalMigration = Number(countries[c].totalPop[currentTimeStep]);
if(currentTotalMigration > maxMigrationValue) {
maxMigrationValue = currentTotalMigration;
}
totalMigration += currentTotalMigration;
}
};
function setTimeStep(timeStep){
currentTimeStep = timeStep;
if(timeStep == -1) {
currentTimeStep = 0;
}
calculateMigratioValues();
reDrawSelectedCountryLines();
clearPieCharts();
drawPieCharts();
};
function clearLines(){
for(var path in allLines){
allLines[path].remove();
}
allLines = [];
};
function clearSelected(){
for(var elementIndex in selectedCountryElements){
selectedCountryElements[elementIndex].setAttribute("selected", "false");
selectedCountryElements = [];
}
};
function reDrawSelectedCountryLines(){
clearLines();
for (var elementIndex in selectedCountryElements) {
drawCountryLines(selectedCountryElements[elementIndex]);
}
};
function clearPieCharts() {
for(var pieChartIndex in allPieCharts){
allPieCharts[pieChartIndex].remove();
}
allPieCharts = [];
}
///
///-----------
///
/// Data & Variable Init //Highly unoptimized
///
///-----------
///
function populateMigrationMatrix(){
var dataSplit = migrationData.split("\n");
var dataPosition = 0;
for(var i = 0; i < dataSplit.length; i++){
nodes[i] = dataSplit[i];
dataPosition++;
if(dataSplit[i] == ""){
break;
}
}
//timestep = one row in migrationMatrix
var dataPointCounter = 0;
var timeStepCounter = 0;
migrationMatrix = [];
migrationMatrix[timeStepCounter] = [];
for(var i = dataPosition; i < dataSplit.length - 1; i++){
if(dataSplit[i] == ""){//new timestep
timeStepCounter++;//increase the timeStepCounter
dataPointCounter = 0;//reset dataPointCounter
migrationMatrix
migrationMatrix[timeStepCounter] = [];//add a new array row
}
migrationMatrix[timeStepCounter][dataPointCounter] = new MigrationDataPoint();
var data = [];
data = dataSplit[i].split(" ");
migrationMatrix[timeStepCounter][dataPointCounter].source_id = data[0];
migrationMatrix[timeStepCounter][dataPointCounter].target_id = data[1];
migrationMatrix[timeStepCounter][dataPointCounter].weight = data[2];
dataPointCounter++;
}
};
function initData(){
populateMigrationMatrix();
// extract country names from nodes
for(var i = 0; i < nodes.length; i++){
var splitNodes = nodes[i].split("/");
nodes[i] = splitNodes[splitNodes.length - 1];
}
// move countryData into the countries array
var i = 0;
for(var c in countryData) {
var country = new Country();
country.code = c;
country.fullName = countryData[c].fullName;
country.coords = countryData[c].coords;
countries[i] = country;
i++;
}
// set ids for all countries
for(var c in countries){
setCountryId(countries[c]);
}
var paths = $("path");
//save html elements to countries array
for (var c in countries) {
for (var p = 0; p < paths.length; p++) {
if( $(paths[p]).data().code.toLowerCase() == countries[c].code.toLowerCase()) {
countries[c].element = paths[p];
countries[c].element.setAttribute("isSelected", "false");
break;
}
}
}
//remove irrelevant countries
var tempArray = [];
for(var c in countries){
if(countries[c].id){
tempArray.push(countries[c]);
}
}
countries = tempArray;
//color countries
for (var c in countries) {
// $(countries[c].element).attr("fill", "gray");
}
//assign each country its own migrationMatrix
for(var c in countries) {
var currentCountry = countries[c];
//outgoing population data
currentCountry.countryEmigrationData = [];
//incoming population data
currentCountry.countryImmigrationData = [];
//total incoming population per timeStep
currentCountry.totalIn = [];
//total outgoing population per timeStep
currentCountry.totalOut = [];
//total migrating population (in + out) per timeStep
currentCountry.totalPop = [];
for(var timeStep in migrationMatrix){
if(!currentCountry.countryEmigrationData[timeStep]){
//add a new array row for the current timeStep
currentCountry.countryEmigrationData[timeStep] = [];
currentCountry.countryImmigrationData[timeStep] = [];
currentCountry.totalIn[timeStep] = 0;
currentCountry.totalOut[timeStep] = 0;
currentCountry.totalPop[timeStep] = 0
}
//outgoing pop data entry counter
var currentDataPoint = 0;
//incoming pop data entry counter
var currentDataPointIn = 0;
for(var dataPoint in migrationMatrix[timeStep]){
var currentMigrationDataPoint = migrationMatrix[timeStep][dataPoint];
//outgoing population (the source is the currentCountry)
if(currentMigrationDataPoint.source_id == currentCountry.id){
currentCountry.countryEmigrationData[timeStep][currentDataPoint] = currentMigrationDataPoint;
currentDataPoint++;
currentCountry.totalOut[timeStep] += Number(currentMigrationDataPoint.weight);
}
//incoming population (the target is the currentCountry)
if(currentMigrationDataPoint.target_id == currentCountry.id){
currentCountry.countryImmigrationData[timeStep][currentDataPointIn] = currentMigrationDataPoint;
currentDataPointIn++;
currentCountry.totalIn[timeStep] += Number(currentMigrationDataPoint.weight);
}
}//end dataPoint iteration
//total migrating population (in + out)
currentCountry.totalPop[timeStep] = Number(currentCountry.totalOut[timeStep]) + Number(currentCountry.totalIn[timeStep]);
}//end timeStep iteration
}//end country iteration
//set country id to be equal to its position in our countries array
var tempArray = [];
for (var c in countries) {
tempArray[countries[c].id] = countries[c];
}
countries = tempArray;
//add the theMap id to our svg element
$("svg")[0].id = "theSVG";//the svg canvas
$("g")[0].id = "theMap";//the map canvas in the svg (is actually a <g> element...)
//get the SVG
theSVG = d3.select("#theSVG");
//assign the map to a variable
theMap = d3.select("#theMap");
//assign tooltip
tooltipDiv = $("#theTooltip")[0];
// $(p).data()
// Object {code: "FR"}
// $(p).data().code
// "FR"
/*
$(p).attr("fill")
"white"
$(p).attr("fill", "blue")
*/
};//endof initData()
///
///-----------
///
/// Helpers & Utils
///
///-----------
///
function getCountryByCode(countryCode) {
for(var c in countries){
if(countries[c].code.toLowerCase() == countryCode.toLowerCase()) return countries[c];
}
return null;
};
function getCountryCenter(countryId){
var point = Object();
point.x = 0;
point.y = 0;
var country = countries[countryId];
if(!country) return null;//non-existant map element
if(!country.element) return null;//non-existant map element
var e = country.element;
var boundRect = e.getBBox();
point.x = (2*boundRect.x + boundRect.width) / 2;
point.y = (2*boundRect.y + boundRect.height) / 2;
return point;
};
function toRad(degrees) {
return degrees * Math.PI / 180;
};
function setCountryId(country) {
for(var nodeId in nodes){
if(country.fullName == nodes[nodeId]) country.id = nodeId;
}
};
///
///-----------
///
/// Event Listeners
///
///-----------
///
function moveMap(direction){
var transformString = theMap.attr("transform");
var translateString = transformString.substring(transformString.lastIndexOf("("));
var translateValue = Number(translateString.substring(translateString.lastIndexOf(",")+1, translateString.length-1));
if(direction == "up"){
translateValue += 10;
} else if(direction == "down") {
translateValue -= 10;
}
console.log(translateValue);
var newTransformString = transformString.substring(0, transformString.lastIndexOf(",") + 1);
newTransformString += " ";
newTransformString += translateValue;
newTransformString += ")";
theMap.attr("transform", newTransformString);
}
function addClickListeners(){
//+ - zoom function
var body = $("body")[0];
body.onkeydown = function(key) {
if(key.keyCode == 189 || key.keyCode == 107) {//key = or +(numpad)
$(".jvectormap-zoomout")[0].click();
} else if (key.keyCode == 187 || key.keyCode == 109) {//key - or -(numpad)
$(".jvectormap-zoomin")[0].click();
} else if (key.keyCode == 38) {//up arrow
moveMap("up");
} else if (key.keyCode == 40) {//down arrow
moveMap("down");
} else if (key.keyCode == 37) {//left arrow
} else if (key.keyCode == 39) {//right arrow
}
}
$("svg").on("click", function(event) {
if(event.which == 2){
//drag event with middle click, do nothing special
return;
}
if(event.target.tagName == "path"){
if(event.ctrlKey == true || event.shiftKey == true){
drawCountryLines(event.target);
event.target.setAttribute("selected", "true");
selectedCountryElements.push(event.target);
} else {
clearLines();
drawCountryLines(event.target);
event.target.setAttribute("selected", "true");
selectedCountryElements.push(event.target);
}
} else if (event.target.tagName == "svg") {
clearLines();
clearSelected();
}
});
};
///
///-----------
///
/// Drawing
///
///-----------
///
var lineFunction = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("linear");
function drawAll(){
clearLines();
for(var c in countries){
var e = countries[c].element;
$(e).click();
}
reDrawSelectedCountryLines();
}
function drawPieCharts() {
//custom values
var minInnerRadius = 2;
var minOuterRadius = 7;
for (var c in countries) {
center = getCountryCenter(c);
if(center == null) continue;
//draw the pie chart in the center of our country
var translationPoint = "translate(" + center.x + "," + center.y + ")";
var totalPop = countries[c].totalPop[currentTimeStep];
var inPop = countries[c].totalIn[currentTimeStep];
var outPop = countries[c].totalOut[currentTimeStep];
//using a custom pie chart ratio scale: from 0 to totalMigrationPopulation
var popScale = d3.scale.linear().domain([0, totalPop]).range([0, 2 * Math.PI]);
var innerRadius = ((totalPop / maxMigrationValue) * 20) / 2;
if(innerRadius < minInnerRadius) innerRadius = minInnerRadius;//make sure innerRadius is not too small
var outerRadius = (totalPop / maxMigrationValue) * 20;
if(outerRadius < minOuterRadius) outerRadius = minOuterRadius;//make sure outerRadius is not too small
var inArc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius)
.startAngle(popScale(0))
.endAngle(popScale(inPop));
var outArc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius)
.startAngle(popScale(inPop))
.endAngle(popScale(totalPop));
var inPieChart = theMap.append("path")
.attr("d", inArc)
.attr("fill", "#4DCB6D")
.attr("transform", translationPoint)
.on("mouseover", tooltipMouseover)
.on("mousemove", tooltipMousemove)
.on("mouseout", tooltipMouseout)
.attr("type", "chart")
.attr("tooltip", "Immigration to " + countries[c].fullName + " : " + inPop );
var outPieChart = theMap.append("path")
.attr("d", outArc)
.attr("fill", "red")
.attr("transform", translationPoint)
.on("mouseover", tooltipMouseover)
.on("mousemove", tooltipMousemove)
.on("mouseout", tooltipMouseout)
.attr("type", "chart")
.attr("tooltip", "Emigration from " + countries[c].fullName + " : " + outPop );
allPieCharts.push(inPieChart);
allPieCharts.push(outPieChart);
}
};
function tooltipMouseover() {
$(tooltipDiv).css("display", "inline");
$(this).css("opacity", "0.5");
}
function tooltipMousemove() {
tooltipDiv.innerHTML = $(this).attr("tooltip");
$(tooltipDiv).attr("style", "left : " + d3.event.pageX +"px; top: " + d3.event.pageY + "px;");
}
function tooltipMouseout() {
$(tooltipDiv).css("display", "none");
if($(this).attr("type") == "chart"){
$(this).css("opacity", "1");
}
else{
$(this).css("opacity", "0.2");
}
}
function drawCountryLines(countryElement){
var country = getCountryByCode($(countryElement).data().code);
if(!country) return;//check if country exists on the map
var emigrationData = country.countryEmigrationData;
var immigrationData = country.countryImmigrationData;
var center = getCountryCenter(country.id);
if(center == null) return;
if(showEmigration)
//draw emigration lines
for(var dataPointIndex in emigrationData[currentTimeStep]){
var dataPoint = emigrationData[currentTimeStep][dataPointIndex];
if (!countries[dataPoint.target_id]) continue; //check if target country exists on the map
var targetCenter = getCountryCenter(dataPoint.target_id);
if (targetCenter == null) continue;
var lineData = [
{ "x": center.x, "y": center.y},
{ "x": targetCenter.x, "y": targetCenter.y}
];
var lineWidth = (dataPoint.weight / totalMigration) * 1000;
if(useMinimumLineWidth) {
if(lineWidth < minLineWidth) lineWidth = minLineWidth;
}
var lineGraph = theMap.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "red")
.attr("stroke-width", lineWidth)
.attr("fill", "none")
.style("opacity", 0.2)
.on("mouseover", tooltipMouseover)
.on("mousemove", tooltipMousemove)
.on("mouseout", tooltipMouseout)
.attr("tooltip", "Emigration from " + country.fullName + " to " + countries[dataPoint.target_id].fullName + ": " + dataPoint.weight);
allLines.push(lineGraph[0][0]);
}
if(showImmigration)
//draw immigration lines
for(var dataPointIndex in immigrationData[currentTimeStep]){
var dataPoint = immigrationData[currentTimeStep][dataPointIndex];
if (!countries[dataPoint.target_id]) continue; //check if target country exists on the map
var targetCenter = getCountryCenter(dataPoint.source_id);
if (targetCenter == null) continue;
var lineData = [
{ "x": center.x, "y": center.y},
{ "x": targetCenter.x, "y": targetCenter.y}
];
var lineWidth = (dataPoint.weight / totalMigration) * 1000;
if(useMinimumLineWidth) {
if(lineWidth < minLineWidth) lineWidth = minLineWidth;
}
var lineGraph = theMap.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "#4DCB6D")
.attr("stroke-width", lineWidth)
.attr("fill", "none")
.style("opacity", 0.2)
.on("mouseover", tooltipMouseover)
.on("mousemove", tooltipMousemove)
.on("mouseout", tooltipMouseout)
.attr("tooltip", "Immigration from " + countries[dataPoint.source_id].fullName + " to " + country.fullName + ": " + dataPoint.weight);
allLines.push(lineGraph[0][0]);
}
};