-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.js
117 lines (109 loc) · 3.52 KB
/
model.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
'use strict';
class Model {
constructor(dataSource) {
this.dataSource = dataSource;
this.data = [];
}
loadData() {
return new Promise((resolve, reject) => {
d3.json(this.dataSource, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
}
/**
* @description
* Returns d3 nested array with keys => academic discipline.
* Keys are sorted in descending order of the length of their values array,
* unless a priority value is provided. If a priority value is provided,
* keys are sorted by number of objects in their values array
* with the specified value.
* Values are sorted in ascending order of the key specified by
* options.sortValuesBy.
*
* @param {Object} options
* @param {String} options.sortValuesBy The key by which to sort values
* @param {Number} options.priorityValue Priority value to be placed first
* in sorted return array
*/
getDataByDiscipline(options) {
var dataByDiscipline = d3.nest()
.key((d) => d['cleandisciplinev2'])
.sortValues((a,b) => {
return Model.compareWithPriority(a,b,options);
})
.entries(this.data);
// Sort keys
dataByDiscipline.sort((a,b) => d3.descending(a.values.length,
b.values.length));
return dataByDiscipline;
}
static sortKeysByFilteredStatus(data, key, value) {
data.sort((a,b) => {
return Model.compareTotalValues(a, b, key, value);
});
return data;
}
/**
* @description
* Returns array of strings of target/perp statuses in ascending order of rank
*/
get targetRolesByRank() {
let targetRolesByRank = d3.nest()
.key((d) => d['cleantargetrole'])
.entries(this.data);
targetRolesByRank.sort((a,b) =>
d3.ascending(a.values[0].targetvalue, b.values[0].targetvalue));
return targetRolesByRank.map(datum => datum.key);
}
/**
* @description
* Comparison method to be used for sorting bars based on total values for
* a given key. Returns comparator with most values for the given key.
* @param {Object} a Comparator 1
* @param {Object} b Comparator 2
* @param {String} key Key to be used for comparison of values
* @param {Number} value Target value used for comparison
*/
static compareTotalValues(a,b,key,value) {
var totalA = a.values.reduce((n, incident) => {
return n + (incident[key] == value)
}, 0);
var totalB = b.values.reduce((n, incident) => {
return n + (incident[key] == value)
}, 0);
return d3.descending(totalA, totalB);
}
/**
* @description
* Comparison method to be used in array.sort with priority exception.
*
* @param {Number} a Comparator 1
* @param {Number} b Comparator 2
* @param {Object} options
* @param {String} options.sortValuesBy Key by which to sort values (objects)
* @param {Number} options.priorityValue Value to be placed first in sorted
* return array
*/
static compareWithPriority(a,b,options) {
if (!options.priorityValue) {
return d3.ascending(a[options.sortValuesBy], b[options.sortValuesBy]);
} else {
if (a[options.sortValuesBy] == options.priorityValue &&
b[options.sortValuesBy] == options.priorityValue) {
return 0;
}
if (a[options.sortValuesBy] == options.priorityValue) {
return -1;
}
if (b[options.sortValuesBy] === options.priorityValue) {
return 1;
}
return d3.ascending(a[options.sortValuesBy], b[options.sortValuesBy]);
}
}
}