-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_lines.html
954 lines (859 loc) · 28.7 KB
/
index_lines.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
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
<!DOCTYPE html>
<meta charset="utf-8">
<title> Global Warming Data </title>
<head>
<!-- Bootstrap theme stuff -->
<!-- Latest compiled and minified CSS -->
<!--link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"-->
<!-- Optional theme -->
<!--link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"-->
<link rel="stylesheet" type="text/css" href="menu1.css"/>
<style type="text/css">
.axis path,
.axis line {
fill: none;
stroke: grey;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
opacity: 0.7;
stroke-width: 2.5px;
}
.line.hidden {
stroke: grey;
opacity: 0.4;
}
.mousedover {
stroke: orange;
opacity: 1.0;
}
.basic-countries {
font-family: sans-serif;
opacity: 0.5;
font-size: 11px;
fill: #444;
}
.highlighted-countries {
font-family: sans-serif;
opacity: 0.8;
font-size: 11px;
font-weight: bold;
fill: #444;
}
.extent {
fill: #000;
fill-opacity: .25;
stroke: #fff;
}
body {
margin: 40px auto;
max-width: 750px;
font-size: 16px;
line-height: 1.6;
color: #444;
padding: 0 10px;
}
h1,h2,h3 {
line-height: 1.2;
}
.btn {
width:80px;
height:30px;
}
.x-label {
font-size: 18px;
}
.y-label {
font-size: 18px;
}
</style>
</head>
<body>
<div id="menu">
<ul>
<li><a href="#">Select Chart</a>
<ul>
<li><a href="co2emission.html">CO2 Emission</a></li>
<li><a href="index.html">CO2 Intensity</a></li>
</ul></li>
</ul>
</div>
<h2> CO2 Emission across Countries from 1960 to 2008 </h2>
Y Variable:
<select id="y-variable">
<option value="Tech">Intensity (Carbon / GDP)</option>
<option value="Year">Year</option>
<option value="Population">Population</option>
<option value="GDP">GDP per capita</option>
<option value="CO2">Carbon emissions</option>
<option value="CO2Total">Carbon emissions (total)</option>
</select>
Y on log scale? <input id="y-log-scale" type="checkbox" value="log-scale">
<br/>
X Variable:
<select id="x-variable">
<option value="Year">Year</option>
<option value="Tech">Intensity (Carbon / GDP)</option>
<option value="Population">Population</option>
<option value="GDP">GDP per capita</option>
<option value="CO2">Carbon emissions</option>
<option value="CO2Total">Carbon emissions (total)</option>
</select>
X on log scale? <input id="x-log-scale" type="checkbox" value="log-scale">
<br/>
<button class="btn btn-default" type="button" id="swap-axes">Swap axes</button>
<button class="btn btn-default" type="button" id="reset">Reset</button>
<button class="btn btn-default" type="button" id="subset-countries" disabled style="width: 180px">Subset to selected countries</button>
<br/>
Region:
<select id="region">
<option value="Summarize-region">Region summaries</option>
<option value="All">Entire World</option>
<option value=" Southeast Asia">Southeast Asia</option>
<option value=" Eastern Europe">Eastern Europe</option>
<option value=" Africa">Africa</option>
<option value=" Western Europe">Western Europe</option>
<option value=" Latin America">Latin America</option>
<option value=" Former Soviet Union">Former Soviet Union</option>
<option value=" Australia_NZ">Australia and NZ</option>
<option value=" Middle East">Middle East</option>
<option value=" China">China</option>
<option value=" Canada">Canada</option>
<option value=" India">India</option>
<option value=" Japan">Japan</option>
<option value=" USA">USA</option>
<option value=" Korea">Korea</option>
</select>
<br/>
<br/>
<div id="container1" style="position: relative; width: 80%; max-height: 450px;"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script type="text/javascript"></script>
<script>
/*
TODO: Carbon emissions are by person. Change it!!
TODO: add in other variables
TODO: add colors by region, If subsetted to only a few countries, display lines by color (fewer than 10??)
*/
// TODO: Allow selection of region when subsetted countries.
/*
A lot of code is taken from "Interactive Data Visualization for the Web"
by Scott Murray.
Also http://bost.ocks.org/mike/nations/ and
http://bl.ocks.org/mbostock/3883245
*/
var w = 700;
var h = 500;
var padding_h = 50;
var padding_left = 70;
var padding_right = 150;
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
// TODO: fix these numbers
var margin2 = {top: 40, right: 10, bottom: 20, left: 40};
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
// From https://stackoverflow.com/questions/14167863/how-can-i-bring-a-circle-to-the-front-with-d3
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
var make_lines = function(varx, vary) {
svg.selectAll("circle").remove();
d3.select("#subset-countries")[0][0].disabled = true;
if (region_selected === "Subset-countries") {
dataset = []
for (var i = 0; i < selectedCountries.length; i++) {
dataset.push(countryData[selectedCountries[i]]);
}
} else if (region_selected === "All") {
dataset = countryArray;
selectedCountries = [];
} else if (region_selected === "Summarize-region") {
dataset = regionSummaryArray;
selectedCountries = [];
} else {
dataset = regionArray[region_selected];
selectedCountries = [];
}
var year_format = d3.format("d");
var xdomain = [];
var ydomain = [];
if (varx === "Year") {
xdomain = [1960, 2010];
} else {
if (logx) {
xdomain = [d3.min(dataset,function(d) {
var min_tmp = d[0][varx];
for (var i = 0; i < d.length; i++) {
min_tmp = d3.min([min_tmp, d[i][varx]]);
}
return min_tmp;
}),
d3.max(dataset, function(d) {
var max_tmp = 0;
for (var i = 0; i < d.length; i++) {
max_tmp = d3.max([max_tmp, d[i][varx]]);
}
return max_tmp;
})];
} else {
xdomain = [0, 1.04*d3.max(dataset, function(d) {
var max_tmp = 0;
for (var i = 0; i < d.length; i++) {
max_tmp = d3.max([max_tmp, d[i][varx]]);
}
return max_tmp;
})];
}
}
if (vary === "Year") {
ydomain = [1960, 2010];
} else {
if (logy) {
ydomain = [d3.min(dataset,function(d) {
var min_tmp = d[0][vary];
for (var i = 0; i < d.length; i++) {
min_tmp = d3.min([min_tmp, d[i][vary]]);
}
return min_tmp;
}),
d3.max(dataset, function(d) {
var max_tmp = 0;
for (var i = 0; i < d.length; i++) {
max_tmp = d3.max([max_tmp, d[i][vary]]);
}
return max_tmp;
})];
} else {
ydomain = [0, 1.04*d3.max(dataset, function(d) {
var max_tmp = 0;
for (var i = 0; i < d.length; i++) {
max_tmp = d3.max([max_tmp, d[i][vary]]);
}
return max_tmp;
})];
}
}
if (logx) {
var x = d3.scale.log()
.domain(xdomain)
.range([padding_left, w - padding_right]);
} else {
var x = d3.scale.linear()
.domain(xdomain)
.range([padding_left, w - padding_right]);
}
if (logy) {
var y = d3.scale.log()
.domain(ydomain)
.range([h - padding_h, padding_h/2]);
} else {
var y = d3.scale.linear()
.domain(ydomain)
.range([h - padding_h, padding_h/2]);
}
var line = d3.svg.line()
.defined(function(d) { return (!isNaN(d[vary])) && (!isNaN(d[varx])); })
.x(function(d) { return x(d[varx]); })
.y(function(d) { return y(d[vary]); });
if (logx) {
var xScale = d3.scale.log()
.domain(xdomain)
.range([padding_left, w - padding_right]);
} else {
var xScale = d3.scale.linear()
.domain(xdomain)
.range([padding_left, w - padding_right]);
}
if (logy) {
var yScale = d3.scale.log()
.domain(ydomain)
.range([h - padding_h, padding_h/2]);
} else {
var yScale = d3.scale.linear()
.domain(ydomain)
.range([h - padding_h, padding_h/2]);
}
if (varx === "Year") {
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.tickFormat(d3.format("d"));
} else {
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
}
if (vary === "Year") {
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.tickFormat(d3.format("d"));
} else {
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
}
// Clean up before adding new axes:
svg.selectAll("g")
.remove();
svg.selectAll("text").attr("class", "x-label").remove();
svg.selectAll("text").attr("class", "y-label").remove();
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding_h) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding_left + ",0)")
.call(yAxis);
var vartonames = function(varname) {
if (varname === "GDP") {
return "GDP per capita (USD)"
} else if (varname === "Tech") {
return "Carbon Intensity (tonnes carbon / $10,000)"
} else if (varname === "Population") {
return "Population (1,000s)"
} else if (varname === "CO2") {
return "Carbon Emissions per capita (tonnes carbon)" // TODO: change!!!
} else if (varname === "CO2Total") {
return "Carbon Emissions (megatonnes carbon)" // TODO: change!!!
} else {
return varname
}
};
if (varx === "Year") {
for (var i = 0; i < dataset.length; i++) {
var xPosition = x(2010.5);
var yPosition = y(dataset[i][dataset[i].length-1][vary]) + 3;
if (isNaN(yPosition)) {
// Use 2008 data
yPosition = y(dataset[i][dataset[i].length-3][vary]) + 3;
}
var tooltip_id = "tooltip" + dataset[i][0].Country;
tooltip_id = tooltip_id.replace(/ /g, '') // Strip spaces
.replace(/\./g, ''); // Strip period
svg.append("text")
//.attr("id", "tooltip")
.attr("id", tooltip_id)
.attr("x", xPosition)
.attr("y", yPosition)
.attr("text-anchor", "start")
.attr("class", "country-names")
.attr("class", "basic-countries")
/*
.attr("opacity", 0.6)
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("font-weight", "bold")
.attr("fill", "#444")
*/
.text(dataset[i][0].Country);
}
}
// add x-axis label:
svg.append("text")
.attr("class", "x-label")
.attr("text-anchor", "middle")
.attr("x", w/2)
.attr("y", h - padding_h/4)
.text(vartonames(varx));
// add y-axis label:
svg.append("text")
.attr("class", "y-label")
.attr("text-anchor", "middle")
.attr("y", 5)
.attr("x", -h/2 + padding_h)
.attr("dy", ".5em")
.attr("transform", "rotate(-90)")
.text(vartonames(vary));
svg.selectAll(".line")
.remove();
if (varx !== "Year" && vary !== "Year") {
svg.selectAll("circle")
.remove();
// create small dataset
start_dataset = [];
for (var i = 0; i < dataset.length; i++) {
var indx = 0;
// while either variable is missing, skip to next year
// 1973 is to handle a special case with Former USSR data
while (isNaN(dataset[i][indx][varx]) ||
isNaN(dataset[i][indx][vary]) ||
dataset[i][indx]["Year"] === 1973) {
indx++;
if (indx === dataset[i].length) {
break;
}
}
var country_start_tmp = {};
country_start_tmp[varx] = dataset[i][indx][varx];
country_start_tmp[vary] = dataset[i][indx][vary];
start_dataset.push(country_start_tmp);
}
end_dataset = [];
for (var i = 0; i < dataset.length; i++) {
var indx = dataset[i].length - 1;
// while either variable is missing, skip to next year
while (isNaN(dataset[i][indx][varx]) ||
isNaN(dataset[i][indx][vary])) {
indx--;
if (indx === 0) {
break;
}
}
var country_end_tmp = {};
country_end_tmp[varx] = dataset[i][indx][varx];
country_end_tmp[vary] = dataset[i][indx][vary];
country_end_tmp["Year"] = dataset[i][indx]["Year"];
end_dataset.push(country_end_tmp);
}
svg.selectAll("circle.start")
.data(start_dataset)
.enter()
.append("circle")
.attr({
cx: function(d) {
return xScale(d[varx]);
},
cy: function(d) {
return yScale(d[vary]);
},
r: 3,
fill: "#f1a340"
});
svg.selectAll("circle.end")
.data(end_dataset)
.enter()
.append("circle")
.attr({
cx: function(d) {
return xScale(d[varx]);
},
cy: function(d) {
return yScale(d[vary]);
},
r: 6,
fill: "#998ec3"
});
svg.selectAll("text")
.data(end_dataset)
.enter()
for (var i = 0; i < end_dataset.length; i++) {
var d = end_dataset[i];
console.log(d);
svg.append("text")
.attr({
x: xScale(d[varx]),
y: yScale(d[vary])
})
.attr("text-anchor", "start")
.attr("font-family", "sans-serif")
.attr("opacity", "0.55")
.attr("font-size", "11px")
.attr("fill", "black")
.text(d.Year);
}
}
// Brush taken from http://bl.ocks.org/mbostock/4063663
var brush = d3.svg.brush()
.x(xScale)
.y(yScale)
.on("brushstart", brushstart)
.on("brush", brushmove)
.on("brushend", brushend);
// svg.call(brush);
svg.append("g")
.attr("class", "brush")
.call(brush);
// Clear the previously-active brush, if any.
function brushstart() {
//console.log("brush starting!")
selectedCountries = [];
svg.selectAll(".highlighted-text")
.classed("basic-countries", true)
.classed("highlighted-text", false);
svg.selectAll("path:not(.hidden)").classed("hidden", true);
brush.clear()
}
// Highlight the selected lines
function brushmove(p) {
var e = brush.extent();
svg.selectAll("path").classed("hidden", function(d) {
// If any point of the line d falls in the box, classify it!!
for (var i = 0, linelen = d.length; i < linelen; i++) {
if (e[0][0] < d[i][varx] && d[i][varx] < e[1][0]
&& e[0][1] < d[i][vary] && d[i][vary] < e[1][1]) {
if (varx === "Year") {
var tooltip_id = "#tooltip" + d[0].Country;
tooltip_id = tooltip_id.replace(/ /g, '') // Strip spaces
.replace(/\./g, ''); // Strip period
svg.selectAll(tooltip_id)
.classed("basic-countries", false)
.classed("highlighted-text", true);
}
return false;
}
}
if (varx === "Year" && d.constructor === Array) {
var tooltip_id = "#tooltip" + d[0].Country;
tooltip_id = tooltip_id.replace(/ /g, '') // Strip spaces
.replace(/\./g, ''); // Strip period
svg.selectAll(tooltip_id)
.classed("basic-countries", true)
.classed("highlighted-text", false);
}
return true;
});
}
// If the brush is empty, as in it takes no volume, select all lines
function brushend() {
var selectedLines = svg.selectAll("path:not(.hidden)").data();
selectedCountries = [];
for (var i = 0; i < selectedLines.length; i++) {
if (selectedLines[i].constructor === Array) {
selectedCountries.push(selectedLines[i][0]["Iso"]);
}
}
if (selectedCountries.length > 0 &&
region_selected !== "Summarize-region") {
d3.select("#subset-countries")[0][0].disabled = false;
} else if (selectedCountries.length > 0) {
d3.select("#subset-countries")[0][0].disabled = true;
} else {
svg.selectAll(".highlighted-text")
.classed("basic-countries", true)
.classed("highlighted-text", false);
d3.select("#subset-countries")[0][0].disabled = true;
}
if (brush.empty()) svg.selectAll(".hidden").classed("hidden", false);
}
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", h+ 7);
// TODO: add color rainbow if not many lines!!
svg.selectAll(".line")
.data(dataset)
.enter()
.append("path")
.attr("class", "line")
.attr("d", line)
.attr("id", function(d) {
return d[0]["Iso"]
})
.on("mouseover", function(d) {
//console.log("moused over");
var xPosition = w/2;
var yPosition = 20;
d3.select(this)
.moveToFront()
.classed("mousedover", true);
if (varx === "Year") {
var tooltip_id = "#tooltip" + d[0].Country;
tooltip_id = tooltip_id.replace(/ /g, '') // Strip spaces
.replace(/\./g, ''); // Strip period
svg.selectAll(tooltip_id)
.classed("basic-countries", false)
.classed("highlighted-text", true);
}
svg.append("text")
.attr("id", "tooltip-hover")
.attr("x", xPosition)
.attr("y", yPosition)
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.attr("font-weight", "bold")
.attr("fill", "orange")
.text("Country: " + d[0].Country);
})
.on("mousedown", function() {
// So that brushing works when you're on top of a line
// Based on https://wrobstory.github.io/2013/11/D3-brush-and-tooltip.html
//console.log("mousedown");
brush_elm = svg.select(".brush").node();
new_click_event = new Event('mousedown');
new_click_event.pageX = d3.event.pageX;
new_click_event.clientX = d3.event.clientX;
new_click_event.pageY = d3.event.pageY;
new_click_event.clientY = d3.event.clientY;
brush_elm.dispatchEvent(new_click_event);
})
.on("mouseout", function(d) {
if (varx === "Year") {
var tooltip_id = "#tooltip" + d[0].Country;
tooltip_id = tooltip_id.replace(/ /g, '') // Strip spaces
.replace(/\./g, ''); // Strip period
svg.selectAll(tooltip_id)
.classed("basic-countries", true)
.classed("highlighted-text", false);
}
//console.log("mouseout detected");
d3.select(this)
.classed("mousedover", false);
d3.select("#tooltip-hover").remove();
});
};
// TODO: stop mutating the [[Prototype]] of an object??
// initialize in case the page is reloaded when it's enabled
d3.select("#subset-countries")[0][0].disabled = true;
var countryData = {
USA: []
};
// regionSummaryData and regionSummaryArray is data for regions, in the same way that we
// have countryData and countryArray
var regionSummaryData = {
};
var regionSummaryArray = [];
var regionsMap = {
};
var regionArray = {
};
var load_data = function(Isocode) {
d3.csv("gcam_region_mapping.csv", function(error, data) {
data.forEach(function(d) {
if (!(d.Region in regionsMap)) {
regionsMap[d.Region] = [d.Isocode];
} else {
regionsMap[d.Region].push(d.Isocode);
}
});
});
d3.csv("region_summary.csv", function(error, data) {
data
.forEach(function(d) {
if (!(d.Region in regionSummaryData)) {
regionSummaryData[d.Region] = [];
}
// create data for map
// TODO: change fullData!!!
regionSummaryData[d.Region].push({
Region: d.Region,
Iso: d.Region, // Uses code for plotting countries without modification
Country: d.Region, // Uses code for plotting countries without modification
Year: +d.Year,
Tech: +d.Tech,
CO2Total: +d.CarbonTotal / Math.pow(10,6),
GDPTotal: +d.GDPTotal,
CO2: +d.CO2,
GDP: +d.GDP,
Population: +d.PopTotal
});
});
for (reg in regionSummaryData) {
regionSummaryArray.push(regionSummaryData[reg]);
}
});
d3.csv("data_co2.csv", function(error, data) {
data
.forEach(function(d) {
// create data for map
fullData[d.Year][d.Isocode] = {
Iso: d.Isocode,
// fillKey: get_fill(+d.Tech),
Year: +d.Year,
Tech: +d.Tech,
GDP: +d.GDP,
Pop: +d.PopTotal
};
if (!(d.Isocode in countryData)) {
countryData[d.Isocode] = [];
}
countryData[d.Isocode].push({
Iso: d.Isocode,
Country: d.Country,
Year: +d.Year,
Tech: +d.Tech,
GDP: +d.GDP,
Population: +d.PopTotal,
CO2: +d.CO2,
CO2Total: d.CO2 * (1000*d.PopTotal) / Math.pow(10, 6)
});
});
//make_scatter(fullData[Year]);
for (var region in regionsMap) {
regionArray[region] = [];
}
function include(arr,obj) {
return (arr.indexOf(obj) != -1);
}
countryArray = [];
for (iso in countryData) {
for (var region in regionsMap) {
// Inefficient way of making this object right now
if (include(regionsMap[region], iso)) {
regionArray[region].push(countryData[iso]);
}
}
countryArray.push(countryData[iso]);
}
//make_lines(countryArray, varx, vary)
make_lines(varx, vary)
});
};
var varx = d3.select("#x-variable")
.property("value");
var vary = d3.select("#y-variable")
.property("value");
var logx = d3.select("#x-log-scale")
.property("checked");
var logy = d3.select("#y-log-scale")
.property("checked");
var region_selected = d3.select("#region")
.property("value");
//load_data(1995);
load_data("USA");
d3.select("#x-variable")
.on("change", function() {
varx = d3.select(this).property("value");
//make_lines(countryArray, varx, vary);
make_lines(varx, vary);
});
d3.select("#y-variable")
.on("change", function() {
vary = d3.select(this).property("value");
//make_lines(countryArray, varx, vary);
make_lines(varx, vary);
});
// TODO: create multiple buttons for log scale which are mutually exclusive, or something
d3.select("#x-log-scale")
.on("change", function() {
console.log("logx change detected");
logx = d3.select(this).property("checked");
//make_lines(countryArray, varx, vary);
make_lines(varx, vary);
});
d3.select("#y-log-scale")
.on("change", function() {
console.log("logy change detected");
logy = d3.select(this).property("checked");
//make_lines(countryArray, varx, vary);
make_lines(varx, vary);
});
d3.select("#region")
.on("change", function() {
console.log("region change detected");
region_selected = d3.select(this).property("value");
make_lines(varx, vary);
});
//<button type="button" id="swap-axes">Swap axes</button>
d3.select("#swap-axes")
.on("click", function() {
console.log("Swap axes button clicked");
var var_tmp = varx;
varx = vary;
vary = var_tmp;
var log_tmp = logx;
logx = logy;
logy = log_tmp;
// swap variables in drop-down menus
xvar_tmp = document.getElementById("x-variable").value;
document.getElementById("x-variable").value =
document.getElementById("y-variable").value;
document.getElementById("y-variable").value = xvar_tmp;
// swap checks for x and y log scale checkboxes
xlogscale_tmp = document.getElementById("x-log-scale").checked;
document.getElementById("x-log-scale").checked =
document.getElementById("y-log-scale").checked;
document.getElementById("y-log-scale").checked = xlogscale_tmp;
//make_lines(countryArray, varx, vary);
make_lines(varx, vary);
});
//<button class="btn btn-default" type="button" id="reset">Reset</button>
d3.select("#reset")
.on("click", function() {
console.log("Reset button clicked");
varx = "Year";
vary = "Tech";
logx = false;
logy = false;
// region_selected = "All";
region_selected = "Summarize-region";
selectedCountries = [];
d3.select("#subset-countries")[0][0].disabled = true;
document.getElementById("x-variable").value = "Year";
// document.getElementById("region").value = "All";
document.getElementById("region").value = "Summarize-region";
document.getElementById("y-variable").value = "Tech";
document.getElementById("x-log-scale").checked = false;
document.getElementById("y-log-scale").checked = false;
make_lines(varx, vary);
});
// TODO: an undo selection button?
var selectedCountries = [];
d3.select("#subset-countries")
.on("click", function() {
console.log("Subset countries button pressed!");
if (selectedCountries.length > 0) {
region_selected = "Subset-countries";
make_lines(varx, vary);
} else {
console.log("Not subsetting because no countries selected");
}
});
var fullData = {
1960: {},
1961: {},
1962: {},
1963: {},
1964: {},
1965: {},
1966: {},
1967: {},
1968: {},
1969: {},
1970: {},
1971: {},
1972: {},
1973: {},
1974: {},
1975: {},
1976: {},
1977: {},
1978: {},
1979: {},
1980: {},
1981: {},
1982: {},
1983: {},
1984: {},
1985: {},
1986: {},
1987: {},
1988: {},
1989: {},
1990: {},
1991: {},
1992: {},
1993: {},
1994: {},
1995: {},
1996: {},
1997: {},
1998: {},
1999: {},
2000: {},
2001: {},
2002: {},
2003: {},
2004: {},
2005: {},
2006: {},
2007: {},
2008: {},
2009: {},
2010: {}
};
</script>
</body>