-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.html
143 lines (142 loc) · 4.72 KB
/
chart.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
<!DOCTYPE html>
<html>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/chart.min.js"></script>
<script src="http://www.w3schools.com/lib/w3data.js"></script>
<head>
<title>Charts</title>
</head>
<head>
<title></title>
</head>
<nav class="navbar navbar-dark bg-inverse">
<a class="navbar-brand" href="#">The Kyoto Protocol</a>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cesium.html">Globe Visualization</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Chart Visualization</a>
</li>
</ul>
</nav>
<h3>Enter a (valid) country name</h3>
<p>You get it's protocol signing date and its CO2 per capita output before and after the protocol.</p>
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..." id="search">
<span class="input-group-btn">
<button class="btn btn-default" type="button" onclick="getData();">Go!</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
<body>
<div id="parent">
<h1 id="countryName"></h1>
<p id="signingInfo"></p>
<div id="parent">
<canvas id="chart" width="10%" height="10%"></canvas>
</div>
<script>
function getData(){
var request = new XMLHttpRequest();
request.open('GET', 'Data/kyoto.json', false); // `false` makes the request synchronous
request.send(null);
var data;
if (request.status === 200) {
data = request.responseText;
}
var country = document.getElementById("search").value;
var obj = JSON.parse(data);
var year = -1;
for(var i = 0; i < obj.length; i++){
if(obj[i].Country == country){
year = obj[i].Year;
}
}
var request = new XMLHttpRequest();
request.open('GET', 'Data/CO2.json', false); // `false` makes the request synchronous
request.send(null);
var data;
if (request.status === 200) {
data = request.responseText;
}
var startCO2 = 0;
var endCO2 = 0;
var startYear = 0;
var endYear = 0;
var obj = JSON.parse(data);
for(var i = 0; i < obj.length; i++){
if(obj[i].Country == country){
if(year <= 2000 && obj[i].Year == 1990){
startCO2 = obj[i].CO2;
startYear = 1990;
}
if(year <= 2000 && obj[i].Year == 2001){
endCO2 = obj[i].CO2;
endYear = 2001;
}
if(year > 2000 && obj[i].Year == 2001){
startCO2 = obj[i].CO2;
startYear = 2001;
}
if(year > 2000 && obj[i].Year == 2012){
endCO2 = obj[i].CO2;
endYear = 2012;
}
}
}
document.getElementById("countryName").innerHTML = country;
document.getElementById("signingInfo").innerHTML = "Signed the protocol in " + year;
$('#chart').remove(); // this is my <canvas> element
$('#parent').append('<canvas id="chart" width="10%" height="10%"></canvas>');
var ctx = document.getElementById("chart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["(" + startYear + ") Starting CO2 (before Kyoto)", "(" + endYear + ") Ending CO2 (after Kyoto)"],
datasets: [{
label: 'CO2 (metric tons) per capita',
data: [startCO2, endCO2],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
</script>
<script>
$(document).ready(function(){
$('#kyotoData').DataTable();
});
</script>
<script>window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js" integrity="sha384-Plbmg8JY28KFelvJVai01l8WyZzrYWG825m+cZ0eDDS1f7d/js6ikvy1+X+guPIB" crossorigin="anonymous"></script>
<script src="js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</div>
</body>
</html>