-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
286 lines (241 loc) · 8.08 KB
/
app.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
const baseUrl = '/api/v1.0';
let province_trend_by_year;
let provinces;
let years;
let ar_trend_chart;
let vr_trend_chart;
let nu_doughnut_total_chart;
//call API and get province trends by year
d3.json(`${baseUrl}/province_trend_by_year`)
.then(
function(data)
{
// store data in variables for laer use
province_trend_by_year = data;
//create list of provinces
provinces = province_trend_by_year.map((item) => item.Province);
//create list of years
years = province_trend_by_year[0].NumberOfUnits.map((item) => item.Year);
//add years to year dropdown
years.forEach((year) => {
d3.select("#selYear")
.append("option")
.text(year)
.property("value", year);
});
//render trend chart for average rents
renderTrendChartAverageRent();
//render trend chart for average vacancy rates
renderTrendChartVacancyRate();
//dispatch on chage even on year combobox to render doughnut chart
d3.select("#selYear").dispatch("change");
});
//function to handle province selection for trend charts
function updateProvinceSelection(button, province) {
//get index of the province from provinces list
const index = provinces.findIndex(el => {
return el === province;
});
//if province is in the list, remove the province and disable button
if(index < 0) {
provinces.push(province);
$(button).removeClass("disabled");
}
//if province is not in the list, add the province and enable button
else {
provinces.splice(index, 1).sort(function(a, b){return a - b});
$(button).addClass("disabled");
}
//render trend chart for average rents
renderTrendChartAverageRent();
//render trend chart for average vacancy rates
renderTrendChartVacancyRate();
}
//function to get color of trend line based on province
function getColor(province) {
if(province == "Alta")
return "red";
else if(province == "B.C.")
return "blue";
else if(province == "Ont.")
return "green";
else if(province == "Que")
return "yellow";
else if(province == "Man.")
return "black";
}
//function to get dataset based on province for trend chart
function getDatasetAverageRent(province) {
//filter province_data based on province
const province_data = province_trend_by_year.filter(
function(province_trend)
{
return province_trend.Province == province;
})[0];
//get and sort average rents for province and sort by year
const averageRents = province_data.AverageRents.sort(function(a, b){return a.Year - b.Year})
//create a dictionary for trend dataset for the provicne
const dict = {
label: province,
data: averageRents.map((item) => item.AverageRent),
borderColor: getColor(province),
backgroundColor: getColor(province),
};
//return dict
return dict;
}
//render trend chart for average rent
function renderTrendChartAverageRent() {
//get list of years
const years = province_trend_by_year[0].AverageRents.map((item) => item.Year);
datasets = [];
//loop through active provinces and create dataset list
provinces.forEach((province) => {
datasets.push(getDatasetAverageRent(province));
});
//create data object for chart.js
const labels = years;
const data = {
labels: labels,
datasets: datasets
};
//create config object for chart.js
const config = {
type: 'line',
data: data,
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'Average Rent'
}
}
},
};
//if chart exists, destroy it
if(ar_trend_chart != undefined)
ar_trend_chart.destroy();
//render cahrt
ar_trend_chart = new Chart("ar_trend_chart", config);
}
//function to get dataset based on province for trend chart
function getDatasetVacancyRate(province) {
//filter province_data based on province
const province_data = province_trend_by_year.filter(
function(province_trend)
{
return province_trend.Province == province;
})[0];
//get and sort vacancy rate for province and sort by year
const vacancyRates = province_data.VacancyRates.sort(function(a, b){return a.Year - b.Year})
//create a dictionary for trend dataset for the provicne
const dict = {
label: province,
data: vacancyRates.map((item) => item.VacancyRate),
borderColor: getColor(province),
backgroundColor: getColor(province),
};
//return dict
return dict;
}
//render trend chart for vacancy rates
function renderTrendChartVacancyRate() {
//get list of years
const years = province_trend_by_year[0].VacancyRates.map((item) => item.Year);
datasets = [];
//loop through active provinces and create dataset list
provinces.forEach((province) => {
datasets.push(getDatasetVacancyRate(province));
});
//create data object for chart.js
const labels = years;
const data = {
labels: labels,
datasets: datasets
};
//create config object for chart.js
const config = {
type: 'line',
data: data,
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'Average Vacancy Rate'
}
}
},
};
//if chart exists, destroy it
if(vr_trend_chart != undefined)
vr_trend_chart.destroy();
//render cahrt
vr_trend_chart = new Chart("vr_trend_chart", config);
}
//render doughnut chart for selected year
function renderNumberOfUnitsChart(year) {
//create dataset for doughnut chart for selected year
let num_units_dataset = province_trend_by_year.map((item) => {
var year_units = item.NumberOfUnits.filter(
function(y)
{
return y.Year == parseInt(year);
})[0];
data_dict = {};
data_dict["Province"] = item.Province;
data_dict["NumberOfUnits"] = year_units.NumberOfUnits;
return data_dict;
});
//sort the dataset by province
num_units_dataset = num_units_dataset.sort(function(a, b){return a.Province - b.Province});
//create data object for chart.js
const data = {
labels: num_units_dataset.map((el) => el.Province),
datasets: [{
data: num_units_dataset.map((el) => el.NumberOfUnits)
}]
};
//create config.object for chart.js
const config = {
type: 'doughnut',
data: data,
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'Number of Units by Province'
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var dataset = data.datasets[tooltipItem.datasetIndex];
var total = dataset.data.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});
var currentValue = dataset.data[tooltipItem.index];
var precentage = Math.floor(((currentValue/total) * 100)+0.5);
return precentage + "%";
}
}
}
}
},
};
//if chart exists, destroy it
if(nu_doughnut_total_chart != undefined)
nu_doughnut_total_chart.destroy();
//render chart
nu_doughnut_total_chart = new Chart("nu_doughnut_total_chart", config);
}