-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcs4460Project.html
More file actions
1174 lines (1032 loc) · 43.4 KB
/
cs4460Project.html
File metadata and controls
1174 lines (1032 loc) · 43.4 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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- Note: Code that has not been identified as having been imported has been written from scratch. -->
<!DOCTYPE html>
<head>
<link rel="icon" href="globe.ico" type="image/x-icon" >
<meta charset="utf-8">
<title>Team World Life</title>
<style>
/*CSS STYLES*/
/*CSS for play button imported and motified. Other CSS written from scratch*/
.play_border {
margin-left:15px;
border: 2px solid rgba(0,0,0,0.7);
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
width: 40px;
height: 40px;
-webkit-box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease;
cursor: pointer;
margin-top: -10px;
position: relative;
z-index: 10px;
}
.play_border:hover{
border-color: transparent;
-webkit-box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
}
.play_border:hover .play_button{
border-left: 10px solid rgba(0,0,0,0.5);
}
.play_border:active,.play_border:focus{
-webkit-box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.2);
-moz-box-shadow: 0px 0px 5.play_button:hoverpx 2px rgba(0, 0, 0, 0.2);
box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.2);
}
.play_button {
position:relative;
top: 10px;
left: 40%;
width: 0;
height: 0;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-left: 10px solid rgba(0,0,0,0.8);
position: relative;
z-index: 10px;
}
@font-face {
font-family: "Telegrafico";
src: url("Telegrafico.ttf");
}
body {
background-color: #ffffff;
margin:0;
padding: 0px;
}
.top-banner {
position: fixed;
background: #2c3e50;
text-align: center;
display: block;
color: #ffffff;
font-size: 25px;
height:35px;
width: 100%;
padding:2px;
z-index: 5;
}
.top-banner a {
font-family: "Telegrafico", sans-serif;
color:#ffffff;
cursor: pointer;
text-decoration:none;
letter-spacing: 4px;
font-weight: bold;
margin-right: 280px;
}
/* Path and line styling to make the axis look nicer */
.line {
fill: none;
stroke-width: 1.5px;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges; /* This is an SVG property that makes sure there is no blur on lines */
}
.axis text {
font-family: Century Gothic, sans-serif;
font-size: 11px;
}
text {
font-family: Century Gothic, sans-serif;
}
p {
font-family: Century Gothic, sans-serif;
}
#dropDown {
margin-left: 5px;
margin-top: 14px;
float: left;
}
#dataDropDown {
margin-left: 5px;
margin-top: 14px;
float: left;
}
.svg-tooltip {
background: rgb(240,240,240);
border-radius: 2px;
}
.tooltip {
padding: 1px;
font-family: Century Gothic, sans-serif;
text-align: center;
color: rgb(0,0,0);
}
#basic_choropleth {
position: relative;
width: 1400px;
height: 600px;
margin: 0 auto;
}
#exitGraph {
float: left;
margin-top: 71px;
margin-left: 1325px;
position: absolute;
font-size: 24px;
font-weight: bold;
line-height: 18px;
color: #000000;
text-shadow: 0 1px 0 #ffffff;
opacity: 0.6;
filter: alpha(opacity=20);
text-decoration: none;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border:none;
background-color:#FFFFFF;
}
#exitGraph:hover {
color: #000000;
text-decoration: none;
opacity: 0.8;
filter: alpha(opacity=40);
cursor: pointer;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#exitGraph.hidden {
display: none;
}
#clearGraph {
float: left;
margin-top: 71px;
margin-left: -15px;
position: absolute;
font-size: 14px;
font-weight: bold;
line-height: 18px;
color: #000000;
text-shadow: 0 1px 0 #ffffff;
opacity: 0.8;
filter: alpha(opacity=20);
text-decoration: none;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border:none;
background-color:#FFFFFF;
}
#clearGraph:hover {
color: #000000;
text-decoration: none;
opacity: 0.9;
filter: alpha(opacity=40);
cursor: pointer;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#clearGraph.hidden {
display: none;
}
#scaleYRadio {
float: left;
margin-top: 30px;
margin-left: 1150px;
position: absolute;
font-family: Century Gothic, sans-serif;
font-size: 14px;
width: 260px;
}
#scaleYRadio.hidden {
display: none;
}
#annotationsRadio {
position: absolute;
z-index: 6;
margin-top: 495px;
margin-left: 60px;
font-family: Century Gothic, sans-serif;
font-size: 14px;
width: 250px;
line-height: 1%;
}
</style>
<!-- Stylesheets and javascript for JQRangeSlider, used to select years. Imported and used as is. CSS heavily modified. -->
<link rel="stylesheet" id="themeCSS" href="iThing.css">
<link rel="stylesheet" href="style.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
<!-- <script src="jquery.mousewheel.min.js"></script> -->
<script src="../jQAllRangeSliders-min.js"></script>
</head>
<body>
<!-- D3, topojson (for datamaps), datamaps, and zoompan.js (for zooming). -->
<!-- Imported and used as is, minor modifications to datamaps.world.min.js -->
<script type="text/javascript" src="d3.v3.js"></script>
<script type="text/javascript" src="topojson.js"></script>
<script src="datamaps.world.min.js"></script>
<script src="zoompan.js"></script>
<!-- Top banner. Written from scratch. -->
<div class="top-banner">
<!-- Dataset drop down. Written from scratch. -->
<div style="width: 280px; margin: auto; margin-top: -7px; float: left;">
<p style="float: left; margin-left: 20px; font-size: 15px; font-family: Century Gothic, sans-serif;">Dataset:</p>
<select name="datsetList" id="dataDropDown">
<option value="0" id="dataDefault">Life Expectancy </option>
<option value="1" id="dataDefault">GDP Per Capita</option>
<option value="2" id="dataDefault">Unemployment Percent</option>
<option value="3" id="dataDefault">Total Population</option>
<option value="4" id="dataDefault">Internet Users Percent</option>
</select>
</div>
<a href="cs4460Project.html">World Change Data</a>
</div>
<!-- Radio to turn annotations on or off. -->
<form action="" id="annotationsRadio" style="height: 35px;">
Annotations:
<input type="radio" name="radio" value="on" checked="checked">On</input>
<input type="radio" name="radio" value="off">Off</input>
<br>
<div style="display: inline-block; width: 12px; height: 12px; background: rgb(160, 160, 160); border-radius: 2px;"></div>
<p style="display: inline-block;">Gray indicates insufficient data</p>
</form>
<div style="padding-top:15px;"></div>
<!-- Div for cholopleth map -->
<div id="basic_choropleth"></div>
<!-- Slider -->
<div id="slider-container">
<div class="play_border" id="button" style="float:left; background-color: white;">
<div type="button" class="play_button"></div>
</div>
<div id="slider" style="width: 1340px; margin-left: 70px;"></div>
</div>
<!-- Clear button. -->
<button type="button" id="clearGraph" class="hidden">
CLEAR
</button>
<!-- Exit button -->
<button type="button" id="exitGraph" class="hidden">
×
</button>
<!-- Radio scale Y -->
<form action="" id="scaleYRadio" class="hidden">
Scale y axis on update?
<input type="radio" name="radio" value="yes">Yes</input>
<input type="radio" name="radio" value="no" checked="checked">No</input>
</form>
<br>
<!-- Country dropdown bar --->
<p style="float: left; margin-left: 20px;">Add countries (5 Max) via map or dropdown:</p>
<select name="countryList" id="dropDown">
<option value="All" id="defaultSelect">Select a country</option>
</select>
<!-- D3 and Javascript portion -->
<script>
//Hard-coded parameters for each dataset. Written from scratch.
var lifeDataDataset = {
name: "LifeData.csv",
initialMinDatasetYear: 1960,
initialMaxDatasetYear: 2012,
colorMin: -2.0,
colorMax: 2.0,
yPrefix: "",
ySuffix: "y",
yDivision: 1,
toFixed: 1,
}
var gdpDataset = {
name: "gdpPerCapita.csv",
initialMinDatasetYear: 1960,
initialMaxDatasetYear: 2013,
colorMin: -15.0,
colorMax: 15.0,
yPrefix: "$",
ySuffix: "k",
yDivision: 1000,
toFixed: 1,
}
var unemploymentDataset = {
name: "unemploymentPercentage.csv",
initialMinDatasetYear: 1991,
initialMaxDatasetYear: 2013,
colorMin: 15.0,
colorMax: -15.0,
yPrefix: "",
ySuffix: "%",
yDivision: 1,
toFixed: 1,
}
var populationDataset = {
name: "populationTotal.csv",
initialMinDatasetYear: 1960,
initialMaxDatasetYear: 2013,
colorMin: -5.0,
colorMax: 5.0,
yPrefix: "",
ySuffix: "mil",
yDivision: 1000000,
toFixed: 0,
}
var internetUsersDataset = {
name: "internetPer100.csv",
initialMinDatasetYear: 1990,
initialMaxDatasetYear: 2013,
colorMin: -30.0,
colorMax: 30.0,
yPrefix: "",
ySuffix: "%",
yDivision: 1,
toFixed: 2,
}
//Global variables to hold datasets
var allDatasets = [];
var currentDataset = "0";
//Checking status of radio buttons
var yScaleBoolean = false;
var annotationsBoolean = true;
allDatasets.push(lifeDataDataset);
allDatasets.push(gdpDataset);
allDatasets.push(unemploymentDataset);
allDatasets.push(populationDataset);
allDatasets.push(internetUsersDataset);
//Reset vis on drop down change. Written from scratch.
var dataDropDown = d3.select("#dataDropDown");
dataDropDown.on("change", menuChanged);
function menuChanged() {
//Resetting stuff:
d3.selectAll("#basic_choropleth").selectAll("*").remove();
$('#dropDown').val("All");
d3.select(".datamap").attr("style", "height: 600px;");
d3.select("#basic_choropleth").attr("style", "height: 600px;");
d3.select("#svg-pan-zoom-controls").attr("transform", "translate(1340 500) scale(0.75)");
d3.select("#exitGraph").classed("hidden", true);
d3.select("#clearGraph").classed("hidden", true);
d3.select("#scaleYRadio").classed("hidden", true);
d3.select("#dataGradient").attr("transform", "translate(40, 530)");
d3.selectAll(".countryGraph1").remove();
d3.select("#annotationsRadio").attr("style", "margin-top: 495px;");
currentDataset = d3.event.target.value;
console.log(currentDataset);
switchData(allDatasets[currentDataset]);
}
//Primary function that contains the vis, called each time the datasets are updated. Written from scratch.
var switchData = function(datasetObject) {
//Input the csv, calling it "dataset"
d3.csv(datasetObject.name, function(error, dataset) {
var initialMinDatasetYear = datasetObject.initialMinDatasetYear;
var initialMaxDatasetYear = datasetObject.initialMaxDatasetYear;
var minDatasetYear = initialMinDatasetYear;
var maxDatasetYear = initialMaxDatasetYear;
//Line chart width, height, and padding
var w = 1450;
var h = 280;
var padding = 50;
//Variable to keep track of the last country selected, and an array to populate the country dropdown
var currentCountry;
var countryName = [];
var xScale = d3.scale.linear();
var yScale = d3.scale.linear();
var xAxis = d3.svg.axis();
var yAxis = d3.svg.axis();
var yAxis2 = d3.svg.axis();
//Varible for when lines are created in the line chart.
var line = d3.svg.line()
.defined(function(d,i) { return d['value'] > 0; })
.x(function(d, i) { return xScale(+d.key); })
.y(function(d, i) { return yScale(+d['value']); });
//Create the JQRangeSlider. Options written from scratch.
$("#slider").rangeSlider({
bounds:{min: minDatasetYear, max: maxDatasetYear},
range: {min: 3, max: maxDatasetYear - minDatasetYear},
defaultValues:{min: minDatasetYear, max: maxDatasetYear}
});
//Update map, popups, and chart when slider is changing. Written from scratch.
$("#slider").on("valuesChanging", function(e, data){
//Stop animation (if button has been pressed)
clearInterval(interval);
animating = false;
minDatasetYear = Math.round(data.values.min);
maxDatasetYear = Math.round(data.values.max);
xScale.domain([minDatasetYear, maxDatasetYear]);
createChoropleth(minDatasetYear, maxDatasetYear);
//Annotations
checkForPopups();
//If there is a line chart already, then
if ($(".countryGraph1")[0]){
//This refreshes the line chart
countryClick(currentCountry, true);
}
});
//Update map, popups, and chart when slider is finished changing. Written from scratch.
$("#slider").on("valuesChanged", function(e, data){
minDatasetYear = Math.round(data.values.min);
maxDatasetYear = Math.round(data.values.max);
xScale.domain([minDatasetYear, maxDatasetYear]);
createChoropleth(minDatasetYear, maxDatasetYear);
//This adds annotations about the world
checkForPopups();
//If there is a line chart already, then
if ($(".countryGraph1")[0]){
//This refreshes the line chart
countryClick(currentCountry, true);
}
});
//Create a new object from the Datamap plugin called basic_choropleth (the variable and id have the same name).
//Datamaps plugin imported, options and details on demand written from scratch or heavily modified.
var basic_choropleth = new Datamap({
element: document.getElementById("basic_choropleth"),
geographyConfig: {
highlightOnHover: true,
highlightFillColor: "#E0E0FF",
highlightBorderColor: '#000000',
borderColor: '#606060',
popupTemplate: function(geography) {
//Details on demand settings
var tooltipOutput;
dataset.forEach(function(d, i){
if(geography.id.replace(" ", "") == d["Country Code"].replace(" ", "")) {
var lifeExpectMin = (d[minDatasetYear + ""]).toFixed(2);
var lifeExpectMax = (d[maxDatasetYear + ""]).toFixed(2);
var slope = ((((d[maxDatasetYear + ""] - d[minDatasetYear + ""])/d[maxDatasetYear + ""])/(maxDatasetYear-minDatasetYear))*100).toFixed(2);
if(isNaN(lifeExpectMin) || lifeExpectMin <= 0){
lifeExpectMin = "N/A";
}
if(isNaN(lifeExpectMax) || lifeExpectMax <= 0){
lifeExpectMax = "N/A";
}
if(isNaN(lifeExpectMin) || isNaN(lifeExpectMax)){
slope = "N/A";
}
tooltipOutput = '<div class="hoverinfo">'
+ geography.properties.name + ' ('+ geography .id + ')' + '<br>'
+ minDatasetYear + ' data value: ' + lifeExpectMin + '<br>'
+ maxDatasetYear + ' data value: ' + lifeExpectMax + '<br>'
+ 'Change per year ' + slope + "%";
}
});
return tooltipOutput;
},
highlightBorderWidth: 1
},
done: function(datamap) { //On country click
datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
currentCountry = geography.id;
if(geography.id != -99 && geography.id != "ESH" && geography.id != "TWN") { //-99 means that a country does not exist. ESH and TWN don't have data.
countryClick(currentCountry, true); //Boolean input "true" removes old graph, creates new line chart.
}
});
},
});
//Setting up the zoom and pan plugin. Imported and modified from zoompan.js demo code.
var zoomPanSetup = function(gutterW, gutterH) {
var beforePan;
beforePan = function(oldPan, newPan){
var stopHorizontal = false
, stopVertical = false
, gutterWidth = gutterW
, gutterHeight = gutterH
, sizes = this.getSizes()
, leftLimit = -((sizes.viewBox.x + sizes.viewBox.width) * sizes.realZoom) + gutterWidth
, rightLimit = sizes.width - gutterWidth - (sizes.viewBox.x * sizes.realZoom)
, topLimit = -((sizes.viewBox.y + sizes.viewBox.height) * sizes.realZoom) + gutterHeight
, bottomLimit = sizes.height - gutterHeight*(1.65) - (sizes.viewBox.y * sizes.realZoom)
customPan = {}
customPan.x = Math.max(leftLimit, Math.min(rightLimit, newPan.x))
customPan.y = Math.max(topLimit, Math.min(bottomLimit, newPan.y))
return customPan;
}
var mapsvg = document.querySelector(".datamap");
var panZoom = svgPanZoom(mapsvg, {
mouseWheelZoomEnabled: false,
dblClickZoomEnabled: false,
zoomEnabled: true,
controlIconsEnabled: true,
fit: true,
center: true,
zoomScaleSensitivity: 0.5,
minZoom: 1,
maxZoom: 3,
beforePan: beforePan
});
}
zoomPanSetup(1300,330); //Set up zoom and pan
//Go through that data values and tell D3 to treat them as numbers, not strings. Written from scratch.
dataset.forEach(function(d) {
for(var j = initialMinDatasetYear; j <= initialMaxDatasetYear; j++)
{
//Plus sign turns data values at years into numbers.
d[j + ""] = +d[j + ""]; //Turn year to string so that d3 understands it.
}
countryName.push([d["Country Name"], d["Country Code"]]); //Get country names and codes for the country dropdown bar.
});
//Set up the country drop down
var dropDown = d3.select("#dropDown");
var countrySort = countryName.sort();
var options = dropDown.selectAll("option")
.data(countrySort)
.enter()
.append("option")
.text(function (d, i) { return d[0] + ": " + d[1]; })
.attr("value", function (d, i) { return d[1]});
//When new country is selected.
dropDown.on("change", menuChanged);
function menuChanged() {
var selectedValue = d3.event.target.value; //Get value selected.
currentCountry = selectedValue;
countryClick(selectedValue, true); //Create line chart.
}
//Animation button for the time interval.
var animating = false; //initally set animating boolean to false.
var interval;
var myButton = d3.select("#button"); //Select the play button.
//On button clicked. Imported and heavily modified.
myButton.on("click", function(){
if(!animating) {
animating = true;
interval = setInterval(animateSlider, 300); //Interval for animation.
}
else {
clearInterval(interval);
animating = false;
}
});
//Function that runs when button is clicked.
var animateSlider = function(){
if(maxDatasetYear < initialMaxDatasetYear){
minDatasetYear++;
maxDatasetYear++;
$("#slider").rangeSlider("values", minDatasetYear, maxDatasetYear);
}
else{
clearInterval(interval);
animating = false;
}
}
//Create choropleth and gradient color scale, from red to green. Written from scratch.
var colorScale = d3.scale.linear()
.domain([datasetObject.colorMin, 0, datasetObject.colorMax])
.clamp(true)
.range(["#c35858", "white", "rgb(71, 126, 58)"]);
//Create the reference gradient in the left hand corner. Imported and heavily modified.
var createGradient = function(){
var gradient = d3.select("#basic_choropleth").selectAll("svg")
.append("svg:g")
.attr("id", "dataGradient")
.attr("width", "400")
.attr("height", "50")
.attr("transform", "translate(40, 530)"); //Put the gradient at the bottom of the map
//Define the gradient with a defs element
var defs = gradient.append("defs");
var linearGrad = defs.append("linearGradient")
.attr({
id: "grad1",
x1: "0%",
y1: "0%",
x2: "100%",
y2: "0%",
});
linearGrad.append("stop") //Tell where to start the gradient (min)
.attr({
offset: "0%",
style: "stop-color:" + colorScale(datasetObject.colorMin) + "; stop-opacity:1",
});
linearGrad.append("stop") //Tell where to start the gradient (min)
.attr({
offset: "50%",
style: "stop-color:" + colorScale(0) + "; stop-opacity:1",
});
linearGrad.append("stop") //Tell where to end the gradient (max)
.attr({
offset: "100%",
style: "stop-color:" + colorScale(datasetObject.colorMax)+ ";stop-opacity:1",
});
var svgGrad = gradient.append("rect") //Create the rectangle holding the gradient
.attr({
width: 300,
height: 30,
rx: 2,
ry: 2,
style: "fill:url(#grad1); stroke-width:1; stroke:rgb(60,60,60)" //Fill rectangle with the gradient created
});
gradient.append("text") //Append the text telling the min % change
.attr({
y: 20,
x: 5,
fill: "black",
})
.text((datasetObject.colorMin).toFixed(1) + " %/year");
gradient.append("text") //Append the text telling the max % change
.attr({
y: 20,
x: 215,
fill: "black",
})
.text((datasetObject.colorMax).toFixed(1) + " %/year");
} //Create gradient
createGradient();
//Update the choropleth with new slider values.
var createChoropleth = function() {
//Color the choropleth. Written from scratch.
dataset.forEach(function(d){
var newColor = {};
if(!isNaN(d[maxDatasetYear + ""]) && !isNaN(d[minDatasetYear + ""]) && d[maxDatasetYear + ""] > 0 && d[minDatasetYear + ""] > 0){
var yearDif = ((((d[maxDatasetYear + ""] - d[minDatasetYear + ""])/d[maxDatasetYear + ""])/(maxDatasetYear-minDatasetYear))*100).toFixed(2);
newColor[d["Country Code"]] = colorScale(yearDif);
}
else{
newColor[d["Country Code"]] = "rgb(160, 160, 160)";
}
basic_choropleth.updateChoropleth(newColor); //Update choropleth
});
}
createChoropleth();
//Set up xScale for line chart.
xScale = d3.scale.linear()
.domain([minDatasetYear, maxDatasetYear])
.range([padding, w - padding]);
var currentCountriesArray = [];
var lineColors = ["#3e3636", "#45b7bc", "#3d3d9f", "#cc79b6", "#c37f2f",];
//Long function to update the line chart when countries are clicked or selected. Written from scratch.
var countryClick = function(countryIn, newCountryBoolean) {
var countryIndex = [];
if(currentCountriesArray.indexOf(countryIn) === -1) { //Only update line chart if country has not been clicked already.
if(currentCountriesArray.length < 5){
currentCountriesArray.push(countryIn);
}
else {
var lineColorReturn = lineColors.pop();
lineColors.unshift(lineColorReturn);
console.log(lineColors);
currentCountriesArray.unshift(countryIn);
currentCountriesArray.length = 5;
}
}
if(currentCountry == undefined){
return;
}
//Make the line chart appear to "pop up." Written from scratch.
d3.select(".datamap").attr("style", "height: 350px;");
d3.select("#basic_choropleth").attr("style", "height: 350px;");
d3.select("#svg-pan-zoom-controls").attr("transform", "translate(1340 250) scale(0.75)");
d3.select("#exitGraph").classed("hidden", false);
d3.select("#clearGraph").classed("hidden", false);
d3.select("#scaleYRadio").classed("hidden", false);
d3.select("#dataGradient").attr("transform", "translate(40, 285)");
d3.select("#annotationsRadio").attr("style", "margin-top: 255px;");
if(newCountryBoolean == true) {
d3.selectAll(".countryGraph1").remove(); //If true is passed in, remove the graph (class countryGraph1)
}
$('#dropDown').val(countryIn); //Set drop down to selection
//Create a new canvas svg element, with class of countryGraph1
var canvas = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("class", "countryGraph1")
.attr("style", "margin: 0 auto;");
//Find the indices for each country that are on the line chart (based on the country name)
dataset.forEach(function(d, i) {
if(d["Country Code"] == currentCountriesArray[0]){
countryIndex[0] = i;
}
else if(d["Country Code"] == currentCountriesArray[1]){
countryIndex[1] = i;
}
else if(d["Country Code"] == currentCountriesArray[2]){
countryIndex[2] = i;
}
else if(d["Country Code"] == currentCountriesArray[3]){
countryIndex[3] = i;
}
else if(d["Country Code"] == currentCountriesArray[4]){
countryIndex[4] = i;
}
});
//Find min and max values for the line chart.
var minValue = 10000000000; //Put in large value as initial min reference.
var maxValue = 0; //Small values as initial max reference.
var initialYear;
var finalYear;
if(yScaleBoolean) {
initialYear = minDatasetYear;
finalYear = maxDatasetYear;
}else {
initialYear = initialMinDatasetYear;
finalYear = initialMaxDatasetYear
}
for(var i = initialYear; i <= finalYear; i++) {
for(var j = 0; j < currentCountriesArray.length; j++) {
//Find min
if(dataset[countryIndex[j]][i + ""] > 0 && dataset[countryIndex[j]][i + ""] < minValue) {
minValue = dataset[countryIndex[j]][i + ""];
}
//Find max
if(dataset[countryIndex[j]][i + ""] > 0 && dataset[countryIndex[j]][i + ""] > maxValue) {
maxValue = dataset[countryIndex[j]][i + ""];
}
}
}
/*
//Set the yScale based on the datapoints
for(var j = 0; j < currentCountriesArray.length; j++) {
if(d3.max(d3.values(dataset[countryIndex[j]])) > maxValue) {
maxValue = d3.max(d3.values(dataset[countryIndex[j]]));
}
}
*/
yScale = d3.scale.linear()
.domain([minValue - 0.01*minValue, maxValue])
.range([h - padding, padding]); //Range is the size of the chart
//xAxis using year xScale. Imported and modified.
xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.tickFormat(d3.format("d")); //decimal ticks (rounded)
//Append a g element containing xAxis. Imported and modified.
canvas.append("g")
.attr("class", "axis")
.attr("id", "xAxis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis); //Took our "g" group elements and put the xAxis on them. Note that we could put the xAxis code in.
//yAxis.
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(10)
.tickFormat(function(d) {return allDatasets[currentDataset].yPrefix
+ (d/allDatasets[currentDataset].yDivision).toFixed(allDatasets[currentDataset].toFixed)
+ allDatasets[currentDataset].ySuffix;});
//.tickFormat(d3.format(".1f"));
//Append yAxis. Imported and modified.
canvas.append("g")
.attr("class", "axis")
.attr("id", "yAxis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);
//Create a line for each country in the array of countries that have been clicked. Written from scratch.
for(var i = 0; i < currentCountriesArray.length; i++) {
if(dataset[countryIndex[i]][minDatasetYear + ""] > 0 && dataset[countryIndex[i]][maxDatasetYear + ""] > 0){
slope = ((((dataset[countryIndex[i]][maxDatasetYear + ""] - dataset[countryIndex[i]][minDatasetYear + ""])
/dataset[countryIndex[i]][maxDatasetYear + ""])/(maxDatasetYear-minDatasetYear))*100).toFixed(2);
} else {
slope = undefined;
}
if (!isNaN(slope) && slope !== 0) {
canvas.append("text")
.attr("y", 20)//yScale(dataset[countryIndex[i]][maxDatasetYear + ""]))
.attr("x", 120 + 110*i)
.text(dataset[countryIndex[i]]["Country Code"] + ": " + slope + " %/y")
//.text(dataset[dataIndex]["Country Name"] + ", Slope = " + slope.toFixed(2))
.attr("fill", "black")
.attr("id", "lineText" + i)
.attr("class", "slopeText");
}
else {
canvas.append("text")
.attr("fill", "black")
.attr("y", 20)//yScale(dataset[countryIndex[i]][maxDatasetYear + ""]))
.attr("x", 120 + 110*i) //1300)
.text(dataset[countryIndex[i]]["Country Code"] + ": N/A %/y")
//.text(dataset[dataIndex]["Country Name"] + ", Slope = N/A")
.attr("id", "lineText" + i)
.attr("class", "slopeText")
.attr("fill", "black");
}
canvas.append("rect")
.attr("x", 120 + 110*i)
.attr("y", 25)
.attr("rx", 2)
.attr("ry", 2)
.attr("width", document.getElementById("lineText" + i).clientWidth)
.attr("height", 5)
.attr("class", "slopeText")
.attr("fill", lineColors[i]);
var entries = d3.entries(dataset[countryIndex[i]]);
entries = entries.filter(function(element){
return(!isNaN(+element.key) && minDatasetYear <= +element.key && +element.key <= maxDatasetYear);
});
canvas.append("path")
.datum(entries)
.attr("class","line")
.attr("id","chartLine" + i)
.attr("style", "stroke: " + lineColors[i])
.attr("d", line);
}
} //countryClick()
//X button for graphs. Written from scratch.
var exitButton = d3.select("#exitGraph");
exitButton.on("click", function(){
$('#dropDown').val("All");
d3.select(".datamap").attr("style", "height: 600px;");
d3.select("#basic_choropleth").attr("style", "height: 600px;");
d3.select("#svg-pan-zoom-controls").attr("transform", "translate(1340 500) scale(0.75)");
d3.select("#exitGraph").classed("hidden", true);
d3.select("#clearGraph").classed("hidden", true);
d3.select("#scaleYRadio").classed("hidden", true);
d3.select("#dataGradient").attr("transform", "translate(40, 530)");
d3.select("#annotationsRadio").attr("style", "margin-top: 495px;");
d3.select(".countryGraph1").remove();
currentCountriesArray = [];
});
//Clear button for graphs. Written from scratch.
var exitButton = d3.select("#clearGraph");
exitButton.on("click", function(){
currentCountriesArray = [];
for(var i = 0; i < 5; i++) {
d3.select("#chartLine" + i).remove();
d3.selectAll(".slopeText").remove();
}
});
//Radio for scale graph. Written from scratch.
$('#scaleYRadio input').change(function () {
if($(this).val() === "yes"){
yScaleBoolean = true;
$("#slider").rangeSlider("values", minDatasetYear+1, maxDatasetYear-1);
$("#slider").rangeSlider("values", minDatasetYear, maxDatasetYear);
}
else{
yScaleBoolean = false;
$("#slider").rangeSlider("values", minDatasetYear+1, maxDatasetYear-1);
$("#slider").rangeSlider("values", minDatasetYear, maxDatasetYear);
}
});
//Radio for annotations:
$('#annotationsRadio input').change(function () {
if($(this).val() === "on"){
annotationsBoolean = true;
checkForPopups();
}
else{
d3.selectAll(".svg-tooltip").remove();
annotationsBoolean = false;
}
});
//Create a popup annotation. Imported and heavily modifed.
var createPopup = function(textInput, tooltipClass, width, xLocation, yLocation, xPoint, yPoint) {
//Offset so that "Page Ruler" chrome extension can give x and y coordinates (to speed up hard-coding of x and y parameters).
xLocation -= 21;
yLocation -= 24;
xPoint -= 21;
yPoint -= 24;
var mapSvg = d3.select(".svg-pan-zoom_viewport");
mapSvg.append("line")
.attr({
x1: xLocation + (0.5*width),
y1: yLocation+10,
x2: xPoint,
y2: yPoint,
style: "stroke:rgb(120,120,120); stroke-width:1;",
class: "svg-tooltip " + tooltipClass,
});