-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_averages.js
110 lines (89 loc) · 2.99 KB
/
game_averages.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
import cf from './cf';
import { ascending } from 'd3-array';
import { Reducer } from 'redugator';
import { averageScoreReducer } from './reducers';
export default class PlayerGameChart {
constructor() {
this.dimension = cf.dimension(d => {
let game = +d.Game;
let disc = d.Discipline;
let discOrder = +d.Disciplineorder * 10;
if (d.Block2 === '-1') {
game += 3;
}
return [disc + ': ' + game, discOrder + game, d.Gender];
});
this.group = Reducer.reduceGroup(averageScoreReducer, this.dimension.group());
this.dimension2 = cf.dimension(d => {
let game = +d.Game;
let discOrder = +d.Disciplineorder * 10;
if (d.Block2 === '-1') {
game += 3;
}
return discOrder + game;
});
this.group2 = Reducer.reduceGroup(averageScoreReducer, this.dimension2.group());
this.initialTotal = this.group2.all().reduce((p,v)=> p + v.value.sum, 0);
}
render() {
let _all = this.group.all().sort((a,b)=> ascending(a.key[1], b.key[1]));
let genders = new Map()
let categories = new Set()
for (let i = 0; i < _all.length; ++i) {
let d = _all[i];
let gender = d.key[2]
if (!genders.has(gender)) genders.set(gender, []);
genders.get(gender).push(d.value.avg);
categories.add(d.key[0]);
}
this.genderCount = genders.size;
if (!_all.length) return;
this.chart = new Highcharts.Chart({
chart: {
renderTo: document.getElementById('playerGameChart'),
type: 'line'
},
title: { text: 'Game Averages' },
xAxis: {
categories: Array.from(categories),
title: { text: null },
},
yAxis: {
min: 0,
title: { text: 'Average' },
labels: { overflow: 'justify' }
},
series: Array.from(genders.keys()).map((gender, i) => ({
name: gender + ' Average',
data: genders.get(gender),
color: Highcharts.theme.colors[i+1]
}))
});
}
redraw() {
if (!this.chart) return this.render();
let _all = this.group2.all();
let data = [];
let total = 0;
for (let i = 0; i < _all.length; ++i) {
let d = _all[i];
data.push(d.value.avg);
total += d.value.sum;
}
const lastIdx = this.chart.series.length - 1;
const lastSeries = this.chart.series[lastIdx];
if (total === this.initialTotal && lastSeries.name === 'Filter Average') {
lastSeries.remove(true);
return;
}
if (lastSeries.name === 'Filter Average') {
lastSeries.setData(data);
} else {
this.chart.addSeries({
name: 'Filter Average',
data: data,
color: Highcharts.theme.colors[0]
});
}
}
}