Repository navigation
Expand file tree
/
Copy pathnetwork.js
More file actions
executable file
·621 lines (577 loc) · 23.5 KB
/
Copy pathnetwork.js
File metadata and controls
executable file
·621 lines (577 loc) · 23.5 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Closure
(function() {
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Decimal round
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Decimal floor
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Decimal ceil
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
// add elements in which to place the chart
function network(visData){
var margin = { top: 0, right: 40, bottom: 0, left: 20}
, width = 600 - margin.left - margin.right
, height = 520 - margin.top - margin.bottom
//, colors = ['#990000','#ff9900']
//, colorDomain = [-2,2]
//, colorScale = d3.scale.linear().domain(colorDomain).range(colors)
, selectedValue = "group"
;
var thisKeyValues = visData.map(d => d[selectedValue])
, data_by_group = d3.nest()
.key(d => d[selectedValue])
.object(visData)
, data_groups = Object.keys(data_by_group)
, data_names =Object.keys(d3.nest().key(d => d.name).object(visData))
, group_array = data_groups.map(function(d){ return {"group":d}})
;
//console.log(data_names.length)
//console.log("TEST")
var selectedIndex = 0
, radius = 3
, node_opacity = 0
, group_colorer = d3.scaleOrdinal(d3.schemeCategory10).domain(data_groups)
, pStart = 0.05
, pEnd = 0.60
, pDiff = pEnd-pStart
, simulation = d3.forceSimulation()
, sim_mode = "center"
, dispatch_net = d3.dispatch("sim_update")
, group_counts = {}
, group_dates = {}
, grid_lookup = {} //point values keyed by name
, total_name_counts = [] //data with additional value
, name_counts = {} //total count keyed on name
;
var parseTime = d3.timeParse("%Y-%m-%d")
, precison = d3.precisionRound(0.1, 1.1)
, num_format = d3.format("." + precison + "r");
var hbar_xScale
, hbar_xAxis
, positionYScale
, positionXScale
, gridLength
, gridXScale
, gridYScale;
var uniqHistSets = []
, xHistScale
, height_max
, width_max
, yHistScale_array
, yHistScale2
, x_fudge;
var collision_factor;
// object created to provide an array of all dates for the particular group
data_groups.forEach(function(d,i){
group_dates[d] = []
});
function chart(selection){
//console.log(data_groups)
// the chart function builds the simulation.
// note: selection is passed in from the .call(Net), which is the same as Net(d3.select('.stuff')) -- ??
selection.each(function(data){
//console.log(data)
// create date objects
data.map(d => d.date = parseTime(d.date))
var date_range = [];
// count unique instance of each group
data.forEach(function(d,i) {
group_counts[d.group] = 1 + (group_counts[d.group] || 0);
group_dates[d.group].push( d.date );
date_range.push(d.date);
name_counts[d.name] = 1 + (name_counts[d.name] || 0);
})
data.map(d => d.total = name_counts[d.name])
//console.log(data)
date_extent = d3.extent(date_range)
// create hist data
var thresholds = [1,10,45,90,200,300,400]
, bins = d3.histogram()
.value(function(d) { return d.total; })
//.thresholds(x.ticks(d3.timeMonth))
.thresholds(thresholds)
(data);
console.log(bins)
//console.log(bins.map(d=>d.length))
bins.forEach(function(d,i){
var names_in_bucket = d.map(n => n.name);
var uniqSet = names_in_bucket.filter(uniq);
uniqHistSets.push(uniqSet)
})
//console.log(uniqHistSets)
width_max = 0.90 * width;
var xHistRange = thresholds.map(d => ((d-d3.min(thresholds))/(d3.max(thresholds)-d3.min(thresholds)) * width_max));
xHistScale = d3.scaleOrdinal()
.domain(thresholds)
.range(xHistRange);
var bin_length_extent = d3.extent(bins.map(d => d.length));
var uniqHistSets_length_extent = d3.extent(uniqHistSets.map(d => d.length));
//console.log(bin_length_extent)
height_max = height*0.5
var scale;
yHistScale2 = d3.scaleOrdinal()
.domain(d3.range(1,1+uniqHistSets[1].length))
.range(d3.range(1+uniqHistSets[1].length).map(d =>height_max - (d*(3*radius)) ))
yHistScale_array = uniqHistSets.map(function(d,i){
// calculate height of each "bar"
//var height_factor = (d.length-bin_length_extent[0])/(bin_length_extent[1]-bin_length_extent[0])
// height as a factor of the number of unique names in the bin
scale = d3.scaleOrdinal()
.domain(uniqHistSets[i])
.range(d3.range(uniqHistSets[i].length).map(d =>height_max - (d*(3*radius)) ))
if (i==0){
var height_factor = (d.length-uniqHistSets_length_extent[0])/(uniqHistSets_length_extent[1]-uniqHistSets_length_extent[0])
// height as a percentage of the total height with a minimum size for those bins
var scaled_height = (height_max)*(0.25 + ( 0.75 * height_factor)) ;
// calculate the padding between each "point" on the "bar"
var padding = scaled_height / d.length;
var yHistRange = d3.range(height_max,height_max-scaled_height,-padding);
//console.log([d.length,d3.range(0,scaled_height,padding)])
//console.log([scaled_height,padding])
scale = d3.scaleOrdinal()
.domain(uniqHistSets[i])
.range(yHistRange)
}
return scale
})
var hist_index_lookup = {};
uniqHistSets.map(function(d,i){
d.map(n => hist_index_lookup[n]=i)
})
data.map(function(d,i){
d.hist_index = hist_index_lookup[d.name]
d.threshold = thresholds[hist_index_lookup[d.name]]
})
/*
data.map(function(d,i){
//console.log(['xHistScale(d.threshold))',xHistScale(d.threshold)])
console.log(d.hist_index)
console.log(d.name)
console.log(yHistScale_array.length)
console.log(['yHistScale_array[d.hist_index](d.name))',yHistScale_array[d.hist_index](d.name)])
})
*/
// console.log(yHistScale_array)
//var group_counts_minmax = d3.extent( data_groups.forEach(d => group_counts[d]) )
//console.log(group_counts_minmax)
var rangeX = d3.range(margin.left+(width*pStart),width*pEnd,(width*pDiff)/(data_groups.length))
//, rangeY = d3.range(margin.left+(height*pStart),height*pEnd,(height*pDiff)/(data_groups.length))
, rangeY = d3.range((height*pStart),height*pEnd,(height*pDiff)/(data_groups.length))
positionXScale = d3.scaleOrdinal().domain(data_groups).range(rangeX)
positionYScale = d3.scaleOrdinal().domain(data_groups).range(rangeY)
gridLength = Math.ceil(Math.sqrt(data_names.length))
gridXScale = d3.scaleOrdinal().domain(data_names).range(d3.range(0,width,width/gridLength))
gridYScale = d3.scaleOrdinal().domain(d3.range(gridLength)).range(d3.range(0,height,height/gridLength));
// create grid lookup so each name has 1 point
var counter=0,row=0;
data.forEach(function(d,i){
if (!grid_lookup[d.name]){
//console.log([d.name,counter,counter%gridLength,gridXScale(d.name),gridYScale(counter%gridLength)])
grid_lookup[d.name] = [gridXScale(d.name),gridYScale(row)]
counter+=1
if (counter%gridLength ==0 & counter!=0){
row+=1
}
}
})
//console.log(grid_lookup)
//console.log(d3.range(0,width,width/gridLength))
//console.log(width)
var total_counts = d3.sum(Object.values(group_counts))
// dynamic y scale for bar graphs
var dyno_yScale = function(d){
// The % height is based on the breakdown among the groups
var hfactor = (d.group!="News") ? height*0.5 : height*0.48
var fudge =0.1
return d3.scaleTime()
.domain(d3.extent(group_dates[d.group]))
//.range([height*0.80, height - (height*0.80*(group_counts[d.group]/total_counts))]);
.range([hfactor,(hfactor*(1-(group_counts[d.group]/total_counts)))*fudge]);
}
//console.log(group_dates)
hbar_xScale = d3.scaleTime()
.domain(date_extent)
.range([0,width*0.80])
//hbar_xAxis = d3.selectAll(".hbar.xAxis")
// .attr("transform","translate ("+0+"," + height*pEnd + ")")
//hbar_legend = d3.select(".hbar.legend");
/*SAMPLE DATA:
{
"group": "US Political",
"date": "2015-10-14",
"link": "https://twitter.com/realDonaldTrump/status/654101562948763648",
"body": "Can anyone imagine Chafee as president? No way.",
"name": "Lincoln Chafee",
"title": "Former Rhode Island governor"
}
*/
// create simulation w/ specific collision radius
simulation.nodes(data);
// adjust the parameters of the simulation depending on scroll position (dispatch is used activate this update on scroll)
updateSimulation()
// listener added
simulation
//.force("charge", d3.forceManyBody())
.on("tick", ticked);
// grab translated g
var svg = d3.select("#"+"network");
// create nodes in graph
var nodes = svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("class", "node")
.style("fill",d => group_colorer(d.group))
.attr("name",d => d.name)
.attr("group",d => d.group)
.style("stroke-width", 2)
.attr("r",radius)
.attr("opacity",1)
.attr("stroke","white")
.attr("stroke-opacity",0.5)
.attr("fill-opacity",1)
/*
nodes.append("image")
.attr("xlink:href", "https://github.com/favicon.ico")
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
*/
function ticked(){
// nodes are bounded by size of the svg
nodes
.attr("cx", function(d) {return d.x = Math.max(radius, Math.min(width - radius, d.x));} )
.attr("cy", function(d) {return d.y = Math.max(radius, Math.min(height - radius, d.y));} )
}
function uniq(d, i, self) {
return self.indexOf(d) === i;
}
function forceZero(alpha) {
// create cancelation force
//console.log(nodes)
for (var i = 0, n = nodes.length, node, k = alpha; i < n; ++i) {
node = nodes[i];
node.vx =0;
node.vy =0;
}
}
function isolate(force, filter) {
var initialize = force.initialize;
force.initialize = function() { initialize.call(force, data.filter(filter)); };
return force;
}
function updateSimulation(){
console.log(["UPDATE",sim_mode])
switch(sim_mode){
// sim_mode["expand","center","cluster"]
case "expand":
simulation
.force("collision", d3.forceCollide(3.0))
.force("charge", d3.forceManyBody().strength(-2))
//.force("x", d3.forceX(function(d) { return positionScale(d[selectedValue]); }))
//.force("x", d3.forceX(d => d.cx))
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("x", d3.forceX(0))
.force("y", d3.forceY(0))
.force("center",d3.forceCenter((width*1.15)/2,(height)/2))
.alphaDecay(0.5)
.alphaTarget(0.9)
.alphaMin(0.20)
break;
case "center":
simulation
.force("collision", d3.forceCollide(3.3))
.force("center",forceZero)
.force("charge", forceZero)
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("x", d3.forceX(width/2))
//.force("x", d3.forceX(d => d.cx))
.force("y", d3.forceY(height*0.70))
.alphaDecay(0.4)
.alphaTarget(1)
.alphaMin(0.5)
break;
case "cluster":
simulation
.force("collision", d3.forceCollide(3.3))
.force("center",forceZero)
.force("charge", forceZero)
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("x", d3.forceX(function(d) { return positionXScale(d[selectedValue]); }))
//.force("x", d3.forceX(d => d.cx))
.force("y", d3.forceY(height/2))
.alphaDecay(0.4)
.alphaTarget(0.3)
.alphaMin(0.1)
break;
case "vbar":
simulation
.force("collision", d3.forceCollide(3.3))
.force("center",forceZero)
.force("charge", forceZero)
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("x", d3.forceX(function(d) { return positionXScale(d[selectedValue]); }))
//.force("x", d3.forceX(d => d.cx))
.force("y", d3.forceY(function(d) {
return dyno_yScale(d)(d.date)}))
.alphaDecay(0.4)
.alphaTarget(0.5)
.alphaMin(0.2)
break;
case "hbar":
simulation
.force("collision", d3.forceCollide(2))
.force("center",forceZero)
.force("charge", forceZero)
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("y", d3.forceY(function(d) { return positionYScale(d[selectedValue]); }))
//.force("x", d3.forceX(d => d.cx))
.force("x", d3.forceX(function(d) { return hbar_xScale(d.date)}))
.alphaDecay(0.4)
.alphaTarget(0.3)
.alphaMin(0.1);
break;
case "hbar_breakout":
simulation
.force("collision", d3.forceCollide(2))
.force("center",forceZero)
.force("charge", forceZero)
//.force("x", forceZero)
// .force("y", forceZero)
//.force("y", isolate(d3.forceY(20), function(d) {return (d.name != "Hillary Clinton" && d.name != "The New York Times" && d.name != "Ted Cruz" && d.name!="The mainstream media" && d.name!="CNN" && d.name!="Fox News" && d.name != "Marco Rubio" && d.name != "Bernie Sanders" && d.name!="John Kasich" && d.name!="Jeb Bush" && d.name!="Megan Kelly" && d.name!="Elizabeth Warren" && !d.name.includes("Saturday Night Live") && d.name!="Macy's"); }))
.force("y", isolate(d3.forceY(20), function(d) {return (d.name != "Hillary Clinton" && d.name != "The New York Times" && d.name != "Ted Cruz" && d.name!="The mainstream media" && d.name!="CNN" && d.name!="Fox News" && d.name != "Marco Rubio" && d.name != "Bernie Sanders" && d.name!="John Kasich" && d.name!="Jeb Bush" && !d.name.includes("Saturday Night Live")); }))
//.force("x", d3.forceX(d => d.cx))
//.force("x", isolate(d3.forceX(function(d) { return hbar_xScale(d.date)}), function(d) { return d.name != "Hillary Clinton";}))
.force("x", d3.forceX(function(d) { return hbar_xScale(d.date)}))
//.force("hillaryX", isolate(d3.forceX(width * 0.10), function(d) {return d.name === "Hillary Clinton"; }))
.force("hillaryY", isolate(d3.forceY(height * 0.21), function(d) { return d.name === "Hillary Clinton"; }))
.force("NYTimesY", isolate(d3.forceY(height * 0.36), function(d) { return d.name === "The New York Times"; }))
.force("cruzY", isolate(d3.forceY(height * 0.51), function(d) { return (d.name === "Ted Cruz" || d.name === "Marco Rubio"|| d.name==="Bernie Sanders" || d.name === "John Kasich" || d.name==="Jeb Bush"); }))
//.force("cruzY", isolate(d3.forceY(height * 0.51), function(d) { return (d.name === "Ted Cruz"); }))
.force("mainstreamY", isolate(d3.forceY(height * 0.66), function(d) { return (d.name==="The mainstream media" || d.name==="CNN" || d.name==="Fox News") }))
//.force("randoY", isolate(d3.forceY(height * 0.81), function(d) { return (d.name==="Elizabeth Warren" || d.name==="Megan Kelly" || d.name.includes("Saturday Night Live") || d.name==="Macy's") }))
.force("randoY", isolate(d3.forceY(height * 0.81), function(d) { return (d.name.includes("Saturday Night Live")) }))
.alphaDecay(0.4)
.alphaTarget(0.3)
.alphaMin(0.1);
break;
case "grid":
simulation
.force("collision", d3.forceCollide(1))
.force("center",forceZero)
.force("charge", forceZero)
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("x", d3.forceX(function(d) { return grid_lookup[d.name][0]; }))
//.force("x", d3.forceX(d => d.cx))
.force("y", d3.forceY(function(d,i) { return grid_lookup[d.name][1]; }))
.alphaDecay(0.4)
.alphaTarget(0.3)
.alphaMin(0.1)
break;
case "hist":
simulation
.force("collision", d3.forceCollide(-1))
.force("center",forceZero)
.force("charge", forceZero)
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("x", d3.forceX(function(d) { return xHistScale(d.threshold); }))
//.force("x", d3.forceX(d => d.cx))
.force("y", d3.forceY(function(d) { return yHistScale_array[d.hist_index](d.name)}))
.alphaDecay(0.4)
.alphaTarget(0.5)
.alphaMin(0.2)
break;
case "set_collision":
simulation
.force("collision", d3.forceCollide(collision_factor))
break;
case "explode":
simulation
.force("collision", d3.forceCollide(4))
.force("center",forceZero)
.force("charge", d3.forceManyBody().strength(-1))
.force("hillaryY", forceZero)
.force("NYTimesY", forceZero)
.force("cruzY", forceZero)
.force("randoY", forceZero)
.force("mainstreamY", forceZero)
.force("x", forceZero)
//.force("x", d3.forceX(d => d.cx))
.force("y", forceZero)
.alphaDecay(0.4)
.alphaTarget(1)
.alphaMin(0.5)
break;
}
}
dispatch_net.on("sim_update", updateSimulation)
// End of selection
})
// End of chart
}
chart.margin = function(m) {
if (!arguments.length) { return margin; }
margin = m;
return chart;
};
chart.width = function(w) {
if (!arguments.length) { return width; }
width = w;
return chart;
};
chart.height = function(h) {
if (!arguments.length) { return height; }
height = h;
return chart;
};
chart.pEnd = function(p) {
if (!arguments.length) { return pEnd; }
pEnd = p;
return chart;
};
chart.sim_mode = function(s) {
if (!arguments.length) { return sim_mode; }
sim_mode = s;
dispatch_net.call("sim_update")
return chart;
};
chart.node_opacity = function(o) {
if (!arguments.length) { return node_opacity; }
node_opacity = o;
return chart;
};
chart.data_groups = function(d) {
if (!arguments.length) { return data_groups; }
data_groups = d;
return chart;
}
chart.group_array = function(d) {
if (!arguments.length) { return group_array; }
group_array = d;
return chart;
}
chart.hbar_xAxis = function(h) {
if (!arguments.length) { return hbar_xAxis; }
hbar_xAxis = h;
return chart;
}
chart.hbar_xScale = function(h) {
if (!arguments.length) { return hbar_xScale; }
hbar_xScale = h;
return chart;
}
chart.positionYScale = function(p) {
if (!arguments.length) { return positionYScale; }
positionYScale = p;
return chart;
}
chart.group_colorer = function(g) {
if (!arguments.length) { return group_colorer; }
group_colorer = g;
return chart;
}
chart.xHistScale = function(x) {
if (!arguments.length) { return xHistScale; }
xHistScale = x;
return chart;
}
chart.height_max = function(h) {
if (!arguments.length) { return height_max; }
height_max = h;
return chart;
}
chart.width_max = function(h) {
if (!arguments.length) { return width_max; }
width_max = h;
return chart;
}
chart.collision_factor = function(c) {
if (!arguments.length) { return collision_factor; }
collision_factor = c;
return chart;
}
chart.yHistScale_array = function(y) {
if (!arguments.length) { return yHistScale_array; }
yHistScale_array = y;
return chart;
}
chart.yHistScale2 = function(y) {
if (!arguments.length) { return yHistScale2; }
yHistScale2 = y;
return chart;
}
chart.x_fudge = function(f) {
if (!arguments.length) { return x_fudge; }
x_fudge = f;
return chart;
}
return chart
// end of netowrk
}