Skip to content

Instantly share code, notes, and snippets.

@jaapz
Last active December 28, 2015 21:19
Show Gist options
  • Save jaapz/7563366 to your computer and use it in GitHub Desktop.
Save jaapz/7563366 to your computer and use it in GitHub Desktop.
calculateSeriesAverage: function() {
var total, firstHour, lastHour, dit, models;
dit = this;
total = 0;
firstTimestamp = this.first().get('timestamp');
lastTimestamp = this.last().get('timestamp');
this.each(function(model, i) {
var width, height, opp, next;
// Get next setpoint, if there is none, we are at the end of the
// series and we cannot calculate any more.
next = dit.at(i + 1);
if (typeof next === 'undefined') {
return;
}
// Calculate the oppervlakte of this setpoint.
if (model.get('value') >= next.get('value')) {
height = next.get('value');
} else {
height = model.get('value');
}
width = next.get('timestamp') - model.get('timestamp');
opp = width * height;
// If the values are not the same, also calculate the remaining
// triangular part and add it to the total.
if (model.get('value') !== next.get('value')) {
height = Math.abs(model.get('value') - next.get('value'));
opp += width * height * 0.5;
}
total += opp;
});
// Divide by number of hours between first and last setpoint.
return Math.round(total * 10 / (lastTimestamp - firstTimestamp)) / 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment