-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.js
More file actions
172 lines (145 loc) · 4.67 KB
/
src.js
File metadata and controls
172 lines (145 loc) · 4.67 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
//The Request Funtion
function request(url, cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
// console.log("THis is console log", json)
cb(undefined, json)
} else {
// console.log("waiting for response");
}
};
xhr.open("GET", url, true);
// xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.send();
}
//The Location Funtion
function getlocation(callback) {
request("https://wtfismyip.com/json", callback)
}
function country(callback) {
request("http://api.population.io:80/1.0/countries", callback)
}
country(function(error, data) {
var select = document.getElementById("myList")
data.countries.sort();
data.countries.forEach(function(ele) {
opt = document.createElement("option");
opt.textContent = ele;
select.appendChild(opt);
})
})
var select = document.getElementById("myList");
getlocation(function(error, data) {
var places=data.YourFuckingLocation;
if(data.YourFuckingLocation.indexOf(",")>-1){
var p = data.YourFuckingLocation.split(",");
var places = p[2]
places = places.trim();
}
if (places == "Occupied Palestine") {
places = "West Bank and Gaza"
}
select.value = places;
//console.log("places", places);
sts(places, function(error, data) {
gchart(data, places)
});
});
select.addEventListener('change', function(){
sts(select.value, function(error, data) {
gchart(data, select.value)
});
});
function sts(place, callback) {
request("http://api.population.io:80/1.0/population/2016/" + place + "/", callback)
}
google.charts.load('current', { 'packages': ['corechart'] });
function gchart(d, p) {
//console.log("pass data", d);
// Set a callback to run when the Google Visualization API is loaded.
var dc1 = [
['Age', '0'],
['Male', 0],
['Female', 0]
];
var dc2 = [
['Gender', 'Total'],
['Male', 0],
['Female', 0]
];
for (var i = 1; i < d.length; i++) {
dc1[0][i] = d[i].age.toString();
dc1[1][i] = d[i].males;
dc1[2][i] = d[i].females;
dc2[1][1] = dc2[1][1] + d[i].males;
dc2[2][1] = dc2[2][1] + d[i].females;
};
function comparem(a, b) {
return (b.males - a.males)
}
function comparef(a, b) {
return (b.females - a.females)
}
var dc3t1 = d.sort(comparem).slice(0, 10);
var dc3t2 = d.sort(comparef).slice(0, 10);
var dc3m = [
['Age', '0'],
['Male', 0]
];
var dc3f = [
['Age', '0'],
['Female', 0]
];
for (var i = 1; i < dc3t1.length; i++) {
dc3m[0][i] = dc3t1[i].age.toString();
dc3m[1][i] = dc3t1[i].males;
dc3f[0][i] = dc3t2[i].age.toString();
dc3f[1][i] = dc3t2[i].females;
};
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
// Create the data table.
var data = new google.visualization.arrayToDataTable(dc1);
var data2 = new google.visualization.arrayToDataTable(dc2);
var data3 = new google.visualization.arrayToDataTable(dc3m);
var data4 = new google.visualization.arrayToDataTable(dc3f);
// Set chart options
var options = {
'title': 'Age Distribution',
'width': 400,
'height': 300,
legend: { position: 'none' }
};
var options2 = {
'title': 'Gender ratio',
'width': 400,
'height': 300,
pieHole: 0.4,
};
var options3 = {
'title': 'Males Top 10 Ages',
'width': 400,
'height': 300,
textPosition: 'in',
hAxis: { slantedText: true }
};
var options4 = {
'title': 'Females Top 10 Ages',
'width': 400,
'height': 300,
textPosition: 'in',
hAxis: { slantedText: true }
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
var chart2 = new google.visualization.PieChart(document.getElementById('chart_div2'));
chart2.draw(data2, options2);
var chart3 = new google.visualization.ColumnChart(document.getElementById('chart_div3'));
chart3.draw(data3, options3);
var chart4 = new google.visualization.ColumnChart(document.getElementById('chart_div4'));
chart4.draw(data4, options4);
}
}