forked from aendra-rininsland/economist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_by_sport.php
248 lines (206 loc) · 6.52 KB
/
get_by_sport.php
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
<?php
header('Content-type: text/javascript');
function randColor(){
$r = dechex(mt_rand(100,200)); // generate the red component
$g = dechex(mt_rand(100,200)); // generate the green component
$b = dechex(mt_rand(100,200)); // generate the blue component
$rgb = $r.$g.$b;
if($r == $g && $g == $b){
$rgb = substr($rgb,0,3); // shorter version
}
return '#'.$rgb;
}
function randGrey(){
//base colour is #A39480
$r = dechex(mt_rand(153,163)); // generate the red components
$g = dechex(mt_rand(138,158)); // generate the green components
$b = dechex(mt_rand(118,138)); // generate the blue component
$rgb = $r.$g.$b;
return '#'.$rgb;
}
function find_color($country, $cd){
foreach ($cd as $data) {
if ($data['name'] == $country) return $data['colour'];
}
}
if (empty($_GET['sheet'])) {$gid = 0;} else {$gid = $_GET['sheet'];}
// Set your CSV feed
$feed = 'https://docs.google.com/spreadsheet/pub?key=0Aqqh1cRUSxC-dGlqV1RYeUkyUmFYalpDZHVPV2VHZmc&single=true&gid=' . $gid . '&output=csv';
$feed_country_data = 'https://docs.google.com/spreadsheet/pub?key=0Aqqh1cRUSxC-dGlqV1RYeUkyUmFYalpDZHVPV2VHZmc&single=true&gid=3&output=csv';
// Arrays we'll use later
$keys = array();
$newArray = array();
$keys_cd = array();
$cd_array = array();
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
// Do it
$data = csvToArray($feed, ',');
$data_cd = csvToArray($feed_country_data, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($data) - 1;
$count_cd = count($data_cd) - 1;
//Use first row for names
$labels = array_shift($data);
$labels_cd = array_shift($data_cd);
foreach ($labels as $label) {
$keys[] = $label;
}
foreach ($labels_cd as $label) {
$keys_cd[] = $label;
}
// Add Ids, just in case we want them later
$keys[] = 'id';
$keys_cd[] = 'id';
for ($i = 0; $i < $count; $i++) {
$data[$i][] = $i;
}
for ($i = 0; $i < $count_cd; $i++) {
$data_cd[$i][] = $i;
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = @array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}
for ($j = 0; $j < $count_cd; $j++) {
$d = @array_combine($keys_cd, $data_cd[$j]);
$cd_array[$j] = $d;
}
$final = array(
"children" => array(),
"name" => "The Economist Medal Map - London 2012",
"id" => "root",
"data" => array(
'$color' => '#ccc',
'$area' => 0
)
);
foreach ($newArray as $event){
if ($event['Medal'] == 'Bronze') {
$size = 5;
} else if ($event['Medal'] == 'Silver') {
$size = 10;
} else if ($event['Medal'] == 'Gold') {
$size = 15;
if (isset($event['Country']) && isset($event['Name'])) {
if ($event['Country'] == 'null') {
$country = '(To be decided on ' . $event['Date'] . ')';
} else {
$country = $event['Country'];
}
$recipients = $event['Name'];
}
}
$final['data']['$area'] += $size;
$event['Country'] = trim($event['Country']);
$event['Discipline'] = trim($event['Discipline']);
$event['Event'] = trim($event['Event']);
$event['Medal'] = trim($event['Medal']);
if ($event['Medal'] == 'Gold') {
if ($event['Name'] == 'null')
$colour = '#9e7e00';
else
$colour = '#ffcc00';
} elseif ($event['Medal'] == 'Silver') {
if ($event['Name'] == 'null')
$colour = '#6b6b6b';
else
$colour = '#cccccc';
} elseif ($event['Medal'] == 'Bronze') {
if ($event['Name'] == 'null')
$colour = '#33281e';
else
$colour = '#8C7853';
}
if ($event['Name'] == 'null') {
$event['Name'] = '(To be awarded on ' . $event['Date'] . ')';
$pcolor = randGrey();
$mcolor = $colour;
} else {
$pcolor = find_color($event['Country'], $cd_array);
$mcolor = $pcolor;
}
/* if (!isset($final['children'][$event['Country']]['name'])) {
$final['children'][$event['Country']] = array(
"name" => $event['Country'],
"id" => strtolower($event['Country']),
"data" => array(
'$area' => 0,
'$color' => randColor()
)
);
}
$final['children'][$event['Country']]['data']['$area'] += 10;*/
if (!isset($final['children'][$event['Discipline']])) {
$final['children'][$event['Discipline']] = array(
"name" => $event['Discipline'],
"id" => preg_replace('#(\'|\s)#', '', strtolower($event['Country'] . '_' . $event['Discipline'])),
"data" => array(
'$area' => 0,
'$color' => '#ccc'
),
"children" => array()
);
}
$final['children'][$event['Discipline']]['data']['$area'] += $size;
if (!isset($final['children'][$event['Discipline']]['children'][$event['Event']])) {
$final['children'][$event['Discipline']]['children'][$event['Event']] = array(
"name" => ucwords($event['Event']),
"id" => preg_replace('#(\'|\s)#', '', strtolower($event['Country'] . '_' . $event['Discipline'] . '_' . $event['Event'])),
"data" => array(
'$area' => 0,
'$color' => $pcolor,
),
"children" => array()
);
if (isset($country) && isset($recipients)){
$final['children'][$event['Discipline']]['children'][$event['Event']]['data']['country'] = $country;
$final['children'][$event['Discipline']]['children'][$event['Event']]['data']['recipients'] = $recipients;
}
}
$final['children'][$event['Discipline']]['children'][$event['Event']]['data']['$area'] += $size;
$final['children'][$event['Discipline']]['children'][$event['Event']]['children'][] = array(
'children' => array(),
"name" => $event['Medal'],
"id" => $event['id'],
"data" => array(
'$area' => $size,
'$color' => $mcolor,//$colour,
'recipients' => $event['Name'],
'country' => $event['Country']
),
);
}// foreach
$final['children'] = array_values($final['children']);
foreach ($final['children'] as $key => $country) {
$final['children'][$key]['children'] = array_values($country['children']);
}
for ($i = 0; $i < count($final['children']); $i++) {
for ($j = 0; $j < count($final['children'][$i]['children']); $j++) {
$final['children'][$i]['children'][$j]['children'] = array_values($final['children'][$i]['children'][$j]['children']);
}
}
//print_r($final);
// Print it out as JSON
// Print it out as JSON
if ($_GET['callback']) {
header('Content-type: text/javascript');
echo $_GET['callback']. '(' . json_encode($final) . ');';
} else {
header('Content-type: application/json');
echo json_encode($final);
}
?>