-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
286 lines (260 loc) · 8.87 KB
/
main.js
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
var options = {
normalize: false,
percent: false,
lines: true,
textdist: {
a: 17,
b: 10
},
referenceYear: 2010,
colorBy: "president", //"president", //
margin: {
left: 40,
right: 10,
top: 10,
bottom: 40
},
height: function () {
return 300 - this.margin.left - this.margin.right
},
width: function () {
return 1000 - this.margin.top - this.margin.bottom
},
separateCountries: true,
onlyLatinoAmerica: false,
joinedScales: false,
language: "es"
};
var messages = {
add: {
"es": "Agregar país",
"en": "Add Country"
}
}
function init() {
$.fn.bootstrapSwitch.defaults.size = 'mini';
$.fn.bootstrapSwitch.defaults.onText = 'on';
$.fn.bootstrapSwitch.defaults.offText = 'off';
$.fn.bootstrapSwitch.defaults.handleWidth = 12;
// Separate presidential periods
function switchPresidentOptions() {
if (options.colorBy == "president") {
options.colorBy = "country";
} else {
options.colorBy = "president";
}
}
$("[name='presidentsMulti']").bootstrapSwitch();
$('input[name="presidentsMulti"]').on('switchChange.bootstrapSwitch', function (event, state) {
switchPresidentOptions();
drawCharts();
});
$("[name='escalas']").bootstrapSwitch();
$('input[name="escalas"]').on('switchChange.bootstrapSwitch', function (event, state) {
options.joinedScales = !options.joinedScales;
drawCharts();
});
// Separate charts into small multiples
function switchSmallMultiples() {
options.separateCountries = !options.separateCountries;
if (options.separateCountries) {
$("div[id=separatecountriesPanel]").show();
$("div[id=multicountryPanel]").hide();
$(".optionMulti").show();
$(".optionSingle").hide();
options.height = function () {
return 300 - this.margin.left - this.margin.right
};
} else {
$("div[id=separatecountriesPanel]").hide();
$("div[id=multicountryPanel]").show();
$(".optionSingle").show();
$(".optionMulti").hide();
options.joinedScales = false;
options.height = function () {
return 600 - this.margin.left - this.margin.right
};
}
}
$("[name='multicountryMulti']").bootstrapSwitch();
$("[name='multicountrySeparate']").bootstrapSwitch();
$('input[name="multicountryMulti"]').on('switchChange.bootstrapSwitch', function (event, state) {
$("[name='multicountrySeparate']").bootstrapSwitch('toggleState', true);
switchSmallMultiples();
drawCharts();
});
$('input[name="multicountrySeparate"]').on('switchChange.bootstrapSwitch', function (event, state) {
$("[name='multicountryMulti']").bootstrapSwitch('toggleState', true);
switchSmallMultiples();
drawCharts();
});
// Draw percent change yr to yr and not values
$("[name='percent']").bootstrapSwitch();
$('input[name="percent"]').on('switchChange.bootstrapSwitch', function (event, state) {
options.percent = !options.percent;
normalizeData(flatdata);
drawCharts();
});
// Allow all countrues
$("[name='allcountries']").bootstrapSwitch();
$('input[name="allcountries"]').on('switchChange.bootstrapSwitch', function (event, state) {
options.onlyLatinoAmerica = !options.onlyLatinoAmerica;
if (options.onlyLatinoAmerica) {
allowOnlyLatinAmerica();
} else {
allowAllCountries();
}
normalizeData(flatdata);
fillCountryList();
drawCharts();
});
// Draw normalized values (CAREFUL SOME DONT HAVE YEAR 2010)
$("[name='reference']").bootstrapSwitch();
$('input[name="reference"]').on('switchChange.bootstrapSwitch', function (event, state) {
options.normalize = !options.normalize;
normalizeData(flatdata);
// I should turn off the percent switch
drawCharts();
});
d3.selectAll(".topSelector").on("change", function () {
var dropdown = $(this);
var newCountry = dropdown.val();
usedCountries[newCountry] = countries[newCountry];
fillCountryList();
drawCharts();
});
d3.selectAll(".bottomSelector").on("change", function () {
var dropdown = $(this);
var newCountry = dropdown.val();
usedCountries[newCountry] = countries[newCountry];
fillCountryList();
drawCharts();
dropdown[0].scrollIntoView();
});
$("div[id=multicountryPanel]").hide();
$(".optionSingle").hide();
}
function initColors() {
countries["Argentina"].scale = colorbrewer.Blues[8];
countries["Argentina"].color = "#092dc6";
countries["Brazil"].scale = colorbrewer.YlGn[8];
countries["Brazil"].color = "#ffd800";
countries["Bolivia"].scale = colorbrewer.Greens[8];
countries["Bolivia"].color = "#029d02";
countries["Chile"].scale = colorbrewer.Reds[8];
countries["Chile"].color = "#e00000";
countries["Uruguay"].scale = colorbrewer.RdPu[8];
countries["Uruguay"].color = "#05b4ed";
countries["Paraguay"].scale = colorbrewer.Greys[8];
countries["Paraguay"].color = "#d300a2";
}
function fillCountryList() {
var dropdown = $(".countryList");
var vals = [];
Object.keys(countries).forEach(function (country) {
if (!(country in usedCountries)) {
vals.push(country);
}
});
vals.sort();
dropdown.empty();
if (vals.length > 0) {
dropdown.show();
dropdown.append('<option selected disabled value="base">' + messages.add[options.language] + '</option>');
$.each(vals, function (index, country) {
dropdown.append("<option value='" + country + "'>" + country + "</option>");
});
} else {
dropdown.hide();
}
}
function allowOnlyLatinAmerica() {
usedCountries = {};
countries = {};
latinAmerica.forEach(function (country) {
countries[country] = allCountries[country];
usedCountries[country] = countries[country];
});
}
function allowAllCountries() {
usedCountries = {};
countries = allCountries;
latinAmerica.forEach(function (country) {
usedCountries[country] = countries[country];
});
}
var flatdata;
// Finally we do something
queue()
.defer(d3.csv, "data/GDP.csv")
.defer(d3.csv, "data/GINI.csv")
.defer(d3.json, "data/presidents.json")
.await(function (error, gdps, ginis, presidents) {
for (var country in presidents) {
presidents[country].presidents.sort(function (a, b) {
if (a.period[0] < b.period[0]) {
return -1;
}
if (a.period[0] > b.period[0]) {
return 1;
}
return 0;
});
}
//console.log(presidents);
allCountries = presidents;
init();
gdps.forEach(function (row) {
if (row["Country Name"] in allCountries) {
allCountries[row["Country Name"]]["gdp"] = row;
}
});
ginis.forEach(function (row) {
if (row["Country Name"] in allCountries) {
allCountries[row["Country Name"]]["gini"] = row;
}
});
flatdata = prepareData(gdps, ginis, options, allCountries); //it does things to allCountries
//allowOnlyLatinAmerica();
allowAllCountries();
normalizeData(flatdata);
fillCountryList();
initColors();
drawCharts();
});
function drawCharts() {
$(".country").remove();
var container = d3.select("#separatecountries");
var tiling = [6, 4];
for (var country in usedCountries) {
oneCountryData = flatdata.filter(function (d) {
return d.country == country;
});
var divName = country.replace(" ", "_");
container.append("div")
.attr("class", "country col-md-" + tiling[1])
.append("div")
.attr("class", "thumbnail")
.append("div")
.attr("class", "chart")
.attr("id", divName);
makeCairoChart(divName, country, oneCountryData, options);
};
var onlyCountries = flatdata.filter(function (d) {
return d.country in usedCountries;
});
if (onlyCountries.length > 0) {
makeCairoChart("generalDiv", "", onlyCountries, options);
}
var directionCountries = [];
if (options.onlyLatinoAmerica) {
directionCountries = flatdata.filter(function (d) {
return latinAmerica.indexOf(d.country) >= 0;
});
} else {
directionCountries = flatdata.filter(function (d) {
return d.country in usedCountries;
});
}
makeDirectionChart(directionCountries, options);
}