Add "Totals" row to DataTable

13,452 views
Skip to first unread message

Steffan Hoeke

unread,
Sep 20, 2011, 9:39:45 AM9/20/11
to google-visua...@googlegroups.com
Hello,

I was wondering if i've overlooked something in the API documentation, as I can't seem to find a way to add a row with totals (for specific columns) to a DataTable ?
I've found a topic from 2008 in which a Googler mentions that they're putting it on their To Do list, and a topic from someone who wrote a function for it, but i'm surprised it's not a part of the standard ?

Thanks

asgallant

unread,
Sep 20, 2011, 10:24:30 AM9/20/11
to google-visua...@googlegroups.com
If you want a "Totals" row, you'll have to do the calculation yourself, and add the data as an additional row in your DataTable object.  If you want a "Totals" column, you can use a DataView with a calculated column:

/*  assumes:
 *    view is a DataView object
 *    chart is the Chart object of the chart you are going to draw
 *    options is an option object for drawing the charts
 *    dataTable and rowNum are NOT used elsewhere
 *  sums columns 1 and 2 of the DataTable object into column 3 of the view
 */
view.setColumns([012{
    type'number',
    label'Total',
    funcfunction (dataTablerowNum{
        return dataTable.getValue(rowNum1dataTable.getValue(rowNum2);
    }
}]);
chart.draw(viewoptions);

Steffan Hoeke

unread,
Sep 22, 2011, 8:57:39 AM9/22/11
to google-visua...@googlegroups.com
Thanks for your response.

I was aware that I can use calculated columns, but that's not what i want ;)
I'm specifically looking for a way to add a totals Row (as a footer in the table), and since a googler indicated in an older topic that it was on their todo list hoped it would have been implemented by now :)

asgallant

unread,
Sep 22, 2011, 9:47:03 AM9/22/11
to google-visua...@googlegroups.com
You'll have to code it yourself, then, sorry  <:o(

Guillaume

unread,
Sep 22, 2011, 10:41:44 AM9/22/11
to Google Visualization API
Hi,

I am looking for the same thing, a total row displayed as a footer so
it won't move when changing the sorting of the column. As this
functionality is not available I am adding an extra row to the table
in which I am calculating the sums. Do you know if there is a nice way
to lock this row so it won't be taken in account in the sorting?

Thanks

smo...@aol.com

unread,
Sep 22, 2011, 11:37:32 AM9/22/11
to google-visua...@googlegroups.com
Is there anyone available for a consultation for a non-tech person on creating a motion chart for a presentation to a professional group?Please back channel.

sm




> wrote:
> You'll have to code it yourself, then, sorry  <:o(

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

ChartMan

unread,
Sep 25, 2011, 5:38:25 PM9/25/11
to google-visua...@googlegroups.com
Sorry

This has not been implemented so far. I would like your feedback, in case of paging should the totals be for the entire data or just the current page.

In the meanwhile you can implement this feature by computing the totals row and using it to draw the table, however set the 
'sort' option to 'event' so that the table is not sorted automatically. Upon a sort event perform the relevant sort using the dataTable sorting methond and again add the the totals row as the last row before draw.

HTH
ChartMan


Guillaume

unread,
Sep 26, 2011, 3:00:56 PM9/26/11
to google-visua...@googlegroups.com
Hi,

Thanks for coming back to us. 

Regarding paging: I don't think a total row at a page level would be really useful for me. Pages are often introduced for practical reasons, because the table is too big for the webpage. In that case the number of lines displayed on a page is driven by the amount of space allocated for displaying the table, which makes totals for the lines appearing on each page quite irrelevant. Obviously this is my own business requirement, someone else might find it very useful... In an ideal world we would have the on/off option for page and table total rows ;-) .

I'll try to implement the workaround, thanks.

Cheers,
G

MC Get Vizzy

unread,
Sep 27, 2011, 12:41:02 PM9/27/11
to google-visua...@googlegroups.com
+1 to the option of having the totals page-based or whole-table.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

NA

unread,
Sep 27, 2011, 12:51:20 PM9/27/11
to Google Visualization API
+1 to make the totals entire table based.
+1 to allowing us to specify the aggregation function for the totals
(similar to how grouping is done).

I agree with Guillaume's comments about totals with pagination. If
you can support both without too much trouble, then go for it, as it
might make more people happy.

thanks,

NA

unread,
Sep 27, 2011, 12:53:43 PM9/27/11
to Google Visualization API
BTW, are we talking about a total row with a DataTable or with a
Table? It seems as if we're mixing the two, as pagination is for a
Table but the comments and subject are for DataTable.

If you can implement it for a DataTable, and have the Table detect the
totals row and display it appropriately, that'd be excellent (perhaps
with a 'showTotalIfPresent' option). In this case, the issue about
pagination isn't relevant.

MC Get Vizzy

unread,
Sep 27, 2011, 12:54:52 PM9/27/11
to google-visua...@googlegroups.com
I think focusing on the Table chart here, instead of the DataTable object, would be a better idea.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

NA

unread,
Sep 27, 2011, 3:07:29 PM9/27/11
to Google Visualization API

I initially agreed with that, but then realized that you'd need to
teach a Table how to aggregate the columns, which it doesn't know how
to do. The DataTable, on the other hand, knows how to perform
aggregations, so adding an totals row property to the DataTable and
having the Table know how to present that row might be smoother.

Either way, while my preference is for the above, I don't see anything
horrible with teaching the Table how to aggregate and then adding the
feature there.

thanks,

Cristiano Maia

unread,
Nov 21, 2012, 8:33:21 AM11/21/12
to google-visua...@googlegroups.com
I did the manual way and it works fine for me.
I check the value from ALL rows in column 4 and sum it to a variable.

function totalWeight() {
 
var weightTotal=0;
 
for (var i=0; i<(tableData.getNumberOfRows()-1); i++)
{
weightTotal += tableData.getValue(i, 4);
}
 
tableData.setValue(tableData.getNumberOfRows()-1, 4, weightTotal);

sagar jadhav

unread,
Jun 22, 2018, 2:57:53 AM6/22/18
to Google Visualization API


So Simple Try This..

double sum = Convert.ToDouble(dt.Compute("SUM(datatable_Column_Name)", string.Empty));
//Datatable Column Name Put Only


Message has been deleted

Shannon Whitty

unread,
Jan 21, 2019, 12:01:00 AM1/21/19
to Google Visualization API
On Thursday, 22 September 2011 22:57:39 UTC+10, Steffan Hoeke wrote:
I was aware that I can use calculated columns, but that's not what i want ;)
I'm specifically looking for a way to add a totals Row (as a footer in the table), and since a googler indicated in an older topic that it was on their todo list hoped it would have been implemented by now :)
Thanks for your response.

This is an old thread but I have recently updated all my tables with what I have found to be the best solution for a "Totals" row for  Google Chart Table.

Why is this solution the best I've used?
1. It is a footer row so does not get sorted when the column header is clicked
2. It is not a separate table so doesn't create problems with aligning column widths

After creating and drawing my table, I add this following 2 lines of code:

    var mytable_foot = jQuery('<tfoot>').appendTo("#mytable table");

    mytable_foot
.append('<tr>'+
         
'<th class="google-visualization-table-td">Total</th>'+
         
'<th class="google-visualization-table-type-number google-visualization-table-td"><?php echo $count;?></th>'+
         
'<th class="google-visualization-table-type-number google-visualization-table-td">$ <?php echo number_format($sales,2);?></th>'+
         
'<th class="google-visualization-table-type-number google-visualization-table-td">$ <?php echo number_format($gp,2);?></th>'+
         
'<th class="google-visualization-table-type-number google-visualization-table-td">$ <?php echo number_format($svc_sales,2);?></th>'+
         
'<th class="google-visualization-table-type-number google-visualization-table-td">$ <?php echo number_format($svc_gp,2);?></th>'+
   
'</tr>');

 
Reply all
Reply to author
Forward
0 new messages