-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.js
372 lines (323 loc) · 13.2 KB
/
apps.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const config = ({
width: 1200,
height: 300,
maxTerms: 100,
maxFontSize: 30,
minFontSize: 12,
padding: 0,
selectedTerm: null
})
// populate dropdown menu with weeks
function populateDates() {
var select = document.getElementById("inputDate");
var currentWeek = new Date(2022, 7, 1);
var options = [];
// getting current date
var currentDate = new Date();
while (currentWeek < currentDate) {
// calc the date when the data for the current week will be generated
var weekEnd = new Date(currentWeek.getTime() + 6 * 24 * 60 * 60 * 1000);
// data generation 5 days after, ie friday
var dataGenerationDate = new Date(weekEnd.getTime() + 5 * 24 * 60 * 60 * 1000);
// check if date of data generation has passed
if (dataGenerationDate <= currentDate) {
var optionDate = new Date(currentWeek);
options.push({
date: optionDate.toISOString().slice(0, 10),
text: "Week of " + optionDate.toLocaleDateString("en-US", { month: 'long', day: 'numeric', year: 'numeric' })
});
}
// next Sunday
currentWeek.setDate(currentWeek.getDate() + 7);
}
options.sort(function(a, b) {
return b.date.localeCompare(a.date);
});
for (var i = 0; i < options.length; i++) {
var option = document.createElement("option");
option.value = options[i].date;
option.text = options[i].text;
select.appendChild(option);
}
// select the most recent option (which is the last week before the current date)
select.selectedIndex = 1;
// show the result text for the selected week
showResultText();
// generate the visualization for the selected week
handleDateSelected();
}
// showing result text for week
function showResultText() {
const dropdown = document.getElementById("inputDate");
const selectedOption = dropdown.options[dropdown.selectedIndex];
const resultWeek = `Top terms in headlines during the ${selectedOption.text}:`;
document.getElementById("resultWeek").textContent = resultWeek;
}
// get the start date for handleDateSelected()
function getSelectedWeekStartDate() {
const selectedDate = document.getElementById("inputDate").value;
const startDate = new Date(selectedDate);
return startDate
}
function handleDateSelected() {
const day = getSelectedWeekStartDate();
const selectedDate = new Date(day.setDate(day.getDate() + 1))
.toLocaleDateString('en-GB')
.split('/')
.reverse()
.join('');
// select DIVs
const leftTermsOnlyDiv = document.getElementById('left-only-terms');
const rightTermsOnlyDiv = document.getElementById('right-only-terms');
const sharedTermsDiv = document.getElementById('shared-terms');
// clear DIV contents
if (leftTermsOnlyDiv && rightTermsOnlyDiv && sharedTermsDiv) {
leftTermsOnlyDiv.innerHTML = '';
rightTermsOnlyDiv.innerHTML = '';
sharedTermsDiv.innerHTML = '';
}
// fetch data, render visualization
fetchData(selectedDate)
.then((data) => {
renderForWeek(selectedDate, data);
})
.catch((error) => {
console.error('Error fetching data:', error);
});
}
function cleanData(rawData) {
const cleanData = rawData.filter(r => r.term.length > 2) // skip small words
.sort((x, y) => d3.descending(x.count, y.count)) // sort by freq
.slice(0, config.maxTerms); // limit total terms
// we also want to normalize by the total number term usage
total = cleanData.map(r => r.count).reduce((a, b) => a + b, 0);
return cleanData.map(r => ({ ...r, tfnorm: r.count/total }));
}
function fontSizeComputer(term, extent, sizeRange){
const size = sizeRange.min + (((sizeRange.max - sizeRange.min)
* (Math.log(term.tfnorm) - Math.log(extent[0]))) / (Math.log(extent[1]) - Math.log(extent[0])));
return size;
};
async function fetchData(selectedDate) {
// fetchData (samples for now)
const rightCsvData = await d3.csv(`./data/${selectedDate}-top-right.csv`, d3.autoType);
const leftCsvData = await d3.csv(`./data/${selectedDate}-top-left.csv`, d3.autoType);
// clean the data and normalize
const rightData = cleanData(rightCsvData, config.maxTerms);
const leftData = cleanData(leftCsvData, config.maxTerms);
return { rightData, leftData };
}
function renderForWeek(selectedDate, data) {
const rightData = data.rightData;
const leftData = data.leftData;
// concat right and left data, normalize
const extent = d3.extent(rightData.concat(leftData), d => d.tfnorm);
const sizeRange = { min: config.minFontSize, max: config.maxFontSize };
const totalWidth = config.width;
// first split the data into left/both/right
const totalCount = leftData.map(r => r.count).reduce((a, b) => a + b, 0) + rightData.map(r => r.count).reduce((a, b) => a + b, 0);
// figure out venn diagram overlap
let leftTerms = leftData.map(d => d.term);
let rightTerms = rightData.map(d => d.term);
const bothTerms = leftTerms.filter(t => rightTerms.includes(t));
leftTerms = leftTerms.filter(t => !bothTerms.includes(t));
rightTerms = rightTerms.filter(t => !bothTerms.includes(t));
// re-normalize split data
const left = leftData.filter(d => leftTerms.includes(d.term)).map(d => ({...d, tfnorm:d.count/totalCount}));
const right = rightData.filter(d => rightTerms.includes(d.term)).map(d => ({...d, tfnorm:d.count/totalCount}));
const both = bothTerms.map(t => {
const leftItem = leftData.find(i => i.term == t);
const rightItem = rightData.find(i => i.term == t);
return {'term': t, 'count': leftItem.count + rightItem.count, tfnorm: (leftItem.count + rightItem.count)/totalCount};
})
.sort((a,b) => a.count < b.count);
// create three SVG elements
const rightSVG = d3.select("#right-only-terms").append("svg")
.attr("width", totalWidth/3)
.attr("height", config.height);
const leftSVG = d3.select("#left-only-terms").append("svg")
.attr("width", totalWidth/3)
.attr("height", config.height);
const sharedSVG = d3.select("#shared-terms").append("svg")
.attr("width", totalWidth/3)
.attr("height", config.height);
// labels for 3 viz
const leftLabel = leftSVG.append('g') // left
.attr("transform", "translate(0,20)")
leftLabel.append('line')
.style("stroke", "#333333")
.style("stroke-width", 1)
.attr("x1", 0)
.attr("y1", 10)
.attr("x2", totalWidth/3 - 20)
.attr("y2", 10);
leftLabel.append("text")
.attr("fill", '#333333')
.attr("font-weight", 900)
.attr("font-size", "16px")
.text("Unique to Media Mostly Shared by Democrats");
const bothLabel = sharedSVG.append('g') // both
.attr("transform", "translate(0,20)")
bothLabel.append('line')
.style("stroke", "#333333")
.style("stroke-width", 1)
.attr("x1", 0)
.attr("y1", 10)
.attr("x2", totalWidth/3 - 20)
.attr("y2", 10);
bothLabel.append("text")
.attr("fill", '#333333')
.attr("font-weight", 900)
.attr("font-size", "16px")
.text("In Media Shared by Both");
const rightLabel = rightSVG.append('g') // right
.attr("transform", "translate(0,20)")
rightLabel.append('line')
.style("stroke", "#333333")
.style("stroke-width", 1)
.attr("x1", 0)
.attr("y1", 10)
.attr("x2", totalWidth/3 - 20)
.attr("y2", 10);
rightLabel.append("text")
.attr("fill", '#333333')
.attr("font-weight", 900)
.attr("font-size", "16px")
.text("Unique to Media Mostly Shared by Republicans");
// word cloud
leftSVG.append('g') // left
.attr("transform", "translate(0,35)")
.node().appendChild(orderedWordCloud(totalWidth/3, left, '#333399', extent, 'left-top'));
sharedSVG.append("g") // both
.attr("transform", "translate(0,35)")
.node().appendChild(orderedWordCloud(totalWidth/3, both, '#800080', extent, 'both-top'));
rightSVG.append('g') // right
.attr("transform", "translate(0,35)")
.node().appendChild(orderedWordCloud(totalWidth/3, right, '#993333', extent, 'right-top'));
// combine all three SVG elements into a single selection
const allSVGs = d3.selectAll([leftSVG.node(), sharedSVG.node(), rightSVG.node()]);
// select all text elements from the combined selection
const terms = allSVGs.selectAll('text');
terms.on('click', function(event, d) {
// get start and end dates for the selected week
const formattedDate = `${selectedDate.slice(4, 6)}-${selectedDate.slice(6)}-${selectedDate.slice(0, 4)}`;
const endDateObj = new Date(new Date(formattedDate).getTime() + 7 * 24 * 60 * 60 * 1000);
const endDateStr = endDateObj.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '-');
// Parse the formatted date back into a JavaScript Date object
const parsedDate = new Date(
formattedDate.slice(6), // Year
parseInt(formattedDate.slice(0, 2)) - 1, // Month (subtract 1 as month is zero-based)
formattedDate.slice(3, 5) // Day
);
// Get one week from the parsed date
const oneWeekLater = new Date(parsedDate.getTime() + 7 * 24 * 60 * 60 * 1000);
// Format the one week later date as "MM-DD-YYYY"
const formattedOneWeekLater = `${
(oneWeekLater.getMonth() + 1).toString().padStart(2, '0')
}-${oneWeekLater.getDate().toString().padStart(2, '0')
}-${oneWeekLater.getFullYear()}`;
// Output the formatted one week later date
console.log(formattedDate)
console.log("One Week Later:", formattedOneWeekLater);
const url = `https://search.mediacloud.org/search?q=${encodeURIComponent(d.term)}&nq=&start=${encodeURIComponent(formattedDate)}&end=${encodeURIComponent(formattedOneWeekLater)}&p=onlinenews-mediacloud&ss=&cs=34412234%253EUnited%2520States%2520-%2520National&any=any`
console.log(url)
// open new tab with search for clicked term
window.open(url)
})
.on('mouseover', function() {
d3.select(this).style('cursor', 'pointer')
.style('font-weight', 'normal')
.style('font-size', d => (d.fontSize * 1.2) + 'px');
})
.on('mouseout', function() {
d3.select(this).style('cursor', 'default')
.style('font-weight', 'bold');
});
}
function getContext2d() {
return document.getElementById('font-helper-canvas').getContext('2d')
}
function listCloudLayout(wordNodes, width, extent, sizeRange) {
// change line below DOM.context2d, only in observable
const canvasContext2d = getContext2d();
let x = 0;
if (typeof (wordNodes) === 'undefined') {
return x;
}
wordNodes.attr('x', (d) => {
const fs = fontSizeComputer(d, extent, sizeRange);
canvasContext2d.font = `bold ${fs}px 'Source Sans Pro'`; // crazy hack for IE compat, instead of simply this.getComputedTextLength()
const metrics = canvasContext2d.measureText(d.term);
const textLength = metrics.width+4; // give it a little horizontal spacing between words
let lastX = x;
if (x + textLength + 10 > width) { // TODO: replace 10 with state property for padding
lastX = 0;
}
x = lastX + textLength + (0.5 * fs);
return lastX;
});
let y = -0.5 * sizeRange.max;
let lastAdded = 0;
wordNodes.attr('y', (d, index, data) => { // need closure here for d3.select to work right on the element
const xPosition = d3.select(data[index]).attr('x');
if (xPosition === '0') { // WTF does this come out as a string???!?!?!?!
const height = 1.2 * fontSizeComputer(d, extent, sizeRange);
y += height;
y = Math.max(y, height);
lastAdded = height;
}
return y;
});
return y + lastAdded;
};
function orderedWordCloud(theWidth, data, termColor, exent, id){
// setup the wrapper svg
const innerWidth = theWidth - (2 * config.padding);
const svg = d3.create("svg")
.attr('height', config.height)
.attr('width', theWidth)
.attr('id', id || 'ordered-word-cloud')
.attr('class', 'word-cloud');
// start height calculations
let y = config.height;
let wordNodes;
const wordListHeight = config.height - (2 * config.padding);
const wordWrapper = svg.append('g')
.attr('transform', `translate(${2 * config.padding},0)`);
const sizeRange = { min: config.minFontSize, max: config.maxFontSize };
const fullExtent = exent || d3.extent(data, d => d.tfnorm)
// start layout loop
while ((y >= wordListHeight) && (sizeRange.max > sizeRange.min)) {
wordNodes = wordWrapper.selectAll('text') // one text per term
.data(data, d => d.term)
.enter()
.append('text') // for incoming data
.attr('class', '')
.attr('fill', termColor)
.attr('font-family', 'Lato, Helvetica, sans')
.classed('word', true)
.classed('hide', d => d.display === false)
.classed('show', d => d.display !== false)
.classed('selected', d => d.term === config.selectedTerm)
.attr('font-size', d => fontSizeComputer(d, fullExtent, sizeRange))
.text(d => d.term)
.attr('font-weight', 'bold')
.on('mouseover', (d) => {
const { event } = d3;
d3.select(event.target).attr('fill', config.linkColor)
.attr('cursor', 'pointer');
})
.on('mouseout', () => {
const { event } = d3;
d3.select(event.target).attr('fill', config.textColor)
.attr('cursor', 'arrow');
});
// Layout
y = 0;
const leftHeight = listCloudLayout(wordNodes, innerWidth, fullExtent, sizeRange);
y = Math.max(y, leftHeight);
sizeRange.max -= 1;
}
// we need to return a DOM element for observable to render
return svg.node();
}