-
Notifications
You must be signed in to change notification settings - Fork 1
/
campaignsJS.js
executable file
·1290 lines (1101 loc) · 38.3 KB
/
campaignsJS.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//initial variables
var height = 500;
var width = 1000;
var margin = 40;
var GenElectCand = ["Hillary Clinton", "Jill Stein", "Donald Trump", "Gary Johnson", "Evan McMullin"]
// Date range and parser
var startDate = new Date(2015,1,1);
var endDate = new Date(2016,12,1);
var parseTime = d3.timeParse("%d-%b-%y");
//initialize Y axis
var y = d3.scaleLinear()
.domain([0,0])
.range([margin,height-margin]);
// TIME SCALE - X AXIS
var timeScale = d3.scaleTime()
.domain([startDate,endDate])
.range([0,width-margin*2]);
// COLOR SCALE
var colors = d3.scaleOrdinal()
.range(["#ff2b02", "#5ec2ff", "#af57fd", "#70cf35", "#239572", "#df08fd", "#d4a7f5", "#d25a8d", "#d85d46", "#f508b4", "#798c20", "#ff1964", "#19d380", "#0fccd6", "#b0bf8d", "#cfba0c", "#be5bcf", "#f9a1a2", "#2b85e3", "#fda60d", "#92807e", "#4b8da0", "#b07652", "#c46e10", "#6c75fc", "#b6b6d7", "#e8ae61"]);
pieColors = d3.scaleOrdinal(d3.schemeCategory20);
// Axis units
var yAxisSelection = 'dollars';
var timeSelection = 'campaign';
//data
var expendData = expendDataMonthly;
var svg,
g,
labelg;
//initial variables
//var boxedpieheight = 150;
var boxedpiewidth = 850;
var boxedpiemargin = 40;
function initVis() {
//CANVAS
svg = d3.select("#mainChart")
.append("svg")
.attr("width", width + width/5)
.attr("height", height + 15);
g = svg.append("g")
.attr("transform", "translate("+margin*2+", 0)");
labelg = svg.append("g")
.attr("transform", "translate(0," +5+")");
// X AXIS + LABEL
g.append("g")
.attr("class", "axis")
.attr("class", "xaxis")
.attr("transform", "translate(0,"+(height-margin)+")")
.call(d3.axisBottom(timeScale)
.ticks(d3.timeMonth.every(2)));
svg.append("text")
.attr("class", "axis-label")
.attr("y", 495)
.attr("x",0 + (width / 2))
.style("text-anchor", "middle")
.text("Time");
// Y AXIS + LABEL
g.append("g")
.attr("class", "axis")
.attr("class", "yaxis")
.call(d3.axisLeft(y));
svg.append("text")
.attr("transform", "rotate(90)")
.attr("class", "axis-label")
.attr("id", "yaxisLabel")
.attr("y", -5)
.attr("x",0 + (500 / 2))
.style("text-anchor", "middle")
.text("Dollars");
//clipPath
g.append("clipPath")
.attr("id", "clipper")
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width-margin)
.attr("height", height-margin);
//Y Axis Selectors
svg.append("rect")
.attr("x", 35)
.attr("y", 8)
.attr("width", 50)
.attr("height", 18)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", "black")
.attr("class", "axisButtons")
.on("mousedown", function(){changeAxis('dollars');});
svg.append("text")
.attr("x", 60)
.attr("y", 21)
.attr("text-anchor", "middle")
.attr("class", "axisButtonText")
.text("Dollars")
.on("mousedown", function(){changeAxis('dollars');});
svg.append("rect")
.attr("x", 90)
.attr("y", 8)
.attr("width", 80)
.attr("height", 18)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", "black")
.attr("class", "axisButtons")
.on("mousedown", function(){changeAxis('percentages');});
svg.append("text")
.attr("x", 130)
.attr("y", 21)
.attr("text-anchor", "middle")
.attr("class", "axisButtonText")
.text("Percentages")
.on("mousedown", function(){changeAxis('percentages');});
//X Axis Selectors
svg.append("rect")
.attr("x", 197)
.attr("y", 8)
.attr("width", 97)
.attr("height", 18)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", "black")
.attr("class", "axisButtons")
.on("mousedown", function(){changeAxis('campaign');});
svg.append("text")
.attr("x", 245)
.attr("y", 21)
.attr("text-anchor", "middle")
.attr("class", "axisButtonText")
.text("Whole Campaign")
.on("mousedown", function(){changeAxis('campaign');});
//2016 button
svg.append("rect")
.attr("x", 300)
.attr("y", 8)
.attr("width", 40)
.attr("height", 18)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", "black")
.attr("class", "axisButtons")
.on("mousedown", function(){changeAxis('2016');});
svg.append("text")
.attr("x", 320)
.attr("y", 21)
.attr("text-anchor", "middle")
.attr("class", "axisButtonText")
.text("2016")
.on("mousedown", function(){changeAxis('2016');});
//General Election button
svg.append("rect")
.attr("x", 345)
.attr("y", 8)
.attr("width", 90)
.attr("height", 18)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", "black")
.attr("class", "axisButtons")
.on("mousedown", function(){changeAxis('general');});
svg.append("text")
.attr("x", 390)
.attr("y", 21)
.attr("text-anchor", "middle")
.attr("class", "axisButtonText")
.text("General Election")
.on("mousedown", function(){changeAxis('general');});
}
//RENDER
function renderVis(plotData) {
//find the max value in the whole nested array
maxValue = d3.max(plotData, function(d){
//function(g) returns the values of nested array (total expend on a day)
return d3.max(d.values, function(g){ return +g.value; });
});
//New Y Scale
var newY = d3.scaleLinear()
.domain([(maxValue *1.1),0])
.range([margin,height-margin]);
//rescale Y Axis and check features
featuresRender();
//function to construct each line in the path
//d.key and d.value are time and expenditure values from the .nest() structure
var line = d3.line()
.curve(d3.curveMonotoneX)
.x(function(d) {return timeScale(parseTime(d.key));})
.y(function(d) {return newY(+d.value);});
//data join with key from nest() as key
var spendLines = g.selectAll('.spendLine').data(plotData, function(d) { return d.key;});
spendLines.exit()
.transition()
.duration(500)
.style("opacity", 0)
.remove();
//generate the paths;
//use line(d.values) to drill down to values of nested data
spendLines.enter()
.append("path")
.attr("class", "spendLine")
.attr("stroke", function(d) { return colors(d.key)})
.attr("stroke-width", 3)
.attr("clip-path", "url(#clipper)")
.attr("id", function(d){ return d.key.split(",", 1)[0] + "SpendLine";})
.attr("d", function(d) {return line(d.values);})
.on("mouseover", function(d){
var targetLabel = document.getElementById(d.key.split(",", 1)[0]+"SpendBackground").setAttribute("opacity", 1);
})
.on("mouseout", function(d){
var targetLabel = document.getElementById(d.key.split(",", 1)[0]+"SpendBackground").setAttribute("opacity", 0);
})
.style("opacity", 0)
.transition()
.duration(500)
.style("opacity", 1);
spendLines
.transition()
.duration(1000)
.attr("d", function(d){ return line(d.values);});
rescaleAxes(newY);
//LABELS DATA JOIN
var spendLabelBackground = labelg.selectAll('.spendLabelBackground').data(plotData, function(d) { return d.key;});
spendLabelBackground.exit().remove();
spendLabelBackground.enter()
.append("rect")
.attr("x", width + 7)
.attr("y", function(d,i){return 22*i - 3;})
.attr("width", 190)
.attr("height", 21)
.attr("class", "spendLabelBackground")
.attr("fill", "none")
.attr("stroke", "#222")
.attr("stroke-width", 2)
.attr("id", function(d){ return d.key.split(",", 1)[0] + "SpendBackground";})
.on("mouseover", function(d){
document.getElementById(d.key.split(",", 1)[0]+"SpendLine").setAttribute("stroke-width", 5);
})
.on("mouseout", function(d){
document.getElementById(d.key.split(",", 1)[0]+"SpendLine").setAttribute("stroke-width", 3);
})
.attr("opacity", 0);
spendLabelBackground
.transition()
.duration(500)
.attr("x", width + 7)
.attr("y", function(d,i){return 22*i - 3;});
var spendLabelRectangles = labelg.selectAll('.spendLabelRectangles').data(plotData, function(d) { return d.key;});
spendLabelRectangles.exit().remove();
spendLabelRectangles.enter()
.append("rect")
.attr("x", width + 10)
.attr("y", function(d,i){return 22*i;})
.attr("width", 15)
.attr("height", 15)
.attr("class", "spendLabelRectangles")
.attr("id", function(d){ return d.key.split(",", 1)[0] + "SpendRectangle";})
.attr("fill", function(d) { return colors(d.key);})
.on("mouseover", function(d){
document.getElementById(d.key.split(",", 1)[0]+"SpendLine").setAttribute("stroke-width", 5);
})
.on("mouseout", function(d){
document.getElementById(d.key.split(",", 1)[0]+"SpendLine").setAttribute("stroke-width", 3);
})
.attr("opacity", 0)
.transition()
.duration(500)
.attr("opacity", 1);
spendLabelRectangles
.transition()
.duration(500)
.attr("x", width + 10)
.attr("y", function(d,i){return 22*i;});
var spendLabels = labelg.selectAll('.spendLabels').data(plotData, function(d) { return d.key;});
spendLabels.exit().remove();
spendLabels.enter()
.append("text")
.attr("x", width + 35)
.attr("y", function(d,i){return 22*i + 13;})
.attr("class", "spendLabels")
.attr("id", function(d){ return d.key.split(",", 1)[0] + "SpendLabel";})
.on("mouseover", function(d){
document.getElementById(d.key.split(",", 1)[0]+"SpendLine").setAttribute("stroke-width", 5);
})
.on("mouseout", function(d){
document.getElementById(d.key.split(",", 1)[0]+"SpendLine").setAttribute("stroke-width", 3);
})
.text(function(d){return d.key;})
.attr("opacity", 0)
.transition()
.duration(500)
.attr("opacity", 1);
spendLabels
.transition()
.duration(500)
.attr("x", width + 35)
.attr("y", function(d,i){return 22*i + 13;});
}
function TotalCatExpend(subcat_data){
var totalspent = 0; //total spent by a candidate in
for (i=0; i<subcat_data.length; i++){
totalspent += subcat_data[i].value;
}
return totalspent;
//return {key: selections, value: totalspent};
}
function gettotalchecked(){
var count = document.querySelectorAll('input[type="checkbox"]:checked').length;
var featureid = document.getElementById("features");
if (featureid.checked == true){
count = count - 1;
}
return count;
}
function BoxedPie(subcat_data, filterOption, selections, Total_Selected_Expend, NoDataCount) {
var totalspent = 0;
//find the total expend in the nested array
var CandidateTotalExpend = d3.max(subcat_data, function(d){
totalspent += d.value;
return totalspent;
});
//scale to percent of total expenditure (scale boxes in x direction)
var CandBoxProportion = d3.scaleLinear()
.domain([0,CandidateTotalExpend])
.range([0,1]);
//scale in y direction
var CombinedTotalSpend = Total_Selected_Expend.reduce(function(a, b) { return a + b; }, 0); //sum up candidate totals for scaling
//what proportion of total selected candidates did they represent
var CandidateProportion = d3.scaleLinear()
.domain([0, CombinedTotalSpend])
.range([0, 1]);
//var TotalOfSelected = TotalCatExpend(Total_Selected_Expend, filterOption, selections)
//console.log(TotalOfSelected);
var totalchecked = gettotalchecked(); //get total checked
totalchecked = totalchecked - NoDataCount;
var CandTot = TotalCatExpend(subcat_data);
var boxedpieheight = CandidateProportion(CandTot)*100*(totalchecked);
var svg = d3.select('#pievis_container')
.append('svg')
.attr('width', boxedpiewidth)
.attr('height', 20+boxedpieheight)
.attr("y",function(d){
//var totalchecked = totalchecked(); //get total checked
return 50+boxedpieheight/100;
})
.attr('class', 'boxedpie')
.attr('id', selections + "_piegraph")
.append('g');
//initialize tooltips
var tip = d3.tip()
.attr("class", "d3-tip")
.html(function(d) { return (d.key)+"<br>"+ "$" + parseInt(d.value.toFixed(0)).toLocaleString();});
svg.call(tip);
//var div = d3.select("#pievis_container").append("div").attr("class", "toolTip");
//svg.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
//if i move this so slice and .append are diff lines, the tool tip stops working??
var piebox = svg.selectAll('#pievis_container').data(subcat_data, function(d) { return d.key; });
var boxloc = 0;
var runningtotal = 0;
piebox.enter()
.append('rect')
.attr("x", function(d,i){
runningtotal += CandBoxProportion(d.value); //running tally of how many boxes/% of total
boxloc = runningtotal - CandBoxProportion(d.value); //shift location of box to the left edge
return boxloc*600; })
.attr("y",function(){
//divide by 100 b/c y axis is backwards and higher #s => lower on svg
return boxedpieheight/100;
})
.attr("height", function(){
return boxedpieheight;
})
.attr("width", function(d){ return CandBoxProportion(d.value)*600; })
.attr('class',function(d) { return d.key;})
.attr('fill', function(d) {return pieColors(d.key);})
.attr('id', function(d){ return d.key + "_" + selections;})
.style('opacity', 0.7)
.on("mouseover", function(d){
tip.show(d);
//bold, magnify the legend text
var el = document.getElementById(d.key + "_pielegendtext");
el.style.fontWeight = 'bold';
el.style.textDecoration = 'underline';
el.style.fontSize = '115%';
var cl = document.getElementsByClassName(d.key);
for (i=0; i<cl.length; i++){
cl[i].style.opacity = '1';
}
//use the method above instead of the one below -- setattribute is less reliable according to Stackoverflow
//document.getElementById(d.key + "_pielegendtext").setAttribute("style","fontWeight=normal");
})
.on("mouseout", function(d){ tip.hide(d);
var el = document.getElementById(d.key + "_pielegendtext");
el.style.fontWeight = 'normal';
el.style.textDecoration = 'none';
el.style.fontSize = '100%';
var cl = document.getElementsByClassName(d.key);
for (i=0; i<cl.length; i++){
cl[i].style.opacity = '0.7';
}
}
);
//slice.transition().duration(1000);
piebox.exit()
.transition()
.style("opacity", 0)
.remove();
//label plot w/candidate name--add text after appending
// or mouseover legend will block out name when opacity is 1
svg.append("text")
.attr("x", 700)
.attr("y", function(){
if(boxedpieheight < 40){
return boxedpieheight + 12;
}
else{ return boxedpieheight/2;}
})
.attr("text-anchor", "middle")
.style("font-size", "14px")
//.style("opacity", 0.7)
//.style("text-decoration", "underline")
.text(function(){
var M = 1000000; //Million
var K = 1000; //thousands
console.log(CandTot)
if (Math.round(CandTot*10/M)/10 >= 1 && Math.round(CandTot/M) < 10){
//*10 /10 gets the decimal to the 10ths place for some reason
var PrintedAmt = Math.round(CandTot*10/M)/10;
return selections + " ($" + PrintedAmt + "M)";
}
else if (Math.round(CandTot/M) >= 10){
var PrintedAmt = Math.round(CandTot/M);
return selections + " ($" + PrintedAmt + "M)";
}
else if (Math.round(CandTot*10/K)/10 < 10){
//round to decimal if under 10k
var PrintedAmt = Math.round(CandTot*10/K)/10;
return selections + " ($" + PrintedAmt + "k)";
}
else if (Math.round(CandTot*10/K)/10 >= 10){
//round to integer if > 10k
var PrintedAmt = Math.round(CandTot/K);
return selections + " ($" + PrintedAmt + "k)";
}
else { return selections + " ($" + CandTot + ")"; }
});
var categorycount = pieColors.domain().length;
//console.log(pieColors.domain().length);
var legendbox = d3.select('#pievis_legend')
.attr('width', 240)
.attr('height', 30 * categorycount);
//console.log(pieColors.domain());
var legend = d3.select('#pievis_legend').selectAll('.pieLabel').data(pieColors.domain(), function(d){return d;});
legend.enter()
.append("rect")
.attr("class", "pieLabel")
.attr("x", 5)
.attr("y", function(d,i){ return 30*i;})
.attr('width', 20) //size of legend box
.attr('height', 20)
.attr('id',function(d) { return d + "_pielegend";}) //d not d.key b/c data here is piecolors.domain which is array of categories
// .attr('display', flex)
// .attr('flex-direction', column)
.style('fill', function(d){return pieColors(d)})
.style('stroke', function(d){return pieColors(d)});
legend.exit().remove();
var legendText = d3.select('#pievis_legend').selectAll('.pieLabelText').data(pieColors.domain(), function(d){return d;});
//pieColors.domain() = subcategories
legendText.enter().append('text')
.attr('x', 30)
.attr('y', function(d,i){return (30*i) + 15;}) //?????
.attr("class", "pieLabelText")
.attr('id',function(d) { return d + "_pielegendtext";})
.on("mouseover", function(d){
//on mouseover of category, ie minibox
//miniboxes = categories--individual squares w/in a candidate's plot
var miniboxes = document.getElementsByClassName(d);
var svgselect = d3.selectAll('.boxedpie'); //svg canvas
//if minibox DNE, then it needs to be 0 or the order gets off
//id of svg canvas
//for each box pie selected
var mini_id_cand = []
var SVGID_Cand = []
//get list of candidates for which we have boxes of a particular category for
for (j=0; j<miniboxes.length; j++){
var mini_id = miniboxes[j].getAttribute("id")
mini_id_cand.push(mini_id.split("_")[1])
}
//get list of candidates for which we have graphs for
for (i=0; i<svgselect.size(); i++){
var SVGID = svgselect.nodes()[i].id
SVGID_Cand.push(SVGID.split("_")[0])
}
//if equal number of candidates and category boxes, then every candidate has data for that category
if (mini_id_cand.length == SVGID_Cand.length)
{
for (i=0; i<SVGID_Cand.length; i++)
{
//get dim of category's box and make another rectangle on top that's a little bigger
var minih = miniboxes[i].getAttribute("height");
var miniw = miniboxes[i].getAttribute("width");
var minix = miniboxes[i].getAttribute("x");
var miniy = miniboxes[i].getAttribute("y");
var minifill = miniboxes[i].getAttribute("fill");
var highlightbox = d3.select(svgselect.nodes()[i])
.append('rect')
.attr('height', +minih + 8)
.attr('width', +miniw + 8)
.attr('x', minix)
.attr('y', miniy)
.attr('class', 'highlight')
.attr('fill', minifill);
}
}
else
{
//if a minibox DNE, a candidate doesn't have data, then need to set dim to 0 or the order gets off
for (i=0; i<SVGID_Cand.length; i++)
{
//check if candidate has a particular category
var index = mini_id_cand.indexOf(SVGID_Cand[i])
if (index >= 0) //indexOf returns -1 if not in array
{
var minih = miniboxes[index].getAttribute("height");
var miniw = miniboxes[index].getAttribute("width");
var minix = miniboxes[index].getAttribute("x");
var miniy = miniboxes[index].getAttribute("y");
var minifill = miniboxes[index].getAttribute("fill");
var highlightbox = d3.select(svgselect.nodes()[i])
.append('rect')
.attr('height', +minih + 8)
.attr('width', +miniw + 8)
.attr('x', minix)
.attr('y', miniy)
.attr('class', 'highlight')
.attr('fill', minifill);
}
else
{
var minih = 0;
var miniw = 0;
var minix = 0;
var miniy = 0;
var minifill = 'black';
var highlightbox = d3.select(svgselect.nodes()[i])
.append('rect')
.attr('height', +minih)
.attr('width', +miniw)
.attr('x', minix)
.attr('y', miniy)
.attr('class', 'highlight')
.attr('fill', minifill);
}
}
}
//document.getElementById(d).setAttribute("height", +document.getElementById(d).getAttribute("height") * 1.2);
//document.getElementById(d).setAttribute("height", +document.getElementById(d).getAttribute("width") * 1.2);
})
.on("mouseout", function(d){
//get element by id only selects first one -- we want to highlight all when hover over legend
var miniboxes = document.getElementsByClassName(d);
var selectmini = d3.selectAll('.'+d)
for(i=0; i<miniboxes.length; i++){
var miniselect = d3.selectAll('.highlight');
for (i=0; i<miniselect.size(); i++){
miniselect.nodes()[i].remove();
}
}
//document.getElementById(d).setAttribute("style","opacity: 0.5");
})
.text(function(d) { return d;});
legendText.exit().remove();
}
function getChecked(){
//get candidate selections
var candBoxes = document.getElementsByClassName("renderBox");
var selections = [];
for (i=0; i<candBoxes.length; i++) {
if (candBoxes[i].checked){
selections.push(candBoxes[i].value);
}
}
//get party selections
var partyBoxes = document.getElementsByClassName("partyBox");
partySelections = [];
for (i=0; i<partyBoxes.length; i++) {
if (partyBoxes[i].checked){
partySelections.push(partyBoxes[i].value);
}
}
//get category selection
var filterOption = document.getElementById("filterSelect").value;
return {selections: selections, partySelections: partySelections, filterOption: filterOption};
}
function removePieChart(selections){
var DoesPieExist = !!document.getElementById(selections + "_piegraph")
if (DoesPieExist == true){
//if pie chart exists
//select svg element by id and delete it
document.getElementById(selections + "_piegraph").remove();
}
else{
return;
}
}
var Total_Selected_Expend =[];
function justChecked(BoxJustChecked){
//use this function for pie charts
//check if change was selecting another candidate or unchecking
var checkedID = document.getElementById(BoxJustChecked);
var filterOption = document.getElementById("filterSelect").value;
if (checkedID.checked == false){
removePieChart(BoxJustChecked);
}
//resize pie chart every time
filterChanged(filterOption)
}
function filterPartyPieData(selections, filterOption){
data = expendData.sort(function(a,b){ return parseTime(a.date) - parseTime(b.date);});
// filter on the selected category, unless total
if(filterOption != 'Total'){
data = data.filter(function(d){ return d.category == filterOption; });
}
if((selections == "Democratic Party") || (selections == "Republican Party"))
{
//filter for parties
var partyFilter = data.filter(function(d){
return selections.indexOf(d.party) > -1;
});
//IF NO DATA for a category, return false
if(partyFilter.length == 0){
console.log("no data")
return false;
}
//nest party data--creating party total expend data for sep party total line --allows us to select total
//data by party, sub category for pie chart
piepartyData = d3.nest()
.key(function(d){ return d.party;})
.key(function(d){ return d.category;})
.sortKeys(d3.ascending)
.key(function(d){ return d.label;})
.rollup(function(leaves){
var spendSum = d3.sum(leaves, function(g)
{
return g.expend;
});
return spendSum;
})
.entries(partyFilter);
}
//if selected totals --calculate total expenditures by summing up each subcategory and creating new JS obj
if (filterOption == "Total"){
var totaldata = CalcTotals(piepartyData);
var subcat_data = totaldata;
}
else{
//go thru candidate expenditures, nested by category and subcat--do not need for party level data
//get values
var subcat_data = GetSubcatValues(piepartyData, filterOption);
}
return {subcat_data: subcat_data, filterOption: filterOption, selections: selections}
}
function filterChanged(filterOption){
var Total_Selected_Expend =[]; //reset total counter
pieColors = d3.scaleOrdinal(d3.schemeCategory20);
//filter changed, update pie charts
//find which candidates are checked
var checked = getChecked();
var selections = checked.selections;
var partySelections = checked.partySelections;
//var filterOption = checked.filterOption;
var allchecked = selections.concat(partySelections);
//allchecked.push(selections)
//allchecked.push(partySelections)
var selectionswithdata = allchecked;
var CandidateTotalExpend;
var AllFilteredPieData = [];
var NoDataCount = 0;
d3.selectAll(".pieLabel").remove();
d3.selectAll(".pieLabelText").remove();
//iterate backwards because splicing will mess up indices b/c it will return array w/varying size
for (var i=selections.length-1; i >=0; i--){
removePieChart(selections[i]);
FilteredPieData = filterForPie(selections[i], [], filterOption);
//if no data, remove filter if one person selected, quit function
if (FilteredPieData == false){
selectionswithdata.splice(i,1); //remove candidate with no data
NoDataCount++;
alert("No data for " + selections[i] + " for " + filterOption);
//console.log(selectionswithdata.length)
//console.log(selectionswithdata)
//if if last candidate had no data, remove legend
if (selectionswithdata.length == 0){
//console.log('remove legend')
d3.selectAll(".pieLabel").remove();
d3.selectAll(".pieLabelText").remove();
//document.getElementById(selections[i]).checked = false;
}
continue;
}
else{
//has data, get total
CandidateTotalExpend = TotalCatExpend(FilteredPieData.subcat_data, FilteredPieData.selections)
Total_Selected_Expend.push(CandidateTotalExpend);
AllFilteredPieData.push(FilteredPieData);
//has data, create pie chart
//renderPie2(FilteredPieData.subcat_data, FilteredPieData.filterOption, FilteredPieData.selections);
//BoxedPie(FilteredPieData.subcat_data, FilteredPieData.filterOption, FilteredPieData.selections,Total_Selected_Expend);
}
}
for (var i=0; i<partySelections.length; i++){
removePieChart(partySelections[i]);
FilteredPieData = filterPartyPieData(partySelections[i], filterOption);
if (FilteredPieData == false){
return;
}
else{
CandidateTotalExpend = TotalCatExpend(FilteredPieData.subcat_data, FilteredPieData.selections)
Total_Selected_Expend.push(CandidateTotalExpend);
AllFilteredPieData.push(FilteredPieData);
//renderPie2(FilteredPieData.subcat_data, FilteredPieData.filterOption, FilteredPieData.selections);
//BoxedPie(FilteredPieData.subcat_data, FilteredPieData.filterOption, FilteredPieData.selections,Total_Selected_Expend);
}
}
//console.log(AllFilteredPieData)
//console.log(FilteredPieData.subcat_data)
for (var i=0; i<AllFilteredPieData.length; i++){
BoxedPie(AllFilteredPieData[i].subcat_data, AllFilteredPieData[i].filterOption, AllFilteredPieData[i].selections,Total_Selected_Expend, NoDataCount);
}
}
function GetSubcatValues(pieData, filterOption){
var subcat_data = [];
//if not total
//go thru candidate expenditures, nested by category and subcat--do not need for party level data
//get values
//can use pieData[0] here b/c newest added category is always on top/first in array
for(var i=0; i < pieData[0].values.length; i++){
if (pieData[0].values[i].key == filterOption){
for (var j=0; j < pieData[0].values[i].values.length; j++){
subcat_data.push(pieData[0].values[i].values[j]);
//console.log(pieData[0].values[i].values[j]);
}
}
}
return subcat_data;
}
function CalcTotals(pieData){
//pieData here is nested to subcategory level for party/candidate. eg to level of airfare within travel
//calculate total per category (eg travel) by summing each sub category for pie chart
var totaldata = [];
for (var i = 0; i < pieData[0].values.length; i++)
{
categorytotal = 0;
for (var j=0; j<pieData[0].values[i].values.length; j++){
//sum subcategory totals
categorytotal += pieData[0].values[i].values[j].value;
//need to reset category total and save as key
}
//totaldata[pieData[0].values[i].key] = categorytotal;
var cat_totaldata = {};
cat_totaldata["key"] = pieData[0].values[i].key;
//console.log(pieData[0].values[i].key);
//console.log(categorytotal);
cat_totaldata["value"] = categorytotal;
totaldata.push(cat_totaldata);
}
return totaldata;
}
function filterForPie(selections, partySelections, filterOption){
//console.log(filterOption);
//Sort by date - otherwise lines double back on eachother
//because dates are plotted out of order
data = expendData.sort(function(a,b){ return parseTime(a.date) - parseTime(b.date);});
// filter on the selected category, unless total
if(filterOption != 'Total'){
data = data.filter(function(d){ return d.category == filterOption; });
}
//filter - keep if d.cand_name has an index in array of selected candidates
var filteredData = data.filter(function(d){
return selections.indexOf(d.cand_name) > -1;
});
//IF NO DATA for a category, return false
if(filteredData.length == 0){
return false;
}
var filteredData = filteredData.filter(function(d){ return d.expend != 0;});
var pieData = [];
//console.log(pieData.length);
//using data log of every expense, take the selection and find the average expenses per sub category per label for selected candidate
//**GET DATA FOR PIE CHART (PARTY LEVEL STUFF ABOVE)
pieData = d3.nest() //takes json data and nests it by groups
.key(function(d){ return d.cand_name; }) //key is a function that goes with nest--group data by candidate first so we don't have to
//search thru each row every time we change candidate and can instead pull out a candidate's chunk
.key(function(d){return d.category;})
.sortKeys(d3.ascending)
.key(function(d){return d.label;}) //pie chart data
.rollup(function(leaves){ //.rollup accesses the tree values/leaves for the purpose of summing up
var spendSum = d3.sum(leaves, function(g){
return g.expend; });
return spendSum;
})
.entries(filteredData);
//pieData = pieData.concat(piepartyData);
//if selected totals --calculate totals by summing up each subcategory and creating new JS obj
if (filterOption == "Total"){
totaldata = CalcTotals(pieData);
var subcat_data = totaldata;
}
else{
var subcat_data = GetSubcatValues(pieData, filterOption);
}
//go thru candidate expenditures, nested by category and subcat--do not need for party level data
//get values
return {subcat_data: subcat_data, filterOption: filterOption, selections: selections};
}
function candidateProcessor(){
var checked = getChecked();
var selections = checked.selections;
var partySelections = checked.partySelections;
var filterOption = checked.filterOption;
var GenElect = document.getElementsByClassName("renderBox")
//console.log(selections);
/*//weekly or monthly data
if(xAxisSelection == '2016'){
expendData = expendDataBiweekly;
}
else{
expendData = expendDataMonthly;
}*/
//filterForPie(selections, partySelections, filterOption);
if(timeSelection == "general"){
expendData = expendDataMonthly.filter(function(d){return parseTime(d.date) > parseTime("25-Jul-16")});
}
else if(timeSelection == "2016"){
expendData = expendDataMonthly.filter(function(d){return parseTime(d.date) > parseTime("01-Dec-15")});
}
else{
expendData = expendDataMonthly;
}
//Sort by date - otherwise lines double back on eachother
//because dates are plotted out of order
var filteredData = expendData.sort(function(a,b){ return parseTime(a.date) - parseTime(b.date);});
// filter on the selected category, unless total
if(filterOption != 'Total'){
filteredData = filteredData.filter(function(d){ return d.category == filterOption; });
}
//filter - keep if d.cand_name has an index in array of selected candidates
//filter is like nested for loop -- returns true/false
filteredData = filteredData.filter(function(d){ return d.expend != 0;});
//filter - keep if d.cand_name has an index in array of selected candidates
var plotData = filteredData.filter(function(d){
return selections.indexOf(d.cand_name) > -1;
});