-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.js
344 lines (295 loc) · 11.5 KB
/
data.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
var dispatch = d3.dispatch("dataLoaded",
"highlight","highlightmeta","highlightelem","unhighlight");
var name = "persName";
document.getElementById("element").innerHTML = "Person Names";
var redraws = 0;
// buttons
d3.select("#btn-persName")
.classed("button-clicked", true)
.on("click",function(){
d3.selectAll(".btn-primary").classed("button-clicked", false);
d3.select(this).classed("button-clicked", true);
document.getElementById("element").innerHTML = "Person Names";
redraws++;
name = "persName";
drawNetwork(name);
});
d3.select("#btn-orgName")
.on("click",function(){
d3.selectAll(".btn-primary").classed("button-clicked", false);
d3.select(this).classed("button-clicked", true);
document.getElementById("element").innerHTML = "Organization Names";
redraws++;
name = "orgName";
drawNetwork(name);
});
d3.select("#btn-placeName")
.on("click",function(){
d3.selectAll(".btn-primary").classed("button-clicked", false);
d3.select(this).classed("button-clicked", true);
document.getElementById("element").innerHTML = "Place Names";
redraws++;
name = "placeName";
drawNetwork(name);
});
var meta;
drawNetwork(name);
function drawNetwork(name){
var queue = d3.queue();
if (!meta) {
queue.defer(d3.csv, "wwo-metadata_2017-12-06.csv")
}
queue.defer(d3.csv, "wwo_element_" + name + ".csv")
.await(function(error, m, elem) {
if (error) throw error;
if (!meta) {
meta = m;
meta.forEach(function(i){
if(i.pubDate == "1558-01-23"){
i.pubDate = "1558";
}
if(i.pubDate == "1601-11-30"){
i.pubDate = "1601";
}
if(i.pubDate == "1643-01-28"){
i.pubDate = "1643";
}
if(i.pubDate == "1760-1761"){
i.pubDate = "1760";
}
i.pubDate = new Date(+i.pubDate,0,1);
if(i.mainGenre == "G.drama" ||
i.mainGenre == "G.drama.prose" ||
i.mainGenre == "G.drama.verse" ||
i.mainGenre == "G.drama.mixed"){
i.genre = "Drama";
if(i.mainGenre == "G.drama"){ i.mainGenre = "Drama"; }
if(i.mainGenre == "G.drama.prose"){ i.mainGenre = "Drama: Prose"; }
if(i.mainGenre == "G.drama.verse"){ i.mainGenre = "Drama: Verse"; }
if(i.mainGenre == "G.drama.mixed"){ i.mainGenre = "Drama: Mixed"; }
}
else if(i.mainGenre == "G.non-fiction" ||
i.mainGenre == "G.non-fiction.other" ||
i.mainGenre == "G.non-fiction.essay" ||
i.mainGenre == "G.non-fiction.letter"){
i.genre = "Non-fiction";
if(i.mainGenre == "G.non-fiction"){ i.mainGenre = "Non-fiction"; }
if(i.mainGenre == "G.non-fiction.other"){ i.mainGenre = "Non-fiction: Other"; }
if(i.mainGenre == "G.non-fiction.essay"){ i.mainGenre = "Non-fiction: Essay"; }
if(i.mainGenre == "G.non-fiction.letter"){ i.mainGenre = "Non-fiction: Letter"; }
}
else if(i.mainGenre == "G.verse" ||
i.mainGenre == "G.verse.lyric"||
i.mainGenre == "G.verse.narrative" ||
i.mainGenre == "G.verse.other"){
i.genre = "Verse";
if(i.mainGenre == "G.verse"){ i.mainGenre = "Verse"; }
if(i.mainGenre == "G.verse.lyric"){ i.mainGenre = "Verse: Lyric"; }
if(i.mainGenre == "G.verse.narrative"){ i.mainGenre = "Verse: Narrative"; }
if(i.mainGenre == "G.verse.other"){ i.mainGenre = "Verse: Other"; }
}
else if(i.mainGenre == "G.fiction" ||
i.mainGenre == "G.fiction.other"||
i.mainGenre == "G.fiction.novel" ||
i.mainGenre == "G.fiction.letter"){
i.genre = "Fiction";
if(i.mainGenre == "G.fiction"){ i.mainGenre = "Fiction"; }
if(i.mainGenre == "G.fiction.other"){ i.mainGenre = "Fiction: Other"; }
if(i.mainGenre == "G.fiction.novel"){ i.mainGenre = "Fiction: Novel"; }
if(i.mainGenre == "G.fiction.letter"){ i.mainGenre = "Fiction: Letter"; }
}
else if(i.mainGenre == ""){
i.mainGenre = "Other";
i.genre = "Other";
}
});
} else {
elem = m;
}
// categorize genres
elem.forEach(function(i){
if(i.pubDate == "1558-01-23"){
i.pubDate = "1558";
}
if(i.pubDate == "1601-11-30"){
i.pubDate = "1601";
}
if(i.pubDate == "1643-01-28"){
i.pubDate = "1643";
}
if(i.pubDate == "1760-1761"){
i.pubDate = "1760";
}
// i.pubDate = +i.pubDate;
i.pubDate = new Date(+i.pubDate,0,1);
if(i.mainGenre == "G.drama" ||
i.mainGenre == "G.drama.prose" ||
i.mainGenre == "G.drama.verse" ||
i.mainGenre == "G.drama.mixed"){
i.genre = "Drama";
if(i.mainGenre == "G.drama"){ i.mainGenre = "Drama"; }
if(i.mainGenre == "G.drama.prose"){ i.mainGenre = "Drama: Prose"; }
if(i.mainGenre == "G.drama.verse"){ i.mainGenre = "Drama: Verse"; }
if(i.mainGenre == "G.drama.mixed"){ i.mainGenre = "Drama: Mixed"; }
}
else if(i.mainGenre == "G.non-fiction" ||
i.mainGenre == "G.non-fiction.other" ||
i.mainGenre == "G.non-fiction.essay" ||
i.mainGenre == "G.non-fiction.letter"){
i.genre = "Non-fiction";
if(i.mainGenre == "G.non-fiction"){ i.mainGenre = "Non-fiction"; }
if(i.mainGenre == "G.non-fiction.other"){ i.mainGenre = "Non-fiction: Other"; }
if(i.mainGenre == "G.non-fiction.essay"){ i.mainGenre = "Non-fiction: Essay"; }
if(i.mainGenre == "G.non-fiction.letter"){ i.mainGenre = "Non-fiction: Letter"; }
}
else if(i.mainGenre == "G.verse" ||
i.mainGenre == "G.verse.lyric"||
i.mainGenre == "G.verse.narrative" ||
i.mainGenre == "G.verse.other"){
i.genre = "Verse";
if(i.mainGenre == "G.verse"){ i.mainGenre = "Verse"; }
if(i.mainGenre == "G.verse.lyric"){ i.mainGenre = "Verse: Lyric"; }
if(i.mainGenre == "G.verse.narrative"){ i.mainGenre = "Verse: Narrative"; }
if(i.mainGenre == "G.verse.other"){ i.mainGenre = "Verse: Other"; }
}
else if(i.mainGenre == "G.fiction" ||
i.mainGenre == "G.fiction.other"||
i.mainGenre == "G.fiction.novel" ||
i.mainGenre == "G.fiction.letter"){
i.genre = "Fiction";
if(i.mainGenre == "G.fiction"){ i.mainGenre = "Fiction"; }
if(i.mainGenre == "G.fiction.other"){ i.mainGenre = "Fiction: Other"; }
if(i.mainGenre == "G.fiction.novel"){ i.mainGenre = "Fiction: Novel"; }
if(i.mainGenre == "G.fiction.letter"){ i.mainGenre = "Fiction: Letter"; }
}
else if(i.mainGenre == ""){
i.mainGenre = "Other";
i.genre = "Other";
}
});
function dataTransform(meta, elem){
// create array of distinct in-text element values
var elemDist = d3.nest()
.key(function(d){return d.element})
.rollup(function(d){return d.length})
.entries(elem);
elemDist.sort(function(a,b){
return b["value"]-a["value"];
});
// take top 20 distinct in-text element values
var elemDistTop = elemDist.slice(0,20);
// filter element data to match elements in elemDistTop
var elemTop = elem.filter(function(d){
var i = 0;
elemDistTop.forEach(function(a){
if(a.key == d.element){
i = 1;
}
});
return i == 1;
});
// elemTop.sort(function(a,b){
// return d3.ascending(a.mainGenre, b.mainGenre);
// });
elemTop.sort(function(a,b){
return b["pubDate"]-a["pubDate"];
});
elemTop.sort(function(a,b){
return d3.ascending(a.element, b.element);
});
// filter metadata to match texts in elemTop
meta.forEach(function(d){
d.isTop = 0;
elemTop.forEach(function(a){
if(a.filename == d.filename){
d.isTop = 1;
}
});
if (d.isTop != 1) {
d.isTop = 0;
}
});
var metaTop = meta.filter(function(d){
return d.isTop == 1;
});
metaTop.sort(function(a,b){
if (a.title < b.title){
return -1;
}
else if (a.title > b.title){
return 1;
}
else{ return 0; }
});
// var metaTop = meta.filter(function(d){
// var i = 0;
// elemTop.forEach(function(a){
// if(a.filename == d.filename){
// i = 1;
// }
// });
// return i == 1;
// });
// create array of distinct genre values
var metaTopGenre = d3.nest()
.key(function(d){ return d.mainGenre })
.rollup(function(d){return d.length})
.entries(metaTop);
metaTopGenre.sort(function(a,b){
return d3.ascending(a.key, b.key);
})
// create objects connecting positions of element and genre text to link data
var elemDistObj = {};
var metaObj = {};
elemDistTop.forEach(function(a){
elemDistObj[a.key] = a
});
metaTopGenre.forEach(function(a){
metaObj[a.key] = a
});
elemTop.forEach(function(i){
i.sourceData = metaObj[i.mainGenre]
i.targetData = elemDistObj[i.element]
});
return {
meta: meta,
metaTop: metaTop,
metaTopGenre: metaTopGenre,
elemTop: elemTop,
elemDistTop: elemDistTop
}
};
async function waitForDataTransform(){
var allData = await dataTransform(meta, elem);
dispatch.call("dataLoaded", null, allData);
};
waitForDataTransform();
// dataTransform(meta, elem).then( function(allData){
// //do something with result
// console.log(allData);
// dispatch.call("dataLoaded", null, allData);
// });
// dispatch.call(Transformation(meta, elem))
// dispatch.call("dataLoaded", null, meta, metaTop, metaTopGenre, elemDistTop, elemTop);
});
};