-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
197 lines (175 loc) · 4.9 KB
/
main.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
import * as d3 from 'https://cdn.jsdelivr.net/npm/d3@7/+esm';
/*
type module = {
ects_achieved: number
ects_module: number
ects_planned: number
grade: string
grade_equiv: number
lecturer: string
module_code: string
module_group: string
module_name: string
module_full_name: string
module_shorthand
semester_nr: number
semester_type: string
status: string
}
*/
let colors = {
darkorange: '#D95204',
mediumorange: '#F2911B',
brightorange: '#F2C288',
blue: '#325573',
brown: '#59252E',
white: '#FEF8EB',
black: '#13160F',
};
let moduleColors = {
a: '#009900',
b: '#cc0000',
c: '#6600ff',
d1: '#0066ff',
d2: '#cc6600',
h: '#993366',
};
let colorMap = (module_group) => {
if (module_group == 'A') return moduleColors.a; // Basic Modules
if (module_group == 'B') return moduleColors.b; // Project & Training, Language
if (module_group == 'C') return moduleColors.c; // Specialisation Modules
if (module_group == 'D1') return moduleColors.d1; // Technical Elective Module
if (module_group == 'D2') return moduleColors.d2; // Blockweeks & General Elective Module
if (module_group == 'H') return moduleColors.h; // Optional Elective Module
return colors.black;
};
let module_data = await d3.json('data/modules.json', (d) => console.log(d));
// Canvas Config
let width = 1920 / 1.5;
let height = 1080 / 1.5;
// Data Viz Config
let x_shift = 30;
let y_shift = 50;
let baseline_height = 10;
// Module Config
let module_width = 130;
let size_per_ects = 15;
let padding = 5;
let ectsPixelrange = (data) => [
d3.min(data, (d) => d.ects_module) * size_per_ects,
d3.max(data, (d) => d.ects_module) * size_per_ects,
];
let characters = 15;
function update(data) {
let ectsRamp = d3
.scaleLinear()
.domain([
d3.min(data, (d) => d.ects_module),
d3.max(data, (d) => d.ects_module),
])
.range(ectsPixelrange(data));
let svg = d3
.select('#data-viz')
.attr('width', width)
.attr('height', height)
.on('click', () => {
console.log('click registered - reloading');
});
let dataviz = svg
.append('g')
.attr('class', 'dataviz')
.attr('transform', `translate(${x_shift},${y_shift})`);
for (let sem = 0; sem <= 8; sem++) {
let semester_data = data.filter((d) => d.semester_nr == sem);
// console.log(`semester_data (semester ${sem}):`);
// console.log(semester_data);
dataviz
.append('g')
.attr('class', `semester sem${sem}`)
.attr('width', module_width)
.attr('height', height)
.attr('x', () => (sem + 1) * module_width + sem * padding);
// Semester Label
dataviz
.select(`g.sem${sem}`)
.append('text')
.attr('class', 'semester-label')
.attr('x', (d) => sem * module_width + sem * padding)
.style('margin-bottom', '1rem')
.text(`Semester ${sem}`);
let semesterSelection = dataviz
.select(`g.sem${sem}`)
.selectAll('.module')
.data(semester_data);
// UPDATE RECT
semesterSelection
.attr('width', module_width)
.attr('height', (d) => ectsRamp(d.ects_module))
.attr('x', (d) => sem * module_width + sem * padding)
.attr('y', (d, i) => {
let y = 0;
for (let module = 0; module < i; module++) {
y += ectsRamp(semester_data[module].ects_module);
}
return y + baseline_height;
})
.attr('rx', 5)
.attr('ry', 5)
.style('fill', (d) => colorMap(d.module_group))
.style('stroke', colors.black);
// UPDATE TEXT
semesterSelection
.select('text.module-name')
.text((d) => d.module_name)
.attr('width', module_width)
.attr('x', (d) => sem * (module_width + padding) + 2)
.attr('y', (d, i) => {
let y = 0;
for (let module = 0; module < i; module++) {
y += ectsRamp(semester_data[module].ects_module);
}
return y + size_per_ects + baseline_height;
})
.style('fill', colors.white);
// ENTER RECT
semesterSelection
.enter()
.append('rect')
.attr('class', 'module')
.attr('width', module_width)
.attr('height', (d) => ectsRamp(d.ects_module))
.attr('x', (d) => sem * module_width + sem * padding)
.attr('y', (d, i) => {
let y = 0;
for (let module = 0; module < i; module++) {
y += ectsRamp(semester_data[module].ects_module);
}
return y + i * padding + baseline_height;
})
.attr('rx', 5)
.attr('ry', 5)
.style('fill', (d) => colorMap(d.module_group))
.style('stroke', colors.black)
.append('text')
.html((d) => d.module_name);
// ENTER TEXT
semesterSelection
.enter()
.append('text')
.attr('class', 'module-name')
.text((d) => d.module_name)
.attr('width', module_width)
.attr('x', (d) => sem * (module_width + padding) + 2)
.attr('y', (d, i) => {
let y = 0;
for (let module = 0; module < i; module++) {
y += ectsRamp(semester_data[module].ects_module);
}
return y + size_per_ects + i * padding + baseline_height;
})
.style('fill', colors.white);
semesterSelection.exit().remove();
dataviz.data(semester_data).enter().append();
}
}
update(module_data);