I've written a function that adds a total row to a Table
Visualization.

This relies on being able to check that the data type is 'number' and
the column pattern does not contain a percent sign '%'.  You cannot
total a percent column, it doesn't make any sense.  I'm also assuming
USD '$', but it could easily be changed to a different currency.  Any
string columns will have 'Total' inserted.

To add this total row it must be added via that DataTable addRow()
function.  This will change the DataTable, so if you use a
Visualization that relies on Dates, you will want to create a DataView
to copy the DataTable and then use something like this to hide the
total row in the DataView.

// remove Total row from DataView before drawing visualizations that
could be confused by it
// assuming Total row is the last rowIndex value
view_row_count = data.getNumberOfRows();
view_row_count--; // rowIndexes start at zero
view.hideRows([view_row_count]);

// ###################
// function to add total row
  function add_total_row() { // add a total row to the DataTable
    var row_count = data.getNumberOfRows();
    var column_count = data.getNumberOfColumns();
    var new_row = new Array();

    for (col_i = 0; col_i < column_count; col_i++) { // total each
column
      var cell_value = 0;
      if (data.getColumnType(col_i) == 'string') { // enter 'Total' on
string columns
        new_row[col_i] = 'Total';
      }
      else if (data.getColumnType(col_i) == 'number') {
        // get column pattern
        col_pattern = data.getColumnPattern(col_i);

        if (col_pattern.match('%') != null) { // percent columns
should not be totaled
          new_row[col_i] = null;
        }
        else { // total column values
          for (row_i = 0; row_i < row_count; row_i++) {
            cell_value = cell_value + data.getValue(row_i, col_i);
          }
          // create rounded value to 2 decimals
          cell_formatted = Math.round(cell_value*100)/100;

          if (col_pattern.match(/\$/i) != null) { // add currency sign
if it's in the column's pattern
            new_row[col_i] = {v: cell_value, f: '$' + cell_formatted};
          }
          else { // no currency sign needed
            new_row[col_i] = {v: cell_value, f: '' + cell_formatted};
          }
        }
      }
      else { // boolean, data, datatime, timeofday types should not be
totaled
        new_row[col_i] = null;
      }
    }

// ################
// now just call the function before drawing your table
    add_total_row();
// Table Visualization
    table = new
google.visualization.Table(document.getElementById('table'));
    table.draw(data, {allowHtml: true});

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-visualization-api?hl=en.

Reply via email to