1
1
import Ember from 'ember' ;
2
+ import _ from 'lodash' ;
2
3
3
4
export default Ember . Component . extend ( {
4
5
classNames : 'graph-data' ,
@@ -79,8 +80,65 @@ export default Ember.Component.extend({
79
80
} ) ;
80
81
fmt . format ( myData , 0 ) ;
81
82
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 , {
84
142
chartArea : { 'left' : 85 , 'width' : '77%' , 'height' : '80%' } ,
85
143
hAxis : {
86
144
minorGridlines : { count : 8 } ,
@@ -89,8 +147,10 @@ export default Ember.Component.extend({
89
147
minorGridlines : { count : 5 } ,
90
148
viewWindow : { min : 0 , } ,
91
149
} ,
92
- isStacked : true ,
150
+ isStacked : false ,
93
151
focusTarget : 'category' ,
152
+ seriesType : 'scatter' ,
153
+ series : seriesOption
94
154
} ) ;
95
155
} ,
96
156
} ) ;
0 commit comments