Right. Having got a dynamic report working I thought I would share a few hints with every one, as I took a lot of notes from here its about time I gave some thing back.

Right by dynamic reports I mean reports that do not have a fixed number of columns, things like cross tab reports.

If your tying to do a cross tab report read this: http://wiki.pentaho.com/display/COM/...port%2C+Part+1
and the second part here:
http://wiki.pentaho.com/display/COM/...port%2C+Part+1

Following these should get you a reasonable report. Be aware the guides use a simplified version of the report.xml format so what you get out of the report designer will look different, the tags are there just with a lot more options.

However it lacked a few things for my needs. Totals being the big one for me.

One thing before we start. If at any point you get errors about invalid XML from the report action, stick a print parameters action in to print out your final report xml into the log. This will help you find the problem.

Setting up the item sum functions to total up each column is reasonably easy.

First ignore the advice in the two guides to remove the sum functions when running the report wizard.

You need two extra templates like the ones for the item band and the header band (See step 10 in part 2 of the linked guides). One will create the group footer entries. The other will create the item Sum Functions themselves. The item Sum XML file should look some thing like the following:
Code:
<expression name="Summary_{colName}Expression" class="org.jfree.report.function.ItemSumFunction">
      <properties>
        <property name="field">{colName}</property>
      </properties>
    </expression>
The group footer entry should look very similar to your item band entry the only difference is you want these to point at the functions you have just templated so you need the following line:
Code:
<basic-object name="field">Summary_{colName}Expression</basic-object>
Two things to watch out for.
1) you probably want to leave the "grand total:" label in the group footer as it saves creating it later in the JavaScript.
2) Don't delete all the functions when you take out any totals already in the report template. Row banding and item hiding functions should be left.

Now we need the .xaction to load these two new files. So create resource entries in the process inputs. Then Create the message Template objects. Then finally we can update the JavaScript entry to use these two fields. Add the resources as inputs to the action, then add some thing like the following in the loop which populates the topband and item band varables:
Code:
if ( i >= 2 ) {    // only want the numbers to have totals
       footerBand += replaceParams( footerTemplate, myXpos, colName );
        functions += replaceParams( functionsTemplate, myXpos, colName );

    }
I use the if to ignore the first two columns as they have text info so summing those makes no sense. Make sure that your varable names here are the same as the place holders you put in the template.xml file when you took the group footer and functions out.

Finally add the footerband and functions variables to the output of the JavaScript and also to the input list of the last message template action which sticks together the report.

Now in theory your report should have totals.

Ok cool. What about a total column at the end of your cross tab I hear you cry?

Ok for this we need a columnsumexpression. Due to our variable number of columns we will need to build this expression in the JavaScript. Now now don't hide behind the sofa, its not that bad.

Back in our JavaScript function we need change quite a bit.
1) Add a +1 to the column count function as we are adding an extra column
Code:
lineWidth = xPos * (colCount+1);
2) Start off our columnsumexpression in a new string:
Code:
RowSumFunction = "<function class=\"org.jfree.report.function.ColumnSumExpression\" name=\"TotalSum\">\n<properties>\n";
(I put the \n in to make it easier to read if you print out the XML)
3)In the loop we need to add an entry for each row we are adding up, so our if statment from eariler becomes:
Code:
if ( i >= 2 ) {    // only want the numbers to have totals
       footerBand += replaceParams( footerTemplate, myXpos, colName );
        functions += replaceParams( functionsTemplate, myXpos, colName );

    RowSumFunction += "<property name=\"field[" + (i-2) + "]\">" + colName + "</property>\n";
    }
4) Finish off our row sum function under the loop and add it on to the main function string:
Code:
RowSumFunction += "</properties>\n</function>\n";
functions += RowSumFunction;
Ok so thats created the expression to add the row up. Now we just need to display it.
Under the above add the following to stick the new column on the end:
Code:
myXpos += xPos;
topBand += replaceParams( topTemplate, myXpos, "Total");
itemBand += replaceParams( itemTemplate, myXpos, "TotalSum");
footerBand += replaceParams( footerTemplate, myXpos, "TotalSum");
functions += replaceParams( functionsTemplate, myXpos, "TotalSum");
That should be it for the total column, you should even end up with a total of totals at the bottom of this column.

So Enjoy. I hope this helps some one out. Any questions I'll keep an eye on this thread so post them below. Any improvements I will welcome. This is my first time writing a guide like this so there are bound to be things that could be worded better. If people are interested I'll put together some screen shots.

Wil