-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtech-diversity-gender.js
148 lines (121 loc) · 3.78 KB
/
tech-diversity-gender.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
function TechDiversityGender() {
// Name for the visualisation to appear in the menu bar.
this.name = 'Tech Diversity: Gender';
// Each visualisation must have a unique ID with no special
// characters.
this.id = 'tech-diversity-gender';
// Layout object to store all common plot layout parameters and
// methods.
this.layout = {
// Margin positions around the plot. Left and bottom margins are
// bigger so there is space for axis and tick labels on the canvas.
leftMargin: 130,
rightMargin: width,
topMargin: 30,
bottomMargin: height,
pad: 5,
plotWidth: function() {
return this.rightMargin - this.leftMargin;
},
// Boolean to enable/disable background grid.
grid: true,
// Number of axis tick labels to draw so that they are not drawn on
// top of one another.
numXTickLabels: 10,
numYTickLabels: 8,
};
// Middle of the plot: for 50% line.
this.midX = (this.layout.plotWidth() / 2) + this.layout.leftMargin;
// Default visualisation colours.
this.femaleColour = color(255, 0 ,0);
this.maleColour = color(0, 255, 0);
// Property to represent whether data has been loaded.
this.loaded = false;
// Preload the data. This function is called automatically by the
// gallery when a visualisation is added.
this.preload = function() {
var self = this;
this.data = loadTable(
'./data/tech-diversity/gender-2018.csv', 'csv', 'header',
// Callback function to set the value
// this.loaded to true.
function(table) {
self.loaded = true;
});
};
this.setup = function() {
// Font defaults.
textSize(16);
};
this.destroy = function() {
};
this.draw = function() {
if (!this.loaded) {
console.log('Data not yet loaded');
return;
}
// Draw Female/Male labels at the top of the plot.
this.drawCategoryLabels();
var lineHeight = (height - this.layout.topMargin) /
this.data.getRowCount();
// Loop over every row in the data.
for (var i = 0; i < this.data.getRowCount(); i++) {
// Calculate the y position for each company.
var lineY = (lineHeight * i) + this.layout.topMargin;
// Create an object that stores data from the current row.
var company = {
// Convert strings to numbers.
'name': this.data.getString(i,'company'),
'female':this.data.getNum(i,'female'),
'male':this.data.getNum(i,'male'),
};
// Draw the company name in the left margin.
fill(0);
noStroke();
textAlign('right', 'top');
text(company.name,
this.layout.leftMargin - this.layout.pad,
lineY);
// Draw female employees rectangle.
fill(this.femaleColour);
rect(this.layout.leftMargin,
lineY,
this.mapPercentToWidth(company.female),
lineHeight - this.layout.pad);
// Draw male employees rectangle.
// ???
fill(this.maleColour);
rect(this.layout.rightMargin,lineY,-this.mapPercentToWidth(company.male),lineHeight - this.layout.pad);
}
// Draw 50% line
stroke(150);
strokeWeight(1);
line(this.midX,
this.layout.topMargin,
this.midX,
this.layout.bottomMargin);
};
this.drawCategoryLabels = function() {
fill(0);
noStroke();
textAlign('left', 'top');
text('Female',
this.layout.leftMargin,
this.layout.pad);
textAlign('center', 'top');
text('50%',
this.midX,
this.layout.pad);
textAlign('right', 'top');
text('Male',
this.layout.rightMargin,
this.layout.pad);
};
this.mapPercentToWidth = function(percent) {
return map(percent,
0,
100,
0,
this.layout.plotWidth());
};
}