This example assumes that the
report you are working with contains a table named
Orders that contains a
field named
Order_Amount, and that
the report contains one level of grouping.
You can summarize the fields in
a group. For example, you can group a report by region, and then summarize the
sales for each region.
Get the
DataDefController object.
DataDefController ddc = rcd.getDataDefController();
Find the
Orders table from
the list of tables.
Tables tables = rcd.getDatabaseController().getDatabase().getTables();
ITable customerTable = tables.findTableByAlias("Orders");
Find the
Order_Amount field
from the list of fields.
Fields fields = customerTable.getDataFields();
IField orderAmountField = fields.findField("{Orders.Order_Amount}", FieldDisplayNameType.formulaName, java.util.Locale.US);
Call the
canSummarizeOn method of the
SummaryFieldController class to
validate that summaries can be calculated for the field.
SummaryFieldController sfc = ddc.getSummaryFieldController();
boolean canSummarizeOn = sfc.canSummarizeOn(orderAmountField);
Get the
Group
object to add the summary to.
In this example, we the innermost group is
retrieved.
Groups groups = ddc.getDataDefinition().getGroups();
IGroup group = groups.getGroup(groups.size() - 1);
Create a new
SummaryField object.
ISummaryField summaryField = new SummaryField();
Call the
setGroup
method to add the summary to a group.
summaryField.setGroup(group);
Call the
setSummarizedField method to
set the field that is being summarized.
summaryField.setSummarizedField(orderAmountField);
Call the
setOperation method to define
the type of summary operation.
summaryField.setOperation(SummaryOperation.sum);
Call the
add method
of
SummaryFieldController to add
the summary to the report.
The value -1 adds the
summary to the end of the summary list.
sfc.add(-1, summaryField);
Example: Adding a summary to a group
The following example shows how to add a summary
field that calculates the sum of the
Orders.Order_Amount
field for each group. The sum of
Orders.Order_Amount
for each group is displayed in the group footer.
public void addGroupSummary(ReportClientDocument rcd) throws ReportSDKException
{
DataDefController ddc = rcd.getDataDefController();
Tables tables = rcd.getDatabaseController().getDatabase().getTables();
ITable customerTable = tables.findTableByAlias("Orders");
Fields fields = customerTable.getDataFields();
IField orderAmountField = fields.findField("{Orders.Order_Amount}", FieldDisplayNameType.formulaName, java.util.Locale.US);
SummaryFieldController sfc = ddc.getSummaryFieldController();
boolean canSummarizeOn = sfc.canSummarizeOn(orderAmountField);
if (canSummarizeOn)
{
Groups groups = ddc.getDataDefinition().getGroups();
IGroup group = groups.getGroup(groups.size() - 1);
ISummaryField summaryField = new SummaryField();
summaryField.setGroup(group);
summaryField.setSummarizedField(orderAmountField);
summaryField.setOperation(SummaryOperation.sum);
sfc.add(-1, summaryField);
}
}