-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
266 lines (215 loc) · 11.4 KB
/
test.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
<html>
<body>
<!-- https://developers.google.com/chart/interactive/docs/gallery/sankey -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="jquery.csv.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="sankey_multiple" style="width: 1360px; height: 830px;"></div>
<script type="text/javascript">
google.charts.load("current", {packages:["sankey"]});
google.charts.setOnLoadCallback(drawChart);
// function uniq(a) {
// var seen = {};
// return a.filter(function(item) {
// return seen.hasOwnProperty(item) ? false : (seen[item] = true);
// });
// }
function uniq(a) {
var seen = {};
return a.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}
function drawChart() {
$.get("the-counted-2015-processed.csv", function(csvString) {
// transform the CSV string into a 2-dimensional array
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
var dataCSV = new google.visualization.arrayToDataTable(arrayData); // this new DataTable object holds all the data
var data = new google.visualization.DataTable();
var allAges = [];
var allStates = [];
var allGenders = [];
var allRaces = [];
var allArmed = [];
var allClassification = [];
console.log(dataCSV);
for (var i = 0; i < dataCSV.Lf.length; i++) { //loop of all data in the array
allAges.push(dataCSV.Lf[i].c[2].v);
allStates.push(dataCSV.Lf[i].c[10].v);
allGenders.push(dataCSV.Lf[i].c[3].v);
allRaces.push(dataCSV.Lf[i].c[4].v);
allArmed.push(dataCSV.Lf[i].c[13].v);
allClassification.push(dataCSV.Lf[i].c[11].v);
}
allAges = uniq(allAges);
allStates = uniq(allStates);
allGenders = uniq(allGenders);
allRaces = uniq(allRaces);
allArmed = uniq(allArmed);
allClassification = uniq(allClassification);
// console.log(allAges);
// console.log(allStates);
// console.log(allGenders);
// console.log(allRaces);
// console.log(allArmed);
// console.log(allClassification);
var FinalDataArray = []; // only need one big array to store everything
// -----------1. Age to Gender-----------// format WRONG, coz age is number not string.
for (var genderindex = 0; genderindex < allGenders.length; genderindex+=1) { // choose a gender to look for
for (var ageindex = 5; ageindex <= 80; ageindex+=5) { // choose a age to look for
var nowCount = 0; // start counting with no values
var nowAge = ageindex; // our chosen age for this loop
var nowGender = allGenders[genderindex]; // out chosen gender for this loop
for (var dataIndex = 0; dataIndex < dataCSV.Lf.length; dataIndex++) { //loop of all data in the array looking for the above race and gender
// console.log( dataCSV.Lf[dataIndex].c[2].v + ' / ' + nowAge)
var inDataAge = dataCSV.Lf[dataIndex].c[2].v;
// if(inDataAge >= nowAge && inDataAge <= nowAge+5){
// console.log("it counts");
// }
if(inDataAge >= nowAge && inDataAge <= nowAge+5 && dataCSV.Lf[dataIndex].c[3].v == nowGender ){ // if this data object matches our chosen gender and race
nowCount++; //add one value to our item count
}
}
var miniArray_local = []; // make a blank array to store this small data object (local only)
console.log('in all the data we found this:');
miniArray_local.push(nowAge+'-'+(nowAge+5)); // push one value to our small blank array
miniArray_local.push(nowGender); // push one value to our small blank array
miniArray_local.push(nowCount);// push one value to our small blank array
console.log(miniArray_local);
FinalDataArray.push(miniArray_local);// push our mini array (three values long) into our master array list
}
}
// -----------1. State to Gender-----------//
// for (var stateindex = 0; stateindex < allStates.length; stateindex++) { // choose a gender to look for
// for (var genderindex = 0; genderindex < allGenders.length; genderindex++) { // choose a race to look for
// var nowCount = 0; // start counting with no values
// var nowState = allStates[stateindex]; // our chosen gender for this loop
// var nowGender = allGenders[genderindex]; // out chosen race for this loop
// for (var dataIndex = 0; dataIndex < dataCSV.Lf.length; dataIndex++) { //loop of all data in the array looking for the above race and gender
// if(dataCSV.Lf[dataIndex].c[10].v == nowState && dataCSV.Lf[dataIndex].c[3].v == nowGender ){ // if this data object matches our chosen gender and race
// nowCount++; //add one value to our item count
// }
// }
// var miniArray_local = []; // make a blank array to store this small data object (local only)
// console.log('in all the data we found this:');
// miniArray_local.push(nowState); // push one value to our small blank array
// miniArray_local.push(nowGender); // push one value to our small blank array
// miniArray_local.push(nowCount);// push one value to our small blank array
// console.log(miniArray_local);
// FinalDataArray.push(miniArray_local);// push our mini array (three values long) into our master array list
// }
// }
// -----------2. Gender to Race-----------//
for (var genderindex = 0; genderindex < allGenders.length; genderindex++) { // choose a gender to look for
for (var raceindex = 0; raceindex < allRaces.length; raceindex++) { // choose a race to look for
var nowCount = 0; // start counting with no values
var nowGender = allGenders[genderindex]; // our chosen gender for this loop
var nowRace = allRaces[raceindex]; // out chosen race for this loop
for (var dataIndex = 0; dataIndex < dataCSV.Lf.length; dataIndex++) { //loop of all data in the array looking for the above race and gender
if(dataCSV.Lf[dataIndex].c[3].v == nowGender && dataCSV.Lf[dataIndex].c[4].v == nowRace ){ // if this data object matches our chosen gender and race
nowCount++; //add one value to our item count
}
}
var miniArray_local = []; // make a blank array to store this small data object (local only)
// console.log('in all the data we found this:');
miniArray_local.push(nowGender); // push one value to our small blank array
miniArray_local.push(nowRace); // push one value to our small blank array
miniArray_local.push(nowCount);// push one value to our small blank array
// console.log(miniArray_local);
FinalDataArray.push(miniArray_local);// push our mini array (three values long) into our master array list
}
}
//-----------3. Race to Armed-----------//
for (var raceindex = 0; raceindex < allRaces.length; raceindex++) { // choose a race to look for
for (var armedindex = 0; armedindex < allArmed.length; armedindex++) {// choose a armed status to look for
var nowCount = 0; // start counting with no values
var nowRace = allRaces[raceindex];// our chosen race for this loop
var nowArmed = allArmed[armedindex];// our chosen armed status for this loop
for (var dataIndex = 0; dataIndex < dataCSV.Lf.length; dataIndex++) { //loop of all data in the array
if(dataCSV.Lf[dataIndex].c[4].v == nowRace && dataCSV.Lf[dataIndex].c[13].v == nowArmed ){// if this data object matches our chosen race and armed status
nowCount++; //add one value to our item count
}
}
var miniArray_local = [];// make a blank array to store this small data object (local only)
// console.log('in all the data we found this:');
miniArray_local.push(nowRace);// push one value to our small blank array
miniArray_local.push(nowArmed);// push one value to our small blank array
miniArray_local.push(nowCount);// push one value to our small blank array
// console.log(miniArray_local);
FinalDataArray.push(miniArray_local);// push our mini array (three values long) into our master array list
}
}
//-----------4. Armed to Classification-----------//
for (var armedindex = 0; armedindex < allArmed.length; armedindex++) { // choose an armed to look for
for (var classificationindex = 0; classificationindex < allClassification.length; classificationindex++) { // choose a classification to look for
var nowCount = 0; // start counting with no values
var nowArmed = allArmed[armedindex]; // our chosen gender for this loop
var nowClassification = allClassification[classificationindex]; // out chosen race for this loop
for (var dataIndex = 0; dataIndex < dataCSV.Lf.length; dataIndex++) { //loop of all data in the array looking for the above race and gender
if(dataCSV.Lf[dataIndex].c[13].v == nowArmed && dataCSV.Lf[dataIndex].c[11].v == nowClassification ){ // if this data object matches our chosen gender and race
nowCount++; //add one value to our item count
}
}
var miniArray_local = []; // make a blank array to store this small data object (local only)
// console.log('in all the data we found this:');
miniArray_local.push(nowArmed); // push one value to our small blank array
miniArray_local.push(nowClassification); // push one value to our small blank array
miniArray_local.push(nowCount);// push one value to our small blank array
// console.log(miniArray_local);
FinalDataArray.push(miniArray_local);// push our mini array (three values long) into our master array list
}
}
console.log('IN THE END we found this:');
console.log(FinalDataArray);
data.addColumn('string', 'From');
data.addColumn('string', 'To');
data.addColumn('number', 'Ammt');
data.addRows(FinalDataArray);
//-----------for style----------//
var colors = ['#ccffcc', '#e4ffd4', '#a1ffad', '#c4ffa1', '#d4e5ff',
'#aeeeae','#d6f6d6',
'#fff68f', '#f2b686',
'#ffefd4', '#f98a9a'];
// var colors = ['#a6cee3', '#b2df8a', '#fb9a99', '#fdbf6f',
// '#cab2d6', '#ffff99', '#1f78b4', '#33a02c'];
var options = {
sankey: {
node: {
colors: colors
},
link: {
colorMode: 'gradient',
colors: colors
}
}
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.Sankey(document.getElementById('sankey_multiple'));
chart.draw(data, options);
});
}
//REFERENCE BELOW
// var data = new google.visualization.DataTable();
// data.addColumn('string', 'From');
// data.addColumn('string', 'To');
// data.addColumn('number', 'Weight');
// data.addRows([
// [ 'Brazil', 'Portugal', 5 ],
// [ 'Brazil', 'France', 1 ],
// [ 'Brazil', 'Spain', 1 ],
// [ 'Brazil', 'England', 1 ],
// [ 'Canada', 'Portugal', 1 ],
// [ 'Canada', 'France', 5 ],
// [ 'Canada', 'England', 1 ],
// [ 'Mexico', 'Portugal', 1 ],
// [ 'Mexico', 'France', 1 ],
// [ 'Mexico', 'Spain', 5 ],
// [ 'Mexico', 'England', 1 ],
// [ 'USA', 'Portugal', 1 ],
// [ 'Morocco', 'India', 1 ],
// [ 'Morocco', 'Japan', 3 ]
// ]);
</script>
</body>
</html>