Skip to content

Commit e10e624

Browse files
Merge pull request #552 from hellp/moving-avg-download-graph
Add 7-day moving averages to download graph. Plus UI tweaks.
2 parents 64c4011 + fae4553 commit e10e624

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

app/components/download-graph.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Ember from 'ember';
2+
import _ from 'lodash';
23

34
export default Ember.Component.extend({
45
classNames: 'graph-data',
@@ -79,8 +80,65 @@ export default Ember.Component.extend({
7980
});
8081
fmt.format(myData, 0);
8182

82-
var chart = new window.google.visualization.AreaChart(this.get('element'));
83-
chart.draw(myData, {
83+
// use a DataView to calculate an x-day moving average
84+
var days = 7;
85+
var view = new window.google.visualization.DataView(myData);
86+
var moving_avg_func_for_col = function(col) {
87+
return function(dt, row) {
88+
// For the last rows (the *first* days, remember, the dataset is
89+
// backwards), we cannot calculate the avg. of previous days.
90+
if (row >= dt.getNumberOfRows() - days) {
91+
return null;
92+
}
93+
94+
var total = 0;
95+
for (var i = days; i > 0; i--) {
96+
total += dt.getValue(row + i, col);
97+
}
98+
var avg = total / days;
99+
return {
100+
v: avg,
101+
f: avg.toFixed(2)
102+
};
103+
};
104+
};
105+
106+
var columns = [0]; // 0 = datetime
107+
var seriesOption = {};
108+
// Colors by http://colorbrewer2.org/#type=diverging&scheme=RdBu&n=10
109+
var colors = ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#92c5de',
110+
'#4393c3', '#2166ac', '#053061'];
111+
var [headers, ] = data;
112+
// Walk over the headers/colums in reverse order, as the newest version
113+
// is at the end, but in the UI we want it at the top of the chart legend.
114+
115+
_.range(headers.length - 1, 0, -1).forEach((dataCol, i) => {
116+
columns.push(dataCol); // add the column itself
117+
columns.push({ // add a 'calculated' column, the moving average
118+
type: 'number',
119+
label: `${headers[dataCol]} ${days}-day avg.`,
120+
calc: moving_avg_func_for_col(dataCol)
121+
});
122+
// Note: while the columns start with index 1 (because 0 is the time
123+
// axis), the series configuration starts with index 0.
124+
seriesOption[i * 2] = {
125+
type: 'scatter',
126+
color: colors[i % colors.length],
127+
pointSize: 3,
128+
pointShape: 'square'
129+
};
130+
seriesOption[i * 2 + 1] = {
131+
type: 'line',
132+
color: colors[i % colors.length],
133+
lineWidth: 2,
134+
curveType: 'function',
135+
visibleInLegend: false
136+
};
137+
});
138+
view.setColumns(columns);
139+
140+
var chart = new window.google.visualization.ComboChart(this.get('element'));
141+
chart.draw(view, {
84142
chartArea: { 'left': 85, 'width': '77%', 'height': '80%' },
85143
hAxis: {
86144
minorGridlines: { count: 8 },
@@ -89,8 +147,10 @@ export default Ember.Component.extend({
89147
minorGridlines: { count: 5 },
90148
viewWindow: { min: 0, },
91149
},
92-
isStacked: true,
150+
isStacked: false,
93151
focusTarget: 'category',
152+
seriesType: 'scatter',
153+
series: seriesOption
94154
});
95155
},
96156
});

0 commit comments

Comments
 (0)