This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Viewing Average and Sum Data of Selected Splits

A lot of my runs follow this format: warm up, tempo, and cool down. On these runs I'd like to see statistics about only the tempo part of the run (i.e. average lap/mile time). If you are viewing the splits of a run, I'd like to be able to select splits and view the average and sum of the selected splits. I quickly hacked together this javascript code so I could do this on my own. If you copy the code below and execute it on the view splits page and click on some splits then you will see an example of how the feature should act. Please implement this, it wouldn't take too long ;)

jQuery('.rich-table-row').click(function(){
//Initialize variables
var metrics = {};
var sumTitle = "Selected Sum";
var avgTitle = "Selected Avg"

//Convert String to Seconds
var stringToSeconds = function(str) {
array = str.split(':');
if(str.indexOf(':') == 0) {
mins = 0;
} else {
mins = array.first();
}
secs = array.last();
return parseInt(mins) * 60 + parseFloat(secs);
}

//Convert Seconds to String
var secondsToString = function(secs) {
minsString = parseInt(secs / 60).toString();
secsString = ((secs % 60).toFixed(2)).toString();
if(parseInt(secsString) < 10) {
secsString = '0' + secsString;
}
return minsString + ':' + secsString;
}

// Adds two values, assumes if first one is minute formatted then so is second
var addValues = function(value1, value2) {
if(value1.indexOf(':') >= 0) {
sumSeconds = stringToSeconds(value1) + stringToSeconds(value2);
return secondsToString(sumSeconds);
} else {
return (parseFloat(value1) + parseFloat(value2)).toString();
}
};

// Divides
var getAverage = function(str, num) {
if(str.indexOf(':') > 0) {
divSeconds = stringToSeconds(str) / num;
return secondsToString(divSeconds)
} else {
return (parseFloat(str) / num).toFixed(2).toString();
}
};

// Modifies selected Row with most up to date averages
var updateSelectedRowAvg = function(selectedRow) {
_.each(selectedRow.find('td'), function(col, index){
if(index == 0) {
jQuery(col).html(avgTitle).css('text-align', 'center');
} else {
jQuery(col).html(getAverage(metrics[index]['sum'], metrics[index]['freq']));
}
});
};

// Modifies selected Row with most up to date sums
var updateSelectedRowSum = function(selectedRow) {
_.each(selectedRow.find('td'), function(col, index){
if(index == 0) {
jQuery(col).html(sumTitle).css('text-align', 'center');
} else {
jQuery(col).html(metrics[index]['sum']);
}
});
};

// Loops through rows scrapes data and appends it to the dom if needed
_.each(jQuery('.splitsRowSelected'), function(row){
_.each(jQuery(row).find('td'), function(col, index) {
if(typeof metrics[index] === 'undefined') {
metrics[index] = {};
metrics[index]['sum'] = '0';
metrics[index]['freq'] = 0;
}
var metric = jQuery(col).html();
if(metric.indexOf('-') < 0) {
metrics[index]['freq'] += 1;
metrics[index]['sum'] = addValues(metric, metrics[index]['sum']);
}
});
});

if(jQuery('.rich-table-subfooter').size() == 1) {
var selectedRowAvg = jQuery('.rich-table-subfooter').first().clone();
updateSelectedRowAvg(selectedRowAvg);
jQuery('tfoot').append(selectedRowAvg);

var selectedRowSum = jQuery('.rich-table-subfooter').first().clone();
updateSelectedRowSum(selectedRowSum);
jQuery('tfoot').append(selectedRowSum);
} else {
var selectedRowAvg = jQuery(".rich-table-subfooter td:contains('" + avgTitle + "')").parent().last();
updateSelectedRowAvg(selectedRowAvg);

var selectedRowSum = jQuery(".rich-table-subfooter td:contains('" + sumTitle + "')").parent().last();
updateSelectedRowSum(selectedRowSum);
}
});