2013-03-08 00:07:59 +08:00
|
|
|
/**
|
|
|
|
Get the y value of a report data point by its index. Negative indexes start from the end.
|
|
|
|
|
|
|
|
@method reportValueY
|
|
|
|
@for Handlebars
|
|
|
|
**/
|
|
|
|
Handlebars.registerHelper('valueAtDaysAgo', function(property, i) {
|
|
|
|
var data = Ember.Handlebars.get(this, property);
|
|
|
|
if( data ) {
|
2013-03-12 22:38:54 +08:00
|
|
|
var wantedDate = Date.create(i + ' days ago', 'en').format('{yyyy}-{MM}-{dd}');
|
2013-03-10 04:53:30 +08:00
|
|
|
var item = data.find( function(d, i, arr) { return d.x === wantedDate; } );
|
2013-03-08 00:07:59 +08:00
|
|
|
if( item ) {
|
|
|
|
return item.y;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
Sum the given number of data points from the report, starting at the most recent.
|
|
|
|
|
|
|
|
@method sumLast
|
|
|
|
@for Handlebars
|
|
|
|
**/
|
|
|
|
Handlebars.registerHelper('sumLast', function(property, numDays) {
|
|
|
|
var data = Ember.Handlebars.get(this, property);
|
|
|
|
if( data ) {
|
2013-03-12 22:38:54 +08:00
|
|
|
var earliestDate = Date.create(numDays + ' days ago', 'en');
|
2013-03-08 00:07:59 +08:00
|
|
|
var sum = 0;
|
|
|
|
data.each(function(d){
|
|
|
|
if(Date.create(d.x) >= earliestDate) {
|
|
|
|
sum += d.y;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
});
|