-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathco2emission.html
506 lines (443 loc) · 14 KB
/
co2emission.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
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="main.css"/>
<link rel="stylesheet" type="text/css" href="menu.css"/>
<link rel="stylesheet" href="slider.css"/>
</head>
<title> CO2 Emission Data </title>
<style>
#clock {
position: relative;
top: 5px;
left: 30px;
}
</style>
<body>
<div id="menu">
<ul>
<li><a href="#">Select Chart</a>
<ul>
<li><a href="index.html">CO2 Intensity</a></li>
<li><a href="index_lines.html">Line Chart</a></li>
</ul></li>
</ul>
</div>
<div id="container">
<h1> CO2 Emission</h1>
<h2> CO2 Emission across Countries from 1960 to 2008 </h2>
<p id="intro"> This is a map of CO2 emission (in tonnes of CO2 emissions per person by country by year from 1960 to 2008. CO2 intensity is a measure of carbon emissions that is more consistent across countries of varying levels of wealth than CO2 emissions per person.</p>
<p id="intro">Move the slider to select a year for viewing emissions. The light grey color represents missing data.</p>
<p id="intro">Demographic variables relevant to CO2 emissions, such as GDP and population size, are shown when hovering over a country.</p>
</div>
<img src="legend2.png" style="position: absolute; left: 1200px; top: 400px; width:130px; height:180px;">
<div id="date">
<p> CO2 Emission for Year: <p>
<span id="clock" style="color:#f6931f; font-weight:bold; font-size:24px" >1960</span>
</div>
<button id = "play" > play </button>
<div id = "slider-container">
<div id = "slider-div"></div>
<div id="slider-probe">
<p></p>
<div></div>
</div>
<!-- <span id = "clock">year</span> --!>
<!--<input id="slider",class="slider" type="range" min="1960" max="2008" value="1960" step="1" herf="slider.css" />-->
</div>
<!--<div id="slider-div"></div>-->
<div id="container1" style="position: relative; left: 50px; width: 80%; height: 50%"></div>
<!--<div id="wrapper">
<button id = "play">play</button>
</div> -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<!-- <script src="http://datamaps.github.io/scripts/datamaps.world.min.js?v=1"></script>-->
<script src="http://datamaps.github.io/scripts/datamaps.world.min.js"></script>
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script>
<!--<script src="https://raw.github.com/fryn/html5slider/master/html5slider.js"></script>-->
<script src="d3.slider.js"></script>
<!--<script type="text/javascript"></script>-->
<!--<script>
onload = function() {
var $ = function(id) { return document.getElementById(id); };
$('slider').oninput = function() { $('clock').innerHTML = this.value; };
$('slider').oninput();
};
</script>-->
<p>CO2 emissions data comes from the <a href="http://www.globalcarbonproject.org/carbonbudget/index.htm">Global Carbon Budget</a>. GDP Data comes from the <a href="http://www.ggdc.net/maddison/maddison-project/home.htm">Maddison Project</a>. Population data comes from the <a href="http://esa.un.org/wpp/">UN World Population Prospects, 2012 edition</a>.</p>
<script>
var width = 960,
height = 600;
var format = d3.format(",");
//animateMap();
var currentYear = 1960, playing = false;
var
color_qmissing = '#ede4e4'
color_q0 = 'rgb(198,219,239)',
color_q1 = 'rgb(158,202,225)',
color_q2 = 'rgb(107,174,214)',
color_q3 = 'rgb(66,146,198)',
color_q4 = 'rgb(33,113,181)',
color_q5 = 'rgb(8,81,156)',
color_q6 = 'rgb(8,48,107)',
colorScale = [
color_q6,
color_q5,
color_q4,
color_q3,
color_q2,
color_q1,
color_q0
] // array used to generate color scale
var quantize = d3.scale.threshold()
.domain([0.04, 0.12, 0.30, 0.70, 1.52, 2.52, 27.83])
.range(d3.range(7).map(function(i) { return "q" + i; }));
var jsonMilExp = {};
var map = new Datamap({
element: document.getElementById('container1'),
scope: 'world',
setProjection: function(element) {
var projection = d3.geo.robinson()
.center([19, -3])
.rotate([4.4, 0])
.scale(250)
.translate([element.offsetWidth / 2, element.offsetHeight / 2]);
var path = d3.geo.path()
.projection(projection);
return {path: path, projection: projection};
},
fills: {
defaultFill: color_qmissing,
qmissing: color_qmissing,
q0: color_q0,
q1: color_q1,
q2: color_q2,
q3: color_q3,
q4: color_q4,
q5: color_q5,
q6: color_q6
},
done: function(datamap) {
datamap.svg.call(d3.behavior.zoom().on("zoom", redraw));
function redraw() {
datamap.svg.selectAll("g").attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
},
data: jsonMilExp,
geographyConfig: {
//highlightOnHover: False,
highlightFillColor: '00FFFFFF',
highlightBorderColor: '#ffa500',
popupTemplate: function(geo, data) {
if (data == null) {
return '<div class="hoverinfo"><strong>No data available for ' + geo.properties.name + '</strong></div>';
} else {
if (data.CO2) {
str_tech = '<br/>CO2 Emission: ' + format(data.CO2) + ' tonnes per person';
} else {
str_tech = '<br/>CO2 Intensity data missing';
}
if (data.GDP) {
str_gdp = '<br/>GDP: $' + format(data.GDP);
} else {
str_gdp = '<br/>GDP data missing';
}
return ['<div class="hoverinfo"><strong>',
geo.properties.name,
'<br/>Year: ' + data.Year,
// '<br/>CO2 Intensity: ' + data.Tech + ' million tonnes per $10,000',
str_tech,
// '<br/>GDP per capita: $' + numberWithCommas(data.GDP),
str_gdp,
'<br/>Population: ' + format(Math.round(1000*data.Pop)),
'</strong></div>'].join('');
}
}
},
highlightBorderWidth: 4,
responsive: true
});
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: {}
};
var get_fill = function(tech_val) {
//function get_fill(tech_val) {
if (tech_val) {
return quantize(tech_val);
} else {
return 'qmissing';
}
}
var load_data = function(Year) {
console.log(get_fill(NaN));
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.CO2),
Year: +d.Year,
CO2: +d.CO2,
GDP: +d.GDP,
Pop: +d.PopTotal
};
});
for (var k in fullData) {
for (var k2 in fullData[k]) {
fullData[k][k2].fillKey = get_fill(fullData[k][k2].CO2);
}
}
map.updateChoropleth(fullData[Year]);
});
};
var update_data = function(Year) {
map.updateChoropleth(fullData[Year]);
};
load_data(1960);
var slider = d3.slider();
var sliderMargin = 65;
var period = [];
for (var i = 1960; i != 2011; ++i) period.push(i);
/*function animateMap() {
var timer; // create timer object
d3.select('#play')
.on('click', function() { // when user clicks the play button
if(playing == false) { // if the map is currently playing
timer = setInterval(function(){ // set a JS interval
if(currentYear < period.length-1) {
currentYear += 1; // increment the current attribute counter
} else {
currentYear = 0; // or reset it to zero
}
update_data(period[currentYear]);
d3.select('#clock').html(period[currentYear]); // update the clock
//d3.select("slider-div .d3-slider-handle")
//.style("left", 100*currentYear/period.length +"%");
//slider.value(currentYear);
}, 1000);
//d3.select("#slider-div .d3-slider-handle")
//.style("left", 100*currentYear/period.length + "%" );
//slider.value(currentYear);
d3.select(this).html('stop'); // change the button label to stop
playing = true; // change the status of the animation
} else { // else if is currently playing
clearInterval(timer); // stop the animation by clearing the interval
d3.select(this).html('play'); // change the button label to play
playing = false; // change the status again
};
});
}*/
var interval;
function createSlider(){
sliderScale = d3.scale.linear().domain([0,50]);
var val = slider ? slider.value() : 0;
slider = d3.slider()
.scale( sliderScale )
/*.on("slide",function(event,value){
if ( isPlaying ){
clearInterval(interval);
}
update_data(period[val]);
})*/
.on("slide", function(event, value){
if (isPlaying){
clearInterval(interval);
}
update_data(period[value]);
d3.select('#clock').html(period[value]);})
.on("slideend",function(){
if ( isPlaying ) animateMap();
d3.select("#slider-div").on("mousemove",sliderProbe)
})
.on("slidestart",function(){
d3.select("#slider-div").on("mousemove",null)
})
.value(val);
d3.select("#slider-div").remove();
d3.select("#slider-container")
.append("div")
.attr("id","slider-div")
.style("width",500 + "px")
.on("mousemove", sliderProbe)
/*.on("slide", function() {
console.log('inside slide');
var Year = this.value;
update_data(Year);
d3.select('#clock').html(Year); // update the clock
})*/
.on("mouseout",function(){
d3.select("#slider-probe").style("display","none");
})
.call( slider );
d3.select("#slider-div a").on("mousemove",function(){
d3.event.stopPropagation();
})
}
/*d3.selectAll("input").on("change", function change() {
var Year = this.value;
update_data(Year);
d3.select('#clock').html(Year); // update the clock
});*/
d3.selectAll("#slider-div .d3-slider-handle").on("slide", function(){
console.log('slider event');
var Year = this.value;
update_data(Year);
d3.select('#clock').html(Year);
});
function sliderProbe(){
//var d = dateScale.invert( ( d3.mouse(this)[0] ) );
var Year = this.value;
/*d3.select("#slider-probe").on("change", function change(){
var Year = this.value;
update_data(Year);
d3.select('#clock').html(Year);
})*/
d3.select("#slider-probe")
.style( "left", d3.mouse(this) + sliderMargin + "px" )
.style("display","block")
.select("p")
.html( Year )
}
createSlider();
var currentYear = 0;
isPlaying = false;
function animateMap(){
var currentYear = slider ? slider.value() : 0;
interval = setInterval( function(){
currentYear++;
if ( currentYear == period.length ) currentYear = 0;
d3.select("#slider-div .d3-slider-handle")
.style("left", 100*currentYear/period.length + "%" );
slider.value(currentYear)
d3.select('#clock').html(period[currentYear])
update_data( period[ currentYear ]);
//d3.select(this).html('stop')
if ( currentYear == period.length ){
isPlaying = false;
d3.select("#play").classed("pause",false).attr("title","Play animation");
clearInterval( interval );
return;
}
},500);
}
d3.select("#play")
.attr("title","Play animation")
.on("click",function(){
if ( !isPlaying ){
isPlaying = true;
//d3.select(this).classed("pause",true).attr("title","Pause animation");
d3.select(this).html('pause').attr("title", "Pause animation");
animateMap();
} else {
isPlaying = false;
//d3.select(this).classed("pause",false).attr("title","Play animation");
d3.select(this).html('play').attr("title", "Play animation");
clearInterval( interval );
}
});
// append the container for the legend
/*d3.select('svg')
.append('g')
.attr('class', 'legend');
var
legendBarWidth = 30,
legendBarHeight = 10,
legendOffsetX = 50,
legendOffsetY = 500;
var date = ["2.52" + " - " + "27.83", "1.52" + " - " + "2.52",
"0.70" + " - " + "1.52", "0.30" + " - " + "0.70", "0.12" + " - " + "0.30",
"0.04" + " - " + "0.12", "0" + " - " + "0.04" ];
// draw the legend
d3.select('.legend').selectAll('.legend')
.data(colorScale)
.enter()
.append('rect')
.attr('x', legendOffsetX)
.attr('y', function(d,i) {
return legendOffsetY + legendBarHeight*i;
})
.attr('width', legendBarWidth)
.attr('height', legendBarHeight)
.attr('fill', function(d){ return d; });
d3.select('.legend').selectAll('.legend')
.data(colorScale)
.enter()
.append('text')
.text(function(d,i){
var quantizedRange = quantize.invertExtent('q'+i);
return date[i];
})
.attr('x', legendOffsetX+(legendBarWidth*1.25))
.attr('y', function(d,i){
return legendOffsetY + (legendBarHeight*0.9) + legendBarHeight*i;
})
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.attr("fill", "black");
d3.select('.legend').selectAll('.legend')
.data(colorScale)
.enter()
.append('text')
.text("CO2 Emission")
.attr("class", "caption")
.attr('x', legendOffsetX)
.attr('y', legendOffsetY-0.8)
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.attr("fill", "black"); */
//window.onload = animateMap();
//animateMap();
</script>
</body>