-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
644 lines (540 loc) · 19.2 KB
/
index.js
File metadata and controls
644 lines (540 loc) · 19.2 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
var width = 962,
rotated = 0,
height = 502;
var colourList = ['lightgreen', 'green', 'blue', 'palevioletred', 'red', 'yellow']
//Store parsed data
var parsedData;
d3.json("/data.json", function(data) {
parsedData = data;
});
//track where mouse was clicked
var initX;
//track scale only rotate when s === 1
var s = 1;
var mouseClicked = false;
// contains the path references for each country
var countries;
//This variable will be used to store the country name when the user clicks on a country
var countryClicked = '';
// Adapted from "d3 map with states and countries", http://bl.ocks.org/MaciejKus/61e9ff1591355b00c1c1caf31e76a668
var projection = d3.geo.mercator()
.scale(153)
.translate([width / 2, height / 1.5])
.rotate([rotated, 0, 0]);
// Adapted from "d3 map with states and countries", http://bl.ocks.org/MaciejKus/61e9ff1591355b00c1c1caf31e76a668
var zoom = d3.behavior.zoom()
.scaleExtent([1, 20])
.on("zoom", zoomed);
// Adapted from "d3 map with states and countries", http://bl.ocks.org/MaciejKus/61e9ff1591355b00c1c1caf31e76a668
var svg = d3.select("body").append("svg")
.attr("width", width+200)
.attr("height", height)
//track where user clicked down
.on("mousedown", function () {
d3.event.preventDefault();
//only if scale === 1
if (s !== 1) return;
initX = d3.mouse(this)[0];
mouseClicked = true;
})
.on("mouseup", function () {
if (s !== 1) return;
rotated = rotated + ((d3.mouse(this)[0] - initX) * 360 / (s * width));
mouseClicked = false;
})
.call(zoom);
// Adapted from "d3 map with states and countries", http://bl.ocks.org/MaciejKus/61e9ff1591355b00c1c1caf31e76a668
function rotateMap(endX) {
projection.rotate([rotated + (endX - initX) * 360 / (s * width), 0, 0])
g.selectAll('path')
.attr('d', path);
}
//for tooltip
var offsetL = document.getElementById('map').offsetLeft + 200;
// + document.getElementsByClassName();
var offsetT = document.getElementById('map').offsetTop + 10;
var path = d3.geo.path()
.projection(projection);
var tooltip = d3.select("#map")
.append("div")
.attr("class", "tooltip hidden");
var g = svg.append("g");
var languageColours = {};
d3.json("colours.json", function(error, data){
for(var lang in data){
languageColours[lang] = data[lang].color;
}
});
var topLanguages = [];
var topLanguagesColours = [];
var topPlatforms = [];
var topDeveloperTypes = [];
countryTops = {};
// parse country data
var countryData = {};
d3.json("data.json", function(error, data){
for(var country in data){
countryData[country] = data[country];
// parse top languages
var topLanguage = Object.keys(data[country].languages)[0];
if (topLanguage == "NA") topLanguage = Object.keys(data[country].languages)[1];
if (topLanguage == undefined) topLanguage = "NA";
if(topLanguages.indexOf(topLanguage) == -1) {
topLanguages.push(topLanguage);
topLanguagesColours.push(languageColours[topLanguage]);
}
// parse top platforms
var topPlatform = Object.keys(data[country].platforms)[0];
if (topPlatform == "NA") topPlatform = Object.keys(data[country].platforms)[1];
if (topPlatform == undefined) topPlatform = "NA";
if (topPlatforms.indexOf(topPlatform) == -1) {
topPlatforms.push(topPlatform);
}
// parse top developer types
var topDeveloperType = Object.keys(data[country].developerTypes)[0];
if (topDeveloperType == "NA") topDeveloperType = Object.keys(data[country].developerTypes)[1];
if (topDeveloperType == undefined) topDeveloperType = "NA";
if (topDeveloperTypes.indexOf(topDeveloperType) == -1) {
topDeveloperTypes.push(topDeveloperType);
}
}
countryTops = {"top languages": topLanguages, "top platforms": topPlatforms, "top developer types": topDeveloperTypes};
console.log(countryTops); //remove
var dropdowndiv = d3.select("body").insert("div", ":first-child");
dropdowndiv.insert("a").html("Colour attribute:");
var dropdown = dropdowndiv.insert("select")
.attr("class", "select")
// Page Title
d3.select("body").insert("h2", ":first-child").html("Stack Overflow Developer Survey (2019): World Map ")
dropdown.selectAll("option")
.data(Object.keys(countryTops))
.enter()
.append("option")
.attr("value", function (d) { return d; })
.text(function (d) {
return d;
//return d[0].toUpperCase() + d.slice(1, d.length); // capitalize 1st letter
});
var legendRectSize = 18;
var legendSpacing = 4;
var legend = svg.append("g")
.attr("class", "legend");
// Handler for dropdown value change
var dropdownChange = function () {
var selectedAttribute = d3.select(this).property('value');
console.log(selectedAttribute);
console.log(countryTops[selectedAttribute]);
if (selectedAttribute == undefined)
selectedAttribute = "top languages";
var rects = legend
.selectAll('rect')
.data(countryTops[selectedAttribute], function(d){return d;})
rects.exit().remove();
rects.enter()
.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.attr('x', width + 20)
.attr('y', function (d, i) { return 100 + i * 25 })
.attr('fill', function (d, i) {
var value = countryTops[selectedAttribute][i]
if (selectedAttribute == "top languages") {
var ret = topLanguagesColours[topLanguages.indexOf(value)];
} else {
var ret = d3.scale.category20().domain(countryTops[selectedAttribute])(value);
if(value == "NA")
return "white"
}
if (ret == undefined)
return "white";
return ret;
})
.attr('stroke', function () { return "black" });
rects.transition()
.attr('y', function (d, i) { return 100 + i * 25 })
.attr('fill', function (d, i) {
var value = countryTops[selectedAttribute][i]
if (selectedAttribute == "top languages") {
var ret = topLanguagesColours[topLanguages.indexOf(value)];
} else {
var ret = d3.scale.category20().domain(countryTops[selectedAttribute])(value);
if(value == "NA")
return "white"
}
if (ret == undefined)
return "white";
return ret;
})
var texts = legend.selectAll('text')
.data(countryTops[selectedAttribute], function (d) { return d; });
texts.exit().remove()
texts.enter()
.append('text')
.attr('x', width + 50)
.attr('y', function (d, i) { return 110 + i * 25 })
.text(function (d, i) { return countryTops[selectedAttribute][i]; });
texts.transition()
.attr('y', function (d, i) { return 110 + i * 25 })
updateMapColours(selectedAttribute);
};
dropdown.on("change", dropdownChange);
dropdownChange();
});
//console.log(countryData);
console.log("topLanguages: ");
console.log(topLanguages);
console.log("topLanguagesColours: ");
console.log(topLanguagesColours);
// update the colour of each country, by attribute parameter
function updateMapColours(attribute){
if(countries != undefined){
console.log(countries);
console.log(countryData);
var altName = attribute;
if(attribute == 'top languages'){
altName = 'languages';
} else if(attribute == 'top platforms'){
altName = 'platforms';
} else if(attribute == 'top developer types'){
altName = 'developerTypes';
}
countries.attr("style", function (d) {
var colour = "white";
if (d.properties.name != undefined && countryData[d.properties.name] != undefined){
//countryData[d.properties.name][altName][0]
var value = Object.keys(countryData[d.properties.name][altName])[0];
if(value == "NA" && Object.keys(countryData[d.properties.name][altName])[1] != undefined)
value = Object.keys(countryData[d.properties.name][altName])[1];
var colour = "white"
if (altName == "languages") {
colour = topLanguagesColours[topLanguages.indexOf(value)];
} else {
colour = d3.scale.category20().domain(countryTops[attribute])(value);
if (value == "NA")
colour = "white"
}
}
console.log(colour);
return "fill:" + colour + ";";
})
console.log(countryData);
console.log(countryTops);
console.log(parsedData);
}
}
// get json data and draw it
d3.json("world-countries.json", function (error, world) {
if (error) return console.error(error);
countries = g.append("g")
.attr("class", "boundary")
.selectAll("boundary")
.attr("width", width-200)
.attr("height", height)
.data(topojson.feature(world, world.objects.countries1).features)
.enter()
.append("path")
.attr("name", function (d) { return d.properties.name; })
.attr("id", function (d) { return d.id; })
.attr("style", function (d) {
var colour = "#f0f0f0";
if(countryData[d.properties.name] != undefined){
languages = Object.keys(countryData[d.properties.name].languages);
colour = languageColours[languages[0]];
if(colour == undefined){
//console.log('language issue: ' + languages[0] + ', country: ' + d.properties.name);
colour = "#f0f0f0";
} else{
if(topLanguages.indexOf(languages[0]) == -1){
topLanguages.push(languages[0]);
topLanguagesColours.push(colour);
}
}
}else{
//console.log("Country issue: " + d.properties.name);
}
return "fill:" + colour + ";";
})
.on('click', selected)
.on("mousemove", showTooltip)
.on("mouseout", function (d, i) {
tooltip.classed("hidden", true);
})
.attr("d", path);
});
// tooltip
function showTooltip(d) {
label = d.properties.name;
var mouse = d3.mouse(svg.node())
.map(function (d) { return parseInt(d); });
tooltip.classed("hidden", false)
.attr("style", "left:" + (mouse[0] + offsetL) + "px;top:" + (mouse[1] + offsetT) + "px")
.html(label);
}
// selection
d3.selection.prototype.moveToFront = function () {
return this.each(function () {
this.parentNode.appendChild(this);
});
};
//This functon will highlight the country pressed and grab the country name
function selected(d) {
if (countryClicked != ''){ //clears the countryClicked variable when user presses new country
countryClicked = '';
countryClicked += d.properties.name; //stores country name in variable
}
countryClicked = d.properties.name; //stores country name in variable
console.log(countryClicked);
//Aabids function for his graphs can go here and he can pass it country name
let attribute = "languages";
displayBarCharts(countryClicked, attribute, "Languages", true, 1);
attribute = "platforms";
displayBarCharts(countryClicked, attribute, "Platforms", true, 2);
attribute = "yearsCoding";
displayBarCharts(countryClicked, attribute, "Coding Experience", false, 3);
attribute = "developerTypes";
displayBarCharts(countryClicked, attribute, "Developer Types", true, 4);
attribute = "student";
displayPieCharts(countryClicked, attribute, "Student", 5);
attribute = "genders";
displayPieCharts(countryClicked, attribute, "Gender", 6);
d3.select('.selected').classed('selected', false);
d3.select(this).classed('selected', true);
d3.select(this).moveToFront();
}
// zoom
function zoomed() {
var t = d3.event.translate;
s = d3.event.scale;
var h = 0;
t[0] = Math.min(
(width / height) * (s - 1),
Math.max(width * (1 - s), t[0])
);
t[1] = Math.min(
h * (s - 1) + h * s,
Math.max(height * (1 - s) - h * s, t[1])
);
zoom.translate(t);
if (s === 1 && mouseClicked) {
rotateMap(d3.mouse(this)[0])
return;
}
g.attr("transform", "translate(" + t + ")scale(" + s + ")");
//adjust the stroke width based on zoom level
d3.selectAll(".boundary")
.style("stroke-width", 1 / s);
}
// Additional charts
//Called when a country on the map is clicked
function displayBarCharts(countryClicked, attribute, xLabel, bool, index){
//use the parsed data
//console.log(parsedData[countryClicked]);
let data = parsedData[countryClicked][attribute];
//console.log(data);
//console.log(Object.keys(data));
//console.log(Object.values(data));
let maxY = Math.max(...Object.values(data));
let newData = [];
let maxLength = 10;
if (Object.keys(data).length < maxLength){
maxLength = Object.keys(data).length;
}
for (let i = 0; i < maxLength; i++){
newData.push({'x': `${Object.keys(data)[i]}`, "y": `${Object.values(data)[i]}`});
}
//console.log("newData = ", newData);
let div = document.getElementById(`vis${index}`);
if(div.hasChildNodes()){
let removeTable = document.getElementById(`svgChart${index}`);
removeTable.parentNode.removeChild(removeTable);
}
const margin = 50;
const width = 500;
const height = 500;
const chartWidth = width - 2 * margin;
const chartHeight = height - 2 * margin - 80;
const colourScale = d3.scale.ordinal()
//.domain([0, d3.max(data, d => d.y)])
.domain([0, 5])
.range(colourList);
const xScale = d3.scale.ordinal()
.rangeRoundBands([0, chartWidth], .1)
.domain(newData.map((d) => d.x))
//console.log("domain = ", xScale.domain())
const yScale = d3.scale.linear()
.range([chartHeight, 0])
.domain([0, maxY]);
//console.log("domain = ", yScale.domain())
const xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
const yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
const svg = d3.select(`#vis${index}`)
.append('svg')
.attr('id', `svgChart${index}`)
.attr('class', 'graph')
.attr('width', width)
.attr('height', height);
const canvas = svg.append('g')
.attr('transform', `translate(${margin}, ${margin})`);
let title;
if (bool == true){
title = `Top ${maxLength} ${xLabel} in ${countryClicked}`
}
else{
title = `${xLabel} in ${countryClicked}`
}
// chart title
svg.append('text')
.attr('x', margin + chartWidth / 1.75)
.attr('y', margin - 30)
.attr('text-anchor', 'middle')
.text(title)
.attr('font-size', 22)
.attr('font-weight', 'bold')
.style("text-decoration", "underline");
// x-axis and label
canvas.append('g')
.attr('transform', `translate(${margin}, ${chartHeight})`)
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-65)")
.attr('font-size', 10);
svg.append('text')
.attr('x', margin + chartWidth / 2 + margin)
.attr('y', chartHeight + 2 * margin + 60)
.attr('text-anchor', 'middle')
.text(`${xLabel}`)
.attr('font-weight', 'bold')
.style('fill', 'red');
// y-axis and label
canvas.append('g')
.attr('transform', `translate(50, 0)`)
.call(yAxis)
.attr('font-size', 15);
svg.append('text')
.attr('x', margin + -(chartWidth / 1.7))
.attr('y', margin - 10)
.attr('transform', 'rotate(-90)')
.attr('text-anchor', 'middle')
.text('Count')
.attr('font-weight', 'bold')
.style('fill', 'red');
//const legend = canvas.
var xs = [];
for (var i = 0; i < newData.length; i++) {
xs.push(newData[i].x);
}
// the bar chart
const bars = canvas.selectAll('rect')
.data(newData)
.enter()
.append('rect')
.attr('x', (d) => margin + xScale(d.x))
.attr('y', chartHeight)
.attr('height', 0)
.attr('width', xScale.rangeBand())
.on('mouseenter', function(source, index) {
d3.select(this)
.transition()
.duration(200)
.attr('opacity', 0.5);
})
.on('mouseleave', function(source, index) {
d3.select(this)
.transition()
.duration(200)
.attr('opacity', 1.0);
});
bars.transition()
.ease("elastic")
.duration(800)
.delay((d, index) => index * 50)
.attr('y', (d) => yScale(d.y))
.attr('height', (d) => chartHeight - yScale(d.y))
.attr('fill', function (d) {
return d3.scale.category20().domain(xs)(d.x);
})
}
function displayPieCharts(countryClicked, attribute, xLabel, index){
var w = 500;
var h = 500;
var r = h/3;
var color = d3.scale.category10();
let div = document.getElementById(`vis${index}`);
if(div.hasChildNodes()){
let removeTable = document.getElementById(`svgChart${index}`);
removeTable.parentNode.removeChild(removeTable);
}
//console.log(parsedData[countryClicked]);
let data = parsedData[countryClicked][attribute];
//console.log(data);
//console.log(Object.keys(data));
//console.log(Object.values(data));
let maxY = Math.max(...Object.values(data));
let newData = [];
let maxLength = 10;
if (Object.keys(data).length < maxLength){
maxLength = Object.keys(data).length;
}
let totalCount = d3.sum(Object.values(data))
//console.log("totalCount", totalCount);
for (let i = 0; i < maxLength; i++){
let percent = Math.round(Object.values(data)[i]/totalCount * 100)
//console.log("divide = ", percent)
newData.push({'category': `${Object.keys(data)[i]}`, "value": `${percent}`});
}
//console.log("newData = ", newData);
var vis = d3.select(`#vis${index}`)
.append("svg:svg")
.attr('id', `svgChart${index}`)
.attr('class', 'graph')
.data([newData])
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + r * 1.62 + "," + r * 1.62 + ")");
var pie = d3.layout.pie().value(function(d){return d.value;});
// chart title
vis.append('text')
.attr('x', 0)
.attr('y', -240)
.attr('text-anchor', 'middle')
.text(`${xLabel} Distribution in ${countryClicked}`)
.attr('font-size', 22)
.attr('font-weight', 'bold')
.style("text-decoration", "underline");
// Declare an arc generator function
var arc = d3.svg.arc().outerRadius(r);
// Select paths, use arc generator to draw
var arcs = vis.selectAll("g.slice").data(pie).enter().append("svg:g").attr("class", "slice");
arcs.append("svg:path")
.attr("fill", function(d, i){return color(i);})
.attr("d", function (d) {return arc(d);})
;
// Add the text
arcs.append("svg:text")
.attr("transform", function(d){
d.innerRadius = 100; /* Distance of label to the center*/
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")";}
)
.attr("text-anchor", "middle")
.text( function(d, i) {return newData[i].value + '%';})
.attr('font-size', 12);
//remove this later
arcs.append("svg:text")
.attr("transform", function(d){
d.innerRadius = 200; /* Distance of label to the center*/
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")";}
)
.attr("text-anchor", "middle")
.text( function(d, i) {return newData[i].category;})
.attr('font-size', 12);
}