-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage_chart.js
122 lines (113 loc) · 2.62 KB
/
average_chart.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
import avgAll from './avgAll';
import cr from './chart_registry.js';
export default class AverageChart {
constructor() {
this.filters = [];
}
hasFilter(key){
if (!arguments.length) {
return !!this.filters.length;
}
return !!~this.filters.indexOf(key);
}
filter(key) {
if (this.hasFilter(key)) {
this.filters.splice(this.filters.indexOf(key), 1);
} else {
this.filters.push(key);
}
if (this.filters.length === 0) {
this.dimension.filter(null);
} else if (this.filters.length === 1) {
this.dimension.filterExact(this.filters[0]);
} else {
this.dimension.filterFunction(d => {
for (let i = 0; i < this.filters.length; ++i) {
let filter = this.filters[i];
if (filter <= d && filter >= d) {
return true;
}
}
return false;
});
}
}
data() {
return avgAll(this.group.all());
}
render() {
let _all = this.data();
if (!_all.length) return;
let _this = this;
this.chart = new Highcharts.chart({
chart: {
type: this.options.type || 'bar',
renderTo: this.options.renderTo,
height: this.options.height
},
plotOptions: {
series: {
cursor: 'pointer',
point: {
events: {
click() {
_this.filter(this.category);
cr.redrawAll();
}
}
}
}
},
title: {
text: this.options.title
},
xAxis: {
categories: _all.map(d => d.key),
title: {
text: null
}
},
yAxis: {
min: 0,
title: {
text: 'Average'
},
labels: {
overflow: 'justify'
}
},
legend: {
enabled: false
},
series: [{
name: 'Average',
data: _all.map(d => d.value.avg)
}]
});
if (this.postRender) {
this.postRender();
}
}
redraw() {
let shouldRedraw = !this._redraw;
if (!this.chart) return this.render();
let _all = this.data();
let keys = []
let values = []
for (let i = 0; i < _all.length; ++i) {
let d = _all[i];
keys.push(d.key);
values.push({
y: d.value.avg,
color: this.getColor(d)
});
}
this.chart.xAxis[0].setCategories(keys, false);
this.chart.series[0].setData(values, shouldRedraw);
if (this._redraw) this._redraw(_all);
if (this.postRedraw) this.postRedraw();
}
getColor(d) {
return !this.hasFilter() ? undefined : (this.hasFilter(d.key) ? undefined : '#333333');
}
}