-
Notifications
You must be signed in to change notification settings - Fork 0
/
missing-persons.js
276 lines (237 loc) · 8.98 KB
/
missing-persons.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
function MissingPersons() {
// Name for the visualisation to appear in the menu bar.
this.name = 'Missing Persons: 1959 to 2018';
// Each visualisation must have a unique ID with no special characters.
this.id = 'missing-persons';
// Title to display above the plot.
this.title = 'Missing People in Harare: 1959 to 2018';
// Names for each axis.
this.xAxisLabel = 'Year';
this.yAxisLabel = 'Number of People';
var marginSize = 35;
// Layout object to store all common plot layout parameters and methods.
this.layout = {
marginSize: marginSize,
// Locations of margin positions. Left and bottom have double margin
// size due to axis and tick labels.
leftMargin: marginSize * 2,
rightMargin: width - marginSize,
topMargin: marginSize,
bottomMargin: height - marginSize * 2,
pad: 5,
plotWidth: function () {
return this.rightMargin - this.leftMargin;
},
plotHeight: function () {
return this.bottomMargin - this.topMargin;
},
// Boolean to enable/disable background grid.
grid: false,
// Number of axis tick labels to draw so that they are not drawn on
// top of one another.
numXTickLabels: 10,
numYTickLabels: 8,
};
// 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/missing-persons.csv', 'csv', 'header',
// Callback function to set the value this.loaded to true.
function (inputdata) {
self.loaded = true;
// Loops to accumulate numbers in columns. Consecutive columns are added together to stack them.
for (var i = 0; i < inputdata.getRowCount(); i++) {
for (var j = 0; j < inputdata.getColumnCount(); j++)
if (j > 1) {
inputdata.setNum(i, j, inputdata.getNum(i, j - 1) + inputdata.getNum(i, j));
}
}
return inputdata;
}
);
// Since the above data has been modified for stacked chart, the original values are loaded again into
// another variable to be able to display the value at mouse position.
this.dataOriginal = loadTable(
'./data/tech-diversity/missing-persons.csv', 'csv', 'header',
function (inputdata) {
return inputdata;
}
);
};
this.setup = function () {
// Font defaults.
textSize(16);
// Set min and max years: assumes data is sorted by date.
this.startYear = this.data.getNum(0, 'Year');
this.endYear = this.data.getNum(this.data.getRowCount() - 1, 'Year');
// Find min and max traffic for mapping to canvas height.
this.minPersons = 0;
this.maxPersons = max(this.data.getColumn(this.data.getColumnCount() - 1));
};
this.destroy = function () {
};
this.draw = function () {
if (!this.loaded) {
console.log('Data not yet loaded');
return;
}
// Draw the title above the plot.
this.drawTitle();
// Draw all y-axis labels.
drawYAxisTickLabels(this.minPersons,
this.maxPersons,
this.layout,
this.mapPersonsToHeight.bind(this),
0);
// Draw x and y axis.
drawAxis(this.layout);
// Draw x and y axis labels.
drawAxisLabels(this.xAxisLabel,
this.yAxisLabel,
this.layout);
// Plot all missing persons values between startYear and endYear using the width
// of the canvas minus margins.
var previous;
var numYears = this.endYear - this.startYear;
var fillColour = ['brown', 'lightblue', 'pink', 'orange', 'yellow'];
// Loop over all rows and for each row, draw a shape between that the line for
// that row and x-axis to fill it with colour.
for (var j = this.data.getColumnCount() - 1; j > 0; j--) {
// Need to reset previous for each row of data.
previous = null;
// Shape between line and x-axis to be filled with color
beginShape();
for (var i = 0; i < this.data.getRowCount(); i++) {
fill(fillColour[j - 1]);
noStroke();
// Create an object to store data for the current year.
var current = {
// Convert strings to numbers.
'year': this.data.getNum(i, 0),
'Persons': this.data.getNum(i, j)
};
vertex(
this.mapYearToWidth(current.year),
this.mapPersonsToHeight(current.Persons)
);
if (previous != null) {
// The number of x-axis labels to skip so that only
// numXTickLabels are drawn.
var xLabelSkip = ceil(numYears / this.layout.numXTickLabels);
// Draw the tick label marking the start of the previous year.
if (i % xLabelSkip == 0) {
drawXAxisTickLabel(previous.year, this.layout,
this.mapYearToWidth.bind(this));
}
}
// Assign current year to previous year so that it is available
// during the next iteration of this loop to give us the start
// position of the next vertex in the shape.
previous = current;
}
//Final two vertexes in the shape to be able to fill the area below line with color
vertex(
this.mapYearToWidth(this.endYear),
this.mapPersonsToHeight(0)
);
vertex(
this.mapYearToWidth(this.startYear),
this.mapPersonsToHeight(0)
);
endShape();
}
//Show legend, load original and modified data for mouseover year then display it along with legend and show a line on graph
if (mouseX > this.layout.leftMargin && mouseX < this.layout.rightMargin) {
// Map mouseX to years rounded
var mouseYear = map(mouseX, this.layout.leftMargin, this.layout.rightMargin, this.startYear, this.endYear).toFixed(0);
var mouseYearOrigData = this.dataOriginal.findRow(mouseYear, 'Year');
var mouseYearModiData = this.data.findRow(mouseYear, 'Year');
stroke(0);
line(
this.mapYearToWidth(mouseYear),
this.mapPersonsToHeight(mouseYearModiData.arr[5]),
this.mapYearToWidth(mouseYear),
this.mapPersonsToHeight(0),
);
noStroke();
fill(255);
ellipse(
this.mapYearToWidth(mouseYear),
this.mapPersonsToHeight(15),
50,
30
);
textAlign(CENTER);
fill(0);
text(
mouseYear,
this.mapYearToWidth(mouseYear),
this.mapPersonsToHeight(15),
)
}
else {
var mouseYear = null;
var mouseYearOrigData = null;
}
textAlign(LEFT);
var legendX = 90;
var legendY = 20;
for (var i = 1; i < this.data.getColumnCount(); i++) {
stroke('black');
fill(fillColour[i - 1]);
// colored ellipse
ellipse(legendX, legendY + 25 * i, 22, 20);
noStroke();
fill(0);
// text label
text(this.data.columns[i], legendX + 30, legendY + 9 + 25 * i);
if (mouseYear) {
textAlign(RIGHT);
//text value on mouse over
text(Math.round(mouseYearOrigData.arr[i]), legendX + 200, legendY + 9 + 25 * i);
text(Math.round(100 * mouseYearOrigData.arr[i] / mouseYearModiData.arr[5]) + '%', legendX + 250, legendY + 9 + 25 * i);
//text value on mouse over
textAlign(LEFT);
}
}
// total missing persons at mouse position
if (mouseYear) {
textStyle(BOLD);
textAlign(RIGHT);
text(Math.round(mouseYearModiData.arr[5]), legendX + 200, legendY + 9 + 150);
textAlign(LEFT);
text('Total No: of People', legendX + 30, legendY + 9 + 150);
//text value on mouse over
textStyle(NORMAL);
}
};
this.drawTitle = function () {
fill(0);
noStroke();
textAlign('center', 'center');
text(this.title,
(this.layout.plotWidth() / 2) + this.layout.leftMargin,
this.layout.topMargin - (this.layout.marginSize / 2));
};
this.mapYearToWidth = function (value) {
return map(value,
this.startYear,
this.endYear,
this.layout.leftMargin,
// Draw left-to-right from margin.
this.layout.rightMargin);
};
this.mapPersonsToHeight = function (value) {
return map(value,
this.minPersons,
this.maxPersons,
// Smaller persons at bottom.
this.layout.bottomMargin,
// Bigger perrsons at top.
this.layout.topMargin);
};
}