Table JavaScript Functions for Adding Calculations

From Q
Jump to navigation Jump to search

This page contains functions that can be used to add new rows or columns containing the results of calculations (eg averages).

To make these functions available when writing a QScript or Rule see JavaScript Reference.

addAveragesToSpans

Takes the average for each span and adds a new row or column next to the span to show the average.

// Add a new row or column to each span which shows the average of the statistics for this span
function addAveragesToSpans(by_columns) {
    includeWeb("Table JavaScript Utility Functions");
    // Choose appropriate heading and summary
    if (by_columns) {
        form.setSummary('Add Average Columns to Tables Containing Spans'); // The summary shown on the ''rules'' tab
        form.setHeading('Computations - Adding Average Columns to Tables Containing Spans'); // The Rule Description heading
    } else {
        form.setSummary('Add Average Rows to Tables Containing Spans'); // The summary shown on the ''rules'' tab
        form.setHeading('Computations - Adding Average Rows to Tables Containing Spans'); // The Rule Description heading
    }
 
    // Don't apply the rule for columns when the table can't have any column spans
    if (by_columns && table.numberColumns < 2) {
        form.ruleNotApplicable(" table cannot have any column spans")
    }
 
    var spans = by_columns ? table.columnSpans : table.rowSpans;// Get the list of spans.
    var statistics = table.statistics;// Get the list of statistics on the table.
    
    // Work out which spans are lowest in the table.
    // These are the spans that are tested within.
    function getLowestLevelSpans(spans) {
        var lowest_spans = [];
        var found_indices = [];
        spans.forEach(function (span) {
            if (span.indices.every(function (x) { return found_indices.indexOf(x) == -1; })) {
                found_indices = found_indices.concat(span.indices);
                lowest_spans.push(span);
            }
        });
        return lowest_spans;
    }
    
    var lowest_spans = getLowestLevelSpans(spans);
    lowest_spans = lowest_spans.sort(function (a, b) {
        var indices_a = a.indices;
        var indices_b = b.indices;
        var last_a = indices_a[indices_a.length-1];
        var last_b = indices_b[indices_b.length-1];
        return last_a - last_b;
    });

 
    for (var i = lowest_spans.length - 1; i >= 0; i--) {// Working backwards through the list of spans...
 
        var span = lowest_spans[i];    // Get the current span.
        var span_indices = span.indices;    // Get the span_indices in this span.
        var last_index = span_indices[span_indices.length - 1];    // What is the last row in this span?
        // Add a row or column for this span
        if (by_columns)
            insertColumnAfterComplete(last_index, 'Average');   
        else
            insertRowAfterComplete(last_index, 'Average');

        var average_index = last_index + 1; 
 
        // Compute the averages and add them to the table
        table.statistics.forEach(function (stat) {
            var values = table.get(stat);
            if (by_columns) {
                for (var row = 0; row < table.numberRows; row++) {
                    if (typeof values[0][0] != "string") {
                        var sum = 0;
                        var count = 0;
                        for (var j = 0; j < span_indices.length; j++) {
                            var column = span_indices[j];
                            if (!isNaN(values[row][column])){
                                sum += values[row][column];
                                count ++;
                            }
                        }
                        values[row][average_index] = sum / count;          
                    } else if (stat == "Column Comparisons")
                        values[row][average_index] = "-"; // Set hyphen to indicate that this cell is not being tested.        
                }
            } else {
                for (var column = 0; column < table.numberColumns; column++) {
                    if (typeof values[0][0] != "string") {
                        var sum = 0;
                        var count = 0;
                        for (var j = 0; j < span_indices.length; j++) {
                            var row = span_indices[j];
                            if (!isNaN(values[row][column])){
                                sum += values[row][column];
                                count ++;
                            }
                        }
                        values[average_index][column] = sum / count; 
                    } else if (stat == "Column Comparisons")
                        values[average_index][column] = "-"; // Set hyphen to indicate that this cell is not being tested.
                }
            }
            table.set(stat, values);
        });
    }
}

See Also