-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
807 lines (507 loc) · 25.9 KB
/
script.js
File metadata and controls
807 lines (507 loc) · 25.9 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
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
var bootstrapClass = 'col-12'
var container = d3.select("#countryContainer")
// .append("div")
// .attr('class', 'container')
// .attr('id', 'countryContainer')
var key = container
.append('div')
var togglingTitleFontSize = '2vw';
var allcountries = []
var countriescheckforrepeat = [];
var countriesNotRepeated = [];
var togglingTitleFontSize = '1.5vw';
var margin = {top: 40, right:0 , bottom: 14, left: 0},
width = window.innerWidth- margin.left - margin.right,
height = window.innerHeight+5000- margin.top - margin.bottom;
// if(window.innerWidth<500){
// width = window.innerWidth/1.2
// }
var unParsedData =[];
//Positive and Negative Bar Chart
var x = d3.scaleLinear()
.range([0,width]);
var y = d3.scaleBand()
.rangeRound([height,0])
.padding(0.3);
var colorArray = ["#0a97d9", "#6254a8", "#2f9969", "#fcc30b", "#a21942"]
var z = d3.scaleOrdinal()
// .range(d3.schemePaired);26bde2
.range(["#2f9969", "#a21942", "#0a97d9", "#6254a8", "#fd9d24", "#fcc30b"]);
//Stacked Area Chart
var parseDate = d3.timeParse("%Y");
// d3.timeYears(1990, 2014[ 2]);
var classArrayForInteraction = [];
//This is an array of all the different income categories
var objectKeys = [];
// Where I put my key value pairs of colors
var objectColors = []
//End overlay
//This is an array of ALL countries combined with their change listed
function render(data, i){
function parseClass(objectkey){
return objectkey.replace('aIncome', "Income")
.replace('bIncome', "Income")
.replace('cIncome', "Income")
.replace('dIncome', "Income")
.replace('eIncome', "Income")
.replace('by', "by the ")
.replace('twenty', " quintile of the population ")
.replace(/_/g, ' ')}
objectColors.sort(function(b, a) {
var nameA = a.Category.toUpperCase(); // ignore upper and lowercase
var nameB = b.Category.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
// objectColors = objectColors.sort()
var chartDiv = d3.select('body')
.append('div')
.attr('id', 'FullCountryChart')
.attr('class', 'chartDiv');
var chartsRow = d3.select("#countryRow")
// .append('div')
// .attr('class', 'row')
// .attr('id', '#countryRow')
// var togglingTitle = chartsRow
// .append('div')
// .attr('class', bootstrapClass)
// .attr('id', "togglingTitle")
// .attr("class", "tooltip")
// .style("opacity", 0);
//one overall svg for everything
var svgContainer =
chartsRow
.append('div')
.attr('class',bootstrapClass)
var tool = d3.select("body")
.append('div')
.attr('class', 'tool')
// .attr('class', 'chartDiv');
var div = d3.selectAll(".tool")
var svg = svgContainer // d3.select("#countryContainer")
.append("svg")
.attr("width", width )
.attr("height", height)
.attr("class", 'barChartSVG')
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("width", width)
svg.append('rect')
.attr("width", width)
.attr("height", width)
//adding mini chart titles
svg
.append("text") // text label for the x axis
.attr("class", "miniTitles")
.attr("x", width/4 )
.attr("y", -10)
.style("text-anchor", "middle")
// .html("+" );
svg
.append("text") // text label for the x axis
.attr("class", "miniTitles")
.attr("x", width/1.3)
.attr("y", -10)
.style("text-anchor", "middle")
// .html("-" );
svg
.append("text") // text label for the x axis
.attr("class", "miniTitles")
.attr("x", width/2 )
.attr("y", -24)
.style("text-anchor", "middle")
// .html("Neg. Change" + "                        " + "                        " +"Pos. Change" );
.html("Most Change" );
var keyContainer = d3.select('#mainTitleRow')
.append('div')
.attr('class', 'legend')
.append('svg')
.attr('width', 148)
.attr('height', 150)
.append('g')
.attr('class', 'keycontainer')
.style('left', 2000)
.style('visibility', 'hidden')
var keys = keyContainer.selectAll('rect')
.data(objectColors)
.enter()
.append('rect')
.attr("x", 10)
.attr("y", function(d,i){return i*17})
.attr('width', 148)
.attr('height', 15)
.attr('class', 'key')
.attr('fill', function(d) { return d.color})
var yforkey = d3.scaleLinear().range([0, 100]);
var arr = [40, 70, 100, 130, 160]
keyContainer
.selectAll('.keyText')
.data(objectColors)
.enter()
.append('text')
.attr('class', 'keyText')
.style('fill', 'white')
.style('opacity', .7)
.attr('x',15)
.attr('y', function(d,i){return i*17 +12})
.html(function(d){return parseClass(d.Category).replace("Income share held by the ","").replace("of the population ","")})
var countriesFullObject = [];
///Yey! This javascript method prevents repetition
var listofuniquecountries = Array.from(new Set(countriesNotRepeated))
//start adding elements to my single country objects. I want Country, Start year/end year, change.
objectKeys.forEach(function(overallLoop) {
listofuniquecountries.forEach(function(d){
var singleCountryObject = new Object();
singleCountryObject.Country = d;
singleCountryObject.Category = overallLoop;
// {quintile change array: country, quintile, max year, min year, change;; [years]},{[years]},{[years]}
//gather the years of each country and stick the min and max in object
var eachCountryYears = []
data.forEach(function(alldata){
if(alldata.Country_Name == d){
eachCountryYears.push(alldata.Time)
}
});
singleCountryObject.StartingYear = d3.min(eachCountryYears)
singleCountryObject.MiddleYear = d3.median(eachCountryYears)
singleCountryObject.EndingYear = d3.max(eachCountryYears)
data.forEach(function(alldata){
var valuesChange = [];
if(alldata.Time == singleCountryObject.EndingYear && singleCountryObject.Country == alldata.Country_Name){
singleCountryObject.EndingYearValue = alldata[overallLoop]
valuesChange.push(alldata[overallLoop])
} else if (alldata.Time == singleCountryObject.StartingYear && singleCountryObject.Country == alldata.Country_Name){
singleCountryObject.StartingYearValue = alldata[overallLoop]
valuesChange.push(alldata[overallLoop])
}
});
function posOrNegChange (startyearvalueinput,endyearvalueinput){
if (startyearvalueinput <= endyearvalueinput){
return 100-startyearvalueinput/endyearvalueinput*100
} else { return (100-endyearvalueinput/startyearvalueinput*100)*-1
}
}
singleCountryObject.Change = posOrNegChange(singleCountryObject.StartingYearValue, singleCountryObject.EndingYearValue);
//find all the values for all quintiles
//pushes object into array
if(singleCountryObject.Change){
countriesFullObject.push(singleCountryObject)
}
});
})
countriesFullObject.sort(function(obj2, obj1) {
// Ascending: first age less than the previous
return Math.abs(obj1.Change) - Math.abs(obj2.Change);
});
x.domain([-100, 100]);
y.domain(countriesFullObject.map(function(d) { return d.Category; }));
///This is where I start building my chart with modified data
// countriesFullObject.forEach(function(allChangeInnerLoop){
var format = d3.format(".1f");
// bar.append("rect")
// .attr("width", x)
// .attr("height", barHeight - 1);
var leastChange =
svg
.append("text") // text label for the x axis
.data(countriesFullObject)
.attr("class", "miniTitles")
.attr("id", "leastChange")
.attr("x", width/2)
.attr("y", height/9)
.style("text-anchor", "middle")
.html("Least Change" );
// bar.append("text")
// .attr("x", function(d) { return x(d) - 3; })
// .attr("y", barHeight / 2)
// .attr("dy", ".35em")
// .text(function(d) { return d; });
var maxMinChangeForTopLable = []
countriesFullObject.forEach(function(d){
maxMinChangeForTopLable.push(d.Change)
});
function round5(x)
{
return Math.ceil(x/5)*5;
}
var maxChange =
svg
.data(countriesFullObject)
.append("text") // text label for the x axis
.attr("class", "minTitles")
.attr("id", "maxChange")
.attr("x", function(){
return x(round5(d3.max(maxMinChangeForTopLable)+5))
})
.attr("y", -20)
.style("text-anchor", "middle")
// .style("fill", "#000000")
.html(function(){ return round5(d3.max(maxMinChangeForTopLable))+ "%"});
var minChange =
svg
.data(countriesFullObject)
.append("text") // text label for the x axis
.attr("class", "minTitles")
.attr("id", "minChange")
.attr("x", function(){
return x(round5(d3.max(maxMinChangeForTopLable)+5)*-1)
})
.attr("y", -20)
.style("text-anchor", "middle")
// .style("fill", "#000000")
.html(function(){ return round5(d3.max(maxMinChangeForTopLable))*-1+ "%"});
var minChangeLine =
svg
.data(countriesFullObject)
.append("line") // text label for the x axis
.attr("class", "minTitles")
.attr("id", "minChangeLine")
.attr("x1", function(){
return x(round5(d3.max(maxMinChangeForTopLable)+5)*-1 -2)
})
.attr("x2", function(){
return x(round5(d3.max(maxMinChangeForTopLable)+6))
})
.attr("y1", -15)
.attr("y2", -15)
.style('stroke', "#a5acb7")
.style('stroke-width', .5)
var rect = svg.selectAll(".anything")
.data(countriesFullObject)
.enter()
.append("rect")
.attr("class", function(d){ return d.Country+"singleBar"})
.attr("id", function(d){return d.Category+d.Country+d.StartingYear})
// .attr("id", function(d){return d.Category+d.Country+d.StartingYear})
.attr("x", function(d){
return d.Change < 0 ? x(d.Change) : x(0);
})
// .transition().duration(300)
.attr("width", function(d){
return d.Change < 0 ? x(d.Change * -1) - x(0) : x(d.Change) - x(0);
})
.attr("y", function(d, i){
return i*(height/1800);
})
.attr("height", 1)
.attr("fill", function(d) {
for(var i=0; i<objectColors.length; i++){
if(d.Category === objectColors[i].Category){
return objectColors[i].color ;
}
}
})
.on('click', function(d, callback){
// console.log(this)
var tempCountry = d.Country;
svg.selectAll( "rect")
.style('opacity', function(d){
if (this.className.baseVal != tempCountry+ 'singleBar'){
return ".1"
} else {
return '1'
}
})
// //Second Click
// .on('click', function(d){
// var tempCountry2 = tempCountry;
// svg.selectAll( "rect")
// .style('opacity', function(d){
// if (this.className.baseVal === tempCountry2+ 'singleBar'){
// return "1"
// }
// })
// })
// d3.selectAll( "."+d.Country+'singleBar')
// .style('fill', '#000000')
})
.on("mouseover", function(d) {
d3.selectAll(".keycontainer")
.transition()
.duration(300)
.style('visibility', 'visible')
// d3.select(d.Category+d.Country+d.StartingYear+"text")
// .style('visibility','visible')
d3.select( "#"+ d.Category+d.Country+d.StartingYear)
.attr("opacity", ".3")
// .attr("stroke-width, '3px")
.attr("font-size", 30)
div.attr("x", "0")
div.transition()
.duration(200)
.style("opacity", .9);
div .html(function() {
var thisText = "<b>" + d.Country.replace('_', ' ') + " from " + d.StartingYear + " to " + d.EndingYear + ":" + "</b>" + "</br>"+ "<span class = 'classColor'>" + parseClass(d.Category) + "</span>"+ increasedDecreased(d.Change) + " from " + format(d.StartingYearValue) + " to " + format(d.EndingYearValue) + " percent, " + "a change of " + format(d.Change) + " percent."
return thisText;
})
////LEFT DEAL WITH LATER
.style("left", (testMousepositionX(d3.event.pageX)) + "px")
.style("top", (testMouseposition(d3.event.pageY - 28)) + "px");
//this is how I tartgeted the Y attribute of each rect
var mouseY = this.y.baseVal.value
d3.select ('#' + d.Category+d.Country+d.StartingYear+"text")
// .style ('fill', '#000000')
.attr("y", function(){
return mouseY +7
})
.attr("x", function(d){
if(d.Change<0){
return x(d.Change) -60 ;
} else { return x(d.Change) +10 }
// return d.Change < 0 ? x(d.Change) : x(0);
})
.attr("dy", ".35em")
// .delay(10)
.transition()
.duration(200)
.style('opacity', 1)
//END MOUSEOUT
})
.on("mouseout", function(d, i) {
d3.select ('#' + d.Category+d.Country+d.StartingYear+"text")
.transition()
.duration(200)
.style('opacity', 0)
d3.select( "#"+ d.Category+d.Country+d.StartingYear)
.attr("opacity", "1")
.attr("fill", function(d) {
for(var i=0; i<objectColors.length; i++){
if(d.Category === objectColors[i].Category){
return objectColors[i].color ;
}
}
})
div.transition()
.duration(500)
.style("opacity", 0);
})
//Test mouse position to avoid overlap
function testMouseposition(mousepos){
if(mousepos<400){
return 300
} else { return mousepos }
};
//Test mouse position to avoid overlap
function testMousepositionX(mousepos){
if(mousepos<width/2){
return 70
} else { return width-width/4 }
};
var ticks = svg.selectAll('.innerText')
.data(countriesFullObject)
.enter()
.append('text') // testMouseposition(d3.event)
.attr('class', 'innerText')
.attr('id', function(d){ return d.Category+d.Country+d.StartingYear+"text"})
.style('opacity', 0)
// .style('fill', "#000000")
.text(function(d){return format(d.Change) + "%";})
d3.selectAll(".classColor")
.data(countriesFullObject)
.style("color", function(d) {
for(var i=0; i<objectColors.length; i++){
if(d.Category === objectColors[i].Category){
return objectColors[i].color ;
}
}
})
keyContainer.on('click', function(d){
// console.log(this)
svg.selectAll('rect')
.style('opacity', 1)
})
svg.on('mouseover', function(d){
svg.selectAll('rect')
.transition()
.duration(500)
.attr("y", function(d, i){
return i*(height/300);
})
.delay(10)
.transition()
.duration(300)
.attr('height', 15)
// d3.select("#lineSide")
// .attr("x1", width/10 )
// .attr("y1", height-50)
// .attr("x2", width/10 )
// .attr("y2", 10)
d3.select("#leastChange")
.attr("y", height/1.5)
})
function increasedDecreased(positiveneg){
if(positiveneg<0){
return "decreased"
} else {return "increased"};
}
}
//LAYER INTERACTIVITY
d3.csv ("data/wdialldata.csv", type, function(error, data){
// if (error) console.log(error);
data.forEach(function(d){
d.Country_Name = d.Country_Name.replace(" ", "_")
d.Time = +d.Time
// console.log(d.Country_Name)
// console.log(d.Time)
});
var country1 = []
var country2 = []
var country3 = []
//Create object Keys so I can send each numeric key through program
function loopthroghObjectKeys(){
var keys = Object.keys(data[0])
for(var i=0; i<keys.length; i++){
if(keys[i]!="Country_Name"&& keys[i]!="Time"&& keys[i]!="Country_Code"){
objectKeys.push(keys[i]);
}
}
}
loopthroghObjectKeys();
function createColorPairs (){
for(var i = 0; i<colorArray.length; i++){
var colorObject = new Object;
colorObject.color = colorArray[i];
colorObject.Category = objectKeys[i];
objectColors.push(colorObject)
}
}
createColorPairs ()
// End Create Object Keys
var cleaned = [];
data.forEach(function(d){
// var countriescheckforrepeat = [];
// var countriesNotRepeated = [];
for( var i = 0; i<objectKeys.length; i++){
//I'm only analyzing data with full year values for all quintiles
//I'm putting all country names in one place
if (d[objectKeys[i]]*1){
cleaned.push(d);
countriesNotRepeated.push(d.Country_Name)
}
// d.Country_Name[i]
}
countriescheckforrepeat.push(cleaned.Country_Name)
});
render(cleaned)
// render(country1)
// render(country3)
/// make a different d3 select all for each item in array of classes
/// when that thing is selected turn all instances of it black
// var selectorOne = d3.selectAll(body)
// .on("mouseover", function(d,i){classArrayForInteraction[i].style("fill", "#000000");})
});
// console.log(countriescheckforrepeat)
function type(d, i, columns) {
// unParsedData.push(d)
//This turns the year and stuff into integers
// d.Time = parseDate(d.Time);
// for (var i = 2, n = columns.length; i < n; ++i) d[columns[i]] = d[columns[i]]/100;
return d;
}
/// If I hover over THIS class, return THIS paragraph associated with this class