-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStatistics.js
268 lines (254 loc) · 9.75 KB
/
Statistics.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
import { ResponsiveContainer, ReferenceLine, BarChart, Bar, XAxis, YAxis, Tooltip, Legend, LabelList } from 'recharts';
import React from 'react';
import Footer from './Footer.js';
import Notice from './Notice.js';
const defaults = {
courseName: '-',
programShort: '-',
programLong: '-',
averageGrade: '0',
passRate: '0',
total: '0',
};
const grades = ['U', '3', 'G', 'TG', '4', '5'];
const colors = {
'U': 'hsl(20, 90%, 40%)',
'3': 'hsl(100, 60%, 80%)',
'G': 'hsl(100, 60%, 80%)',
'TG': 'hsl(100, 60%, 80%)',
'4': 'hsl(100, 60%, 60%)',
'5': 'hsl(100, 60%, 40%)'
};
class Statistics extends React.Component {
constructor(props) {
super(props);
this.state = {
expand: 'none',
data: null,
info: null,
exams: true,
stack: true,
misc: true,
};
}
componentDidMount() {
this.fetchInfo(this.props.match.params.initial);
window.onpopstate = () => {
this.setState(this.props.history.location.state);
};
}
percentScore(value, payload) {
let total = grades.reduce(((acc, grade) => { return acc + payload[grade]; }), 0);
return Math.round(100*(value/total))+'%';
}
fetchInfo(value) {
Promise.all([
fetch(`${process.env.PUBLIC_URL}/results/${value}`).then(r => r.json()),
fetch(`${process.env.PUBLIC_URL}/courses/${value}`).then(r => r.json()),
]).then(([r1, r2]) => {
this.props.history.replace(`/${value}/`, { data: r1, info: r2 })
this.setState({ data: r1, info: r2 });
});
}
downloadData(value) {
fetch(`${process.env.PUBLIC_URL}/results/${value}`)
.then(r => r.json())
.then(json => json.map(result => [result.date,`"${result.type}"`,result[3],result[4],result[5],result.G,result.VG,result.TG].join(',')))
.then(csv => 'Date,Type,U,3,4,5,G,VG,TG\r\n' + csv.join('\r\n'))
.then(data => this.downloadCSV(data, value))
}
downloadCSV(data, fileName) {
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(data);
hiddenElement.target = '_blank';
hiddenElement.download = `${fileName}.csv`;
hiddenElement.click();
hiddenElement.remove();
}
renderBackButton() {
if (this.props.history.length > 2) {
return (<button id="all-courses" onClick={this.props.history.goBack}>← All courses</button>);
} else {
return (<a id="all-courses" href={process.env.PUBLIC_URL+'/'}>← All courses</a>);
}
}
renderDownloadButton() {
return (<button id="download-csv" title="Download a CSV file of unfiltered data" className="button is-small is-link is-outlined" onClick={() => this.downloadData(this.props.match.params.initial.toUpperCase())}>Download CSV</button>);
}
renderInput() {
const handleChange = (event) => {
const val = event.target.value;
const regex = '^[a-zA-Z]{3}\\d{3}$';
const match = val.match(regex);
if (match) {
this.fetchInfo(match[0])
}
};
return (<input className="input" type="text" placeholder="Course code" onChange={handleChange} />);
}
render() {
let infoRender = {};
if (this.state.info) {
infoRender = this.state.info;
} else {
infoRender = defaults;
}
const InfoBar = (
<div>
<nav className="level">
<div className="level-item has-text-centered">
<div>
<p className="heading">Course</p>
<p className="title">{ infoRender.courseName }</p>
</div>
</div>
</nav>
<nav className="level">
<div className="level-item has-text-centered">
<div>
<p className="heading">Program name</p>
<p className="title"><abbr title={infoRender.programLong}>{ infoRender.programShort }</abbr></p>
</div>
</div>
<div className="level-item has-text-centered">
<div>
<p className="heading">Pass rate</p>
<p className="title">{ Math.round(infoRender.passRate * 1000)/10 }%</p>
</div>
</div>
<div className="level-item has-text-centered">
<div>
<p className="heading">Average grade</p>
<p className="title">{ Math.round(infoRender.averageGrade*100)/100 }</p>
</div>
</div>
<div className="level-item has-text-centered">
<div>
<p className="heading">Total number of results</p>
<p className="title">{ infoRender.total }</p>
</div>
</div>
</nav>
</div>
);
const radio = (
<div className="control">
<div className="control-group">
<label className="radio" title="Stacked">
<input type="radio" name="stacked" onClick={() => this.setState({ stack: true })} defaultChecked />
<span className="icon">
<i className="fas fa-bars"></i>
</span>
</label>
<br></br>
<label className="radio" title="Unstacked">
<input type="radio" name="stacked" onClick={() => {
let expand = this.state.expand
if (this.state.expand === 'silhouette') {
expand = 'none';
}
this.setState({ stack: false, expand: expand });
}} />
<span className="icon">
<i className="fas fa-align-left"></i>
</span>
</label>
</div>
<div className="control-group">
<label className="radio">
<input type="radio" name="setting" checked={this.state.expand === 'none'} onChange={() => this.setState({ expand: 'none' })} />
Standard
</label>
<br></br>
<label className="radio">
<input type="radio" name="setting" checked={this.state.expand === 'expand'} onChange={() => this.setState({ expand: 'expand' })} />
Normalized
</label>
<br></br>
<label className="radio">
<input type="radio" name="setting" disabled={!this.state.stack} checked={this.state.expand === 'silhouette'} onChange={() => this.setState({ expand: 'silhouette' })} />
Median
</label>
<br></br>
</div>
<div className="control-group">
{ this.renderDownloadButton() }
<br></br>
<label className="checkbox">
<input type="checkbox" defaultChecked={this.state.exams} onClick={() => this.setState({ exams: !this.state.exams })}/>
Show exams
</label>
<br></br>
<label className="checkbox">
<input type="checkbox" defaultChecked={this.state.misc} onClick={() => this.setState({ misc: !this.state.misc })}/>
Show misc
</label>
</div>
</div>
);
let heightFactor = 1;
if (!this.state.stack) {
heightFactor = 3;
}
let filtered = [];
if (this.state.data) {
if (this.state.exams && this.state.misc) {
filtered = this.state.data;
} else if (this.state.exams || this.state.misc) {
let bool = (item) => item.type.includes('Tentamen');
if (this.state.misc) {
bool = (item) => !item.type.includes('Tentamen');
}
filtered = this.state.data.filter(bool);
}
if (this.state.expand === 'expand' && !this.state.stack) {
filtered = filtered.map(item => {
let total = grades.reduce(((acc, grade) => {return acc+item[grade]}), 0);
let it = Object.assign({}, item);
grades.forEach(grade => {
it[grade] /= total;
});
return it;
});
}
}
return (
<div className="container">
{ this.renderBackButton() }
<Notice />
{ this.renderInput(this.handleChange) }
{ InfoBar }
{ radio }
{ this.state.info && filtered &&
<ResponsiveContainer width="100%" height={Math.max(Object.keys(filtered).length * 40 * heightFactor, 300)}>
<BarChart
data={filtered}
layout="vertical"
margin={{ top: 20, right: 40, left: 40, bottom: 5 }}
stackOffset={this.state.expand}
label="type"
>
<XAxis type="number" orientation="top"/>
<YAxis type="category" tickLine={false} dataKey="date" />
<Tooltip itemSorter={()=>1} formatter={(value,name,props) => (this.state.expand==='expand'? this.percentScore(value, props.payload) : value) } />
<Legend />
{ grades.map(grade =>
this.state.info[grade] > 0 &&
<Bar key={grade} barSize={20} dataKey={grade} {... (this.state.stack ? {stackId: 'a'} : {})} fill={colors[grade]}>
<LabelList
fontSize={10}
valueAccessor={x => (x.width>20 ? (this.state.expand==='expand' ? this.percentScore(x[grade], x.payload) : x[grade]) : null)}
position="center" />
</Bar>
)}
{ this.state.expand === 'silhouette' &&
<ReferenceLine x="0" stroke="black" isFront strokeDasharray="3 3" />
}
</BarChart>
</ResponsiveContainer>}
<Footer />
</div>
);
}
}
export default Statistics;