-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeystone.js
More file actions
executable file
·421 lines (349 loc) · 15.5 KB
/
keystone.js
File metadata and controls
executable file
·421 lines (349 loc) · 15.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
/*
Todo
Part 1 todo:
3rd color intreset groups
put democratrs on top of republicans in both the chord diagram and bar chart
fix tool tip -
make background lighter/clearer
change language so it's just the name and the percent
add border to subgroup box
remove black line from bar chart and change order
align money to the right... make it $20k instead of $20,00
input all of subgroups from the excel sheet
Pac/Non Pac Switch View
Joe Rowley
Keystone JS
Arcs on outside of circle
Chrods on inside of circle
Arcs --> groups in D3 Lingo
Arc length is determined by aggregating one complete row of the matrix
Width of cord is determined by location in the matrix
A lot of this code comes from Steven Hall
*/
//*******************************************************************
// Create Matrix and Map
//*******************************************************************
//var matrix;
d3.csv('yesNoMany.csv', function (error, data) {
var mpr = chordMpr(data);
// mpr.addToMap('conMem','count')
mpr.
addValuesToMap('conMem','conMem')
.addValuesToMap('doner','doner')
//.addValuesToMap('party', 'party')
.setFilter(function (row, a, b) {
return (row.doner === a.name && row.conMem === b.name ) ||
(row.doner === b.name && row.conMem === a.name );
})
.setAccessor(function (recs, a, b) {
if (!recs[0]) return 0;
// console.log(recs[0]);
// console.log(recs.length);
//return recs.length;
//return +recs[0].count;
return { count: recs[0].count, valueOf: value}
});
function value() { return +this.count; }
matrix = mpr.getMatrix();
drawChords2(mpr.getMatrix(), mpr.getMap());
});
var chordFill2 = d3.scale.ordinal()
.domain(d3.range(5))
.range(["#e9a3c9", "#fde0ef", "#f7f7f7", "#e6f5d0","#a1d76a"]);
/*
#c51b7d
#e9a3c9
#fde0ef
#f7f7f7
#e6f5d0
#a1d76a
#4d9221
*/
//*******************************************************************
// DRAW THE CHORD DIAGRAM
//*******************************************************************
function drawChords2 (matrix, mmap) {
var w = 700, h = 700, r1 = h / 2, r0 = r1 - 100;
var fill = d3.scale.ordinal()
.range(['#c7b570','#c6cdc7','#335c64','#768935','#507282','#5c4a56','#aa7455','#574109','#837722','#73342d','#0a5564','#9c8f57','#7895a4','#4a5456','#b0a690','#0a3542',]);
var chord = d3.layout.chord()
.padding(.01)
//.sortGroups()
//.sortSubgroups(d3.ascending)
.sortGroups(
/*
democrats = 26754682
repubs = 58424112
function d3_ascending(a, b) {
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
}
*/
//console.log(d3.ascending);/
//d3.ascending
function(a,b){
//console.log("A: ", a, "B: ", b);
//if(a==1558488)return 2;
//else
//console.log(d3.ascending);
//console.log("B: ", b);
// A hack so that democrats and republicans
// are right next to each other because
// as it turns out, fossil fuel energy industry
// gives more money than democrats recieve.
var fossVal = 26928999;
var demoVal = 26754682;
var repubVal = 58424112;
//console.log(a,b);
if((a == fossVal)&&(b == demoVal)) return -1;
if((b == demoVal)&&(a == repubVal)) return -1;
if(a<b) {
// console.log(-1);
return -1;
}else if (a>b) {
// console.log(1);
return 1;
}else if(a>=0) {
// console.log(0);
return 0;
}else {
// console.log(NaN);
return NaN;
}
//return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
//console.log(d3.ascending);
}
)
//.sortSubgroups(function(a,b) { return 1; })
//.sortChords(d3.ascending)
//.sortGroups(d3.descending);
chord.sortSubgroups(
d3.descending
// function(a,b){
// //console.log("A: ", a, "B: ", b);
// //if(a==1558488)return 2;
// //else
// //console.log(d3.ascending);
// //console.log("B: ", b);
// // A hack so that democrats and republicans
// // are right next to each other because
// // as it turns out, fossil fuel energy industry
// // gives more money than democrats recieve.
// console.log(a,b);
// //return d3.ascneding
// //return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
// //console.log(d3.ascending);
// }
);
// console.log(chord.groups)
var arc = d3.svg.arc()
.innerRadius(r0)
.outerRadius(r0 + 20);
var svg = d3.select("#chartBox2").append("svg:svg")
.attr("width", w)
.attr("height", h)
.attr("id","chordDiagram2")
.append("svg:g")
.attr("id", "circle2")
.attr("transform", function(){
var trans= "translate(" + w / 2 + "," + h / 2 + ")";
var trans2 = "scale(-1,-1)";
var trans3= trans + trans2;
return trans3;
})
svg.append("circle2")
.attr("r", r0 + 20);
var rdr = chordRdr(matrix, mmap);
chord.matrix(matrix);
//console.log(chord.matrix(matrix).groups);
var g = svg.selectAll("g.group")
.data(chord.groups())
.enter().append("svg:g")
.attr("class", "group")
.on("mouseover", mouseover2)
.on("mouseout", mouseout2);
g.append("svg:path")
.style("stroke", "black")
.style("fill", function(d) {
//console.log(rdr(d));
var fillColor;
//console.log(rdr(d).gname);
if(rdr(d).gname=="Yes") return "#c51b7d";
else if (rdr(d).gname=="No") return "#4d9221";
return rdr(d).gdata == "conMem" ? "#f7f7f7": "grey";
//return "purple";
})
.attr("d", arc);
g.append("svg:text")
.each(function(d) {
d.angle = (d.startAngle + d.endAngle) / 2;
//d.party = "joe";
})
.attr("dy", ".35em")
.style("font-family", "helvetica, arial, sans-serif")
.style("font-size", "12px")
.attr("id", function(d){
return "text"+d.index;
})
.attr("text-anchor", function(d) {
return d.angle < Math.PI ? "end" : null;
})
// Space out all the text
.attr("transform", function(d) {
var trans = "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
+ "translate(" + (r0 + 26) + ")" // pushes it out to a certain radius
+ (d.angle > Math.PI ? "rotate(180)" : "");
var yUp = 280;
//if(d.angle<.12) yUp = 280 + (d.value/89000 )*3;
/*
89000
435900
595292
1518891
*/
if(d.value<89052) yUp += 0;
else if(d.value<435905) yUp += 10;
else if(d.value<595293) yUp += 20;
else if(d.value<1518892) yUp += 30;
var xTrans = Math.sin(d.angle)*280;
var yTrans = -Math.cos(d.angle)*yUp;
//console
var trans2 = "translate("+xTrans+","+ yTrans+")"
var trans3 = " scale(-1, -1)";
var allTrans = trans2 + trans3;
return allTrans;
// return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
// + "translate(" + (r0 + 26) + ")"
// + (d.angle > Math.PI ? "rotate(180)" : "");
})
.style("visibility",function(d){
if(d.value<1518892) return "hidden";
else return ""
})
.text(function(d) {
// if(rdr(d).gname.indexOf("&") > -1) {
// //d3.select("#text"+d.index).append("tspan");
// }
return rdr(d).gname;
});
// chord.sortSubGroups(d3.descending)
var chordPaths = svg.selectAll("path.chord")
.data(chord.chords())
.enter().append("svg:path")
.attr("class", "chord")
.style("stroke", "black")
.style("fill", function(d) {
var dd =rdr(d);
//var q = d3.round(num);
// Make a number between 0 and 4
if(dd.sname == "No") {
var num = dd.tvalue/dd.ttotal;
var num2 = 4*num;
var num3 = d3.round(num2);
// console.log(dd.tname, num, num3);
//console.log()
return chordFill2(num3);
}
else if(dd.sname == "Yes") {
var num = dd.tvalue/dd.ttotal;
var num2 = 4*num;
var num3 = d3.round(num2);
//console.log(dd.tname, num, num3);
// Reverse the number
return chordFill2(4-num3);
}
else return "#f7f7f7";
// return dd.tname == "Independent oil & gas producers" ? "#00592d": "#ff6200";
})
.attr("d", d3.svg.chord().radius(r0))
.on("mouseover", function (d) {
d3.select("#tooltip2")
.style("visibility", "visible")
.html(chordTip2(rdr(d)))
.style("top", function () { return (d3.event.pageY - 170)+"px"})
.style("left", function () { return (d3.event.pageX - 100)+"px";})
})
.on("mouseout", function (d) { d3.select("#tooltip22").style("visibility", "hidden") });
function chordTip2 (d) {
var p = d3.format(".1%"), q = d3.format(",f")
//console.log(d);
//console.log(d.tname);
var msg= "<strong>Contribution Info:</strong><br/>"
+ d.tname + " to " + d.sname +"s"
+ ": $" + q(d.tvalue) + "<br/>"
+ p(d.tvalue/d.ttotal) + " of " + d.tname + "'s Total ($" + q(d.ttotal) + ")<br/>"
+ p(d.tvalue/(d.mtotal/2)) + " of Total Donated($" + q(d.mtotal/2) + ")";
return msg;
}
function groupTip2 (d) {
// console.log( d);
var p = d3.format(".1%"), q = d3.format(",f")
// return "Group Info:<br/>"
// + d.gdata
// + "<br/>"
// + d.gname + " : " + q(d.gvalue) + "<br/>"
// + p(d.gvalue/(d.mtotal/2)) + " of Matrix Total (" + q(d.mtotal/2) + ")"
return d.gname + "s: $" + q(d.gvalue) + "<br/>"
+ p(d.gvalue/(d.mtotal/2)) + " of Total ($" + q(d.mtotal/2) + ")"
}
function fillinfoBox2 (d) {
var generalCongressInfo ="<h3>House of Representatives Make Up</h3>"
// console.log( d);
var p = d3.format(".1%"), q = d3.format(",f")
// Fill in all the sub groups and how much they donated here
// Maybe fill in special text for each one too.. idk.
var subGroups = {
"Fossil Fuels": { "Coal mining": {"Republicans":"$1,797,895.00","Democrats":"$31,000.00"} , "Oil Production":{"Democrats":"$10","Republicans":"$15"} },
"Construction": {"ILBC":"$10","SUA":"$1000"},
"Unions": {"ILBC":"$10","SUA":"$1000"},
"Ideological Groups": {"dumb ":"$10","SUA":"$1000"},
"Transportation": {"cars":"$10","SUA":"$1000"},
"Environmental policy": {"ILBC":"$10","SUA":"$1000"},
"Business Associations": {"ILBC":"$10","SUA":"$1000"},
"Alternate energy production & services": {"ILBC":"$10","SUA":"$1000"},
"Nuclear energy": {"ILBC":"$10","SUA":"$1000"},
"Other": {"ILBC":"$10","SUA":"$1000"},
"Materials & Manufacturing": {"ILBC":"$10","SUA":"$1000"}
};
//_each
var rows = "";
var aGroup= subGroups[d.gname];
_.forEach(aGroup, function(n, key) {
console.log(key, n.Democrats)
rows += "<tr><td>"+ key +"</td><td>" + n.Democrats + "</td><td>" + n.Republicans + "</td></tr>";
});
var header = "<strong>" +d.gname + ":</strong> <br>";
var strGroups = "Subgroups: <table> <tr> <th>Group</th><th>Democrats</th><th>Republicans</th>" + rows + "</table>";
var strTotalDonate = "Contributed: $" + q(d.gvalue) + " (" + p(d.gvalue/(d.mtotal/2)) +" of total donations) <br>";
var msg = header + strTotalDonate + strGroups ;
return msg;
}
function mouseover2(d, i) {
d3.select("#tooltip2")
.style("visibility", "visible")
.html(groupTip2(rdr(d)))
.style("top", function () { return (d3.event.pageY - 80)+"px"})
.style("left", function () { return (d3.event.pageX - 130)+"px";})
d3.select("#infoBox2")
.style("visibility", "visible")
.html(groupTip2(rdr(d)))
d3.select("#infoBox2")
.style("visibility", "visible")
.html(fillinfoBox2(rdr(d)))
var textStringID = "#ext"+i;
d3.select(textStringID)
.style("visibility", "visible")
chordPaths.classed("fade", function(p) {
return p.source.index != i
&& p.target.index != i;
});
}
function mouseout2(d, i) {
d3.select("#tooltip2").style("visibility", "hidden")
//Only remove text labels for small tight groups
if(d.value<1518892) {
var textStringID = "#ext"+i;
d3.select(textStringID)
.style("visibility", "hidden")
}
}
}