Home » Archived » BIRT » Any Example of adding groups to a table and applying Total Class Fucntions?
Any Example of adding groups to a table and applying Total Class Fucntions? [message #216745] |
Fri, 02 February 2007 12:38  |
Eclipse User |
|
|
|
Originally posted by: alikhan.xxxxx.com
Hello Community,
I am wodering if there are examples our there where the groups are added
dynamically to a report using DE API and the BIRT functions like Total.ave
and Total.weightedAve() applied in the group folder,I have had issues
applying the expressions in the group footer, and I have asked the
questions many times without getting any reply.
It would be helpful if *ATLEAST* some one from the BIRT community points
out to an existing example that does so.
Thanks in Advance!
|
|
|
Re: Any Example of adding groups to a table and applying Total Class Fucntions? [message #216768 is a reply to message #216745] |
Fri, 02 February 2007 13:56   |
Eclipse User |
|
|
|
Take a look at this example.
It does not do a group but shows you how to add the total function to a
bound column and then
puts the value in the footer.
Jason
import java.io.File;
import java.io.IOException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException ;
import org.eclipse.birt.report.model.api.command.ContentException;
import org.eclipse.birt.report.model.api.command.NameException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConst ants;
import org.eclipse.birt.report.model.api.elements.structures.Comput edColumn;
import org.eclipse.birt.report.model.api.metadata.IMetaDataDictiona ry;
import org.eclipse.birt.report.model.elements.interfaces.IReportIte mModel;
import org.eclipse.birt.report.model.elements.interfaces.IStyleMode l;
import org.eclipse.birt.report.model.api.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.ScriptDataSourceHandle;
import com.ibm.icu.util.ULocale;
public class ScriptedDSReport
{
ReportDesignHandle reportDesignHandle = null;
ElementFactory elementFactory = null;
IMetaDataDictionary dict = null;
ComputedColumn cs1, cs2, cs3, cs4 = null;
public static void main( String[] args ) throws SemanticException,
IOException
{
new ScriptedDSReport( ).createReport( );
}
void createReport( ) throws SemanticException, IOException
{
//Configure the Engine and start the Platform
DesignConfig config = new DesignConfig( );
config.setProperty("BIRT_HOME",
"C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
IDesignEngine engine = null;
try{
Platform.startup( config );
IDesignEngineFactory factory = (IDesignEngineFactory) Platform
.createFactoryObject(
IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
engine = factory.createDesignEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
// Create a new report
reportDesignHandle = session.createDesign( );
// Element factory is used to create instances of BIRT elements.
elementFactory = reportDesignHandle.getElementFactory( );
dict = new DesignEngine( null ).getMetaData( );
createMasterPages( );
createDataSources( );
createDataSets( );
createBody( );
String outputPath = "output";//$NON-NLS-1$
File outputFolder = new File( outputPath );
if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) )
{
throw new IOException( "Can not create the output folder" );//$NON-NLS-1$
}
reportDesignHandle.saveAs( outputPath + "/" +
"SDSReport.rptdesign" );//$NON-NLS-1$//$NON-NLS-2$
System.out.println("finished");
}
//Scripted Data Set
private void createDataSources( ) throws SemanticException
{
ScriptDataSourceHandle dataSourceHandle =
elementFactory.newScriptDataSource( "Data Source" );//$NON-NLS-1$
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}
private void createDataSets( ) throws SemanticException
{
// Data Set
ScriptDataSetHandle dataSetHandle = elementFactory.newScriptDataSet( "Data
Set" );//$NON-NLS-1$
dataSetHandle.setDataSource( "Data Source" );//$NON-NLS-1$
// Set open( ) in code
dataSetHandle.setOpen( "i=0;"//$NON-NLS-1$
);//$NON-NLS-1$
// Set fetch( ) in code
dataSetHandle.setFetch( "if ( i < 4 ){"//$NON-NLS-1$
+ "row[\"Month\"] = 1;"//$NON-NLS-1$
+ "row[\"Product\"] = 'My Product';"//$NON-NLS-1$
+ "row[\"Amount\"] = i;"//$NON-NLS-1$
+ "i++;"//$NON-NLS-1$
+ "return true;}" + "else return false;" );//$NON-NLS-1$//$NON-NLS-2$
// Set computed columns
cs1 = StructureFactory.createComputedColumn( );
cs1.setName( "Month" );//$NON-NLS-1$
cs1.setExpression( "row[\"Month\"]" );//$NON-NLS-1$
cs1.setDataType( "integer" );//$NON-NLS-1$
cs2 = StructureFactory.createComputedColumn( );
cs2.setName( "Product" );//$NON-NLS-1$
cs2.setExpression( "row[\"Product\"]" );//$NON-NLS-1$
cs2.setDataType( "string" );//$NON-NLS-1$
cs3 = StructureFactory.createComputedColumn( );
cs3.setName( "Amount" );//$NON-NLS-1$
cs3.setExpression( "row[\"Amount\"]" );//$NON-NLS-1$
cs3.setDataType( "integer" );//$NON-NLS-1$
PropertyHandle computedSet = dataSetHandle.getPropertyHandle(
ScriptDataSetHandle.COMPUTED_COLUMNS_PROP );
computedSet.addItem( cs1 );
computedSet.addItem( cs2 );
computedSet.addItem( cs3 );
reportDesignHandle.getDataSets( ).add( dataSetHandle );
}
/*
private void createDataSources( ) throws SemanticException
{
OdaDataSourceHandle dataSourceHandle =
elementFactory.newOdaDataSource("Data Source",
"org.eclipse.datatools.connectivity.oda.flatfile");
dataSourceHandle.setProperty("HOME", "c:/dwn");
dataSourceHandle.setProperty("CHARSET", "UTF-8");
dataSourceHandle.setProperty("INCLTYPELINE", "NO");
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}
private void createDataSets( ) throws SemanticException
{
// Data Set
OdaDataSetHandle dsHandle = elementFactory.newOdaDataSet( "Data Set",
"org.eclipse.datatools.connectivity.oda.flatfile.dataSet" );
dsHandle.setDataSource( "Data Source" );
dsHandle.setQueryText( "select Date, Open, High, Low from table.csv" );
reportDesignHandle.getDataSets( ).add( dsHandle );
}
*/
private void createMasterPages( ) throws ContentException, NameException
{
DesignElementHandle simpleMasterPage =
elementFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
reportDesignHandle.getMasterPages( ).add( simpleMasterPage );
}
private void createBody( ) throws SemanticException
{
TableHandle table = elementFactory.newTableItem( null, 3, 1, 1, 1 );
table.setProperty( IStyleModel.TEXT_ALIGN_PROP,
DesignChoiceConstants.TEXT_ALIGN_CENTER );
table.setWidth( "80%" );//$NON-NLS-1$
table.setProperty( IReportItemModel.DATA_SET_PROP, "Data
Set" );//$NON-NLS-1$
PropertyHandle computedSet = table.getColumnBindings( );
cs1 = StructureFactory.createComputedColumn( );
cs2 = StructureFactory.createComputedColumn( );
cs3 = StructureFactory.createComputedColumn( );
cs4 = StructureFactory.createComputedColumn( );
cs1.setName("MyMonth");
cs1.setExpression( "dataSetRow[\"Month\"]" );//$NON-NLS-1$
computedSet.addItem( cs1 );
cs2.setName("Product");
cs2.setExpression( "dataSetRow[\"Product\"]" );//$NON-NLS-1$
computedSet.addItem( cs2 );
cs3.setName("Amount");
cs3.setExpression( "dataSetRow[\"Amount\"]" );//$NON-NLS-1$
computedSet.addItem( cs3 );
cs4.setName("TotalAmount");
cs4.setExpression( "Total.sum(dataSetRow[\"Amount\"])" );//$NON-NLS-1$
computedSet.addItem( cs4 );
// Header
RowHandle header = (RowHandle) table.getHeader( ).get( 0 );
CellHandle tcell = (CellHandle) header.getCells( ).get( 0 );
LabelHandle label = elementFactory.newLabel( null );
label.setText( "Month" );//$NON-NLS-1$
tcell.getContent( ).add( label );
tcell = (CellHandle) header.getCells( ).get( 1 );
label = elementFactory.newLabel( null );
label.setText( "Product" );//$NON-NLS-1$
tcell.getContent( ).add( label );
tcell = (CellHandle) header.getCells( ).get( 2 );
label = elementFactory.newLabel( null );
label.setText( "Amount" );//$NON-NLS-1$
tcell.getContent( ).add( label );
DataItemHandle data = null;
// Detail
RowHandle detail = (RowHandle) table.getDetail( ).get( 0 );
tcell = (CellHandle) detail.getCells( ).get( 0 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs1.getName( ) );
tcell.getContent( ).add( data );
tcell = (CellHandle) detail.getCells( ).get( 1 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs2.getName( ) );
tcell.getContent( ).add( data );
tcell = (CellHandle) detail.getCells( ).get( 2 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs3.getName( ) );
tcell.getContent( ).add( data );
RowHandle footer = (RowHandle) table.getFooter( ).get( 0 );
tcell = (CellHandle) footer.getCells( ).get( 2 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs4.getName( ) );
tcell.getContent( ).add( data );
reportDesignHandle.getBody( ).add( table );
}
}
"Ali Khan" <alikhan@xxxxx.com> wrote in message
news:d9e567163d472fca12af174ad73ebd52$1@www.eclipse.org...
> Hello Community,
>
> I am wodering if there are examples our there where the groups are added
> dynamically to a report using DE API and the BIRT functions like Total.ave
> and Total.weightedAve() applied in the group folder,I have had issues
> applying the expressions in the group footer, and I have asked the
> questions many times without getting any reply.
>
> It would be helpful if *ATLEAST* some one from the BIRT community points
> out to an existing example that does so.
>
> Thanks in Advance!
>
|
|
| |
Re: Any Example of adding groups to a table and applying Total Class Fucntions? [message #216818 is a reply to message #216773] |
Fri, 02 February 2007 18:39   |
Eclipse User |
|
|
|
Try the attached code.
Jason
import java.io.File;
import java.io.IOException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TableGroupHandle;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException ;
import org.eclipse.birt.report.model.api.command.ContentException;
import org.eclipse.birt.report.model.api.command.NameException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConst ants;
import org.eclipse.birt.report.model.api.elements.structures.Comput edColumn;
import org.eclipse.birt.report.model.api.metadata.IMetaDataDictiona ry;
import org.eclipse.birt.report.model.elements.interfaces.IReportIte mModel;
import org.eclipse.birt.report.model.elements.interfaces.IStyleMode l;
import org.eclipse.birt.report.model.api.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.ScriptDataSourceHandle;
import com.ibm.icu.util.ULocale;
public class ScriptedDSReport
{
ReportDesignHandle reportDesignHandle = null;
ElementFactory elementFactory = null;
IMetaDataDictionary dict = null;
ComputedColumn cs1, cs2, cs3, cs4, cs5 = null;
public static void main( String[] args ) throws SemanticException,
IOException
{
new ScriptedDSReport( ).createReport( );
}
void createReport( ) throws SemanticException, IOException
{
//Configure the Engine and start the Platform
DesignConfig config = new DesignConfig( );
config.setProperty("BIRT_HOME",
"C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
IDesignEngine engine = null;
try{
Platform.startup( config );
IDesignEngineFactory factory = (IDesignEngineFactory) Platform
.createFactoryObject(
IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
engine = factory.createDesignEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
// Create a new report
reportDesignHandle = session.createDesign( );
// Element factory is used to create instances of BIRT elements.
elementFactory = reportDesignHandle.getElementFactory( );
dict = new DesignEngine( null ).getMetaData( );
createMasterPages( );
createDataSources( );
createDataSets( );
createBody( );
String outputPath = "output";//$NON-NLS-1$
File outputFolder = new File( outputPath );
if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) )
{
throw new IOException( "Can not create the output folder" );//$NON-NLS-1$
}
reportDesignHandle.saveAs( outputPath + "/" +
"SDSReport.rptdesign" );//$NON-NLS-1$//$NON-NLS-2$
System.out.println("finished");
}
//Scripted Data Set
private void createDataSources( ) throws SemanticException
{
ScriptDataSourceHandle dataSourceHandle =
elementFactory.newScriptDataSource( "Data Source" );//$NON-NLS-1$
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}
private void createDataSets( ) throws SemanticException
{
// Data Set
ScriptDataSetHandle dataSetHandle = elementFactory.newScriptDataSet( "Data
Set" );//$NON-NLS-1$
dataSetHandle.setDataSource( "Data Source" );//$NON-NLS-1$
// Set open( ) in code
dataSetHandle.setOpen( "i=0;"//$NON-NLS-1$
);//$NON-NLS-1$
// Set fetch( ) in code
dataSetHandle.setFetch( "if ( i < 20 ){"//$NON-NLS-1$
+ "row[\"Month\"] = 1;"//$NON-NLS-1$
+ "row[\"Product\"] = 'My Product' + parseInt(i/3);"//$NON-NLS-1$
+ "row[\"Amount\"] = i;"//$NON-NLS-1$
+ "i++;"//$NON-NLS-1$
+ "return true;}" + "else return false;" );//$NON-NLS-1$//$NON-NLS-2$
// Set computed columns
cs1 = StructureFactory.createComputedColumn( );
cs1.setName( "Month" );//$NON-NLS-1$
cs1.setExpression( "row[\"Month\"]" );//$NON-NLS-1$
cs1.setDataType( "integer" );//$NON-NLS-1$
cs2 = StructureFactory.createComputedColumn( );
cs2.setName( "Product" );//$NON-NLS-1$
cs2.setExpression( "row[\"Product\"]" );//$NON-NLS-1$
cs2.setDataType( "string" );//$NON-NLS-1$
cs3 = StructureFactory.createComputedColumn( );
cs3.setName( "Amount" );//$NON-NLS-1$
cs3.setExpression( "row[\"Amount\"]" );//$NON-NLS-1$
cs3.setDataType( "integer" );//$NON-NLS-1$
PropertyHandle computedSet = dataSetHandle.getPropertyHandle(
ScriptDataSetHandle.COMPUTED_COLUMNS_PROP );
computedSet.addItem( cs1 );
computedSet.addItem( cs2 );
computedSet.addItem( cs3 );
reportDesignHandle.getDataSets( ).add( dataSetHandle );
}
/*
private void createDataSources( ) throws SemanticException
{
OdaDataSourceHandle dataSourceHandle =
elementFactory.newOdaDataSource("Data Source",
"org.eclipse.datatools.connectivity.oda.flatfile");
dataSourceHandle.setProperty("HOME", "c:/dwn");
dataSourceHandle.setProperty("CHARSET", "UTF-8");
dataSourceHandle.setProperty("INCLTYPELINE", "NO");
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}
private void createDataSets( ) throws SemanticException
{
// Data Set
OdaDataSetHandle dsHandle = elementFactory.newOdaDataSet( "Data Set",
"org.eclipse.datatools.connectivity.oda.flatfile.dataSet" );
dsHandle.setDataSource( "Data Source" );
dsHandle.setQueryText( "select Date, Open, High, Low from table.csv" );
reportDesignHandle.getDataSets( ).add( dsHandle );
}
*/
private void createMasterPages( ) throws ContentException, NameException
{
DesignElementHandle simpleMasterPage =
elementFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
reportDesignHandle.getMasterPages( ).add( simpleMasterPage );
}
private void createBody( ) throws SemanticException
{
TableHandle table = elementFactory.newTableItem( null, 3, 1, 1, 1 );
table.setProperty( IStyleModel.TEXT_ALIGN_PROP,
DesignChoiceConstants.TEXT_ALIGN_CENTER );
table.setWidth( "80%" );//$NON-NLS-1$
table.setProperty( IReportItemModel.DATA_SET_PROP, "Data
Set" );//$NON-NLS-1$
PropertyHandle computedSet = table.getColumnBindings( );
cs1 = StructureFactory.createComputedColumn( );
cs2 = StructureFactory.createComputedColumn( );
cs3 = StructureFactory.createComputedColumn( );
cs4 = StructureFactory.createComputedColumn( );
cs5 = StructureFactory.createComputedColumn( );
cs1.setName("MyMonth");
cs1.setExpression( "dataSetRow[\"Month\"]" );//$NON-NLS-1$
computedSet.addItem( cs1 );
cs2.setName("Product");
cs2.setExpression( "dataSetRow[\"Product\"]" );//$NON-NLS-1$
computedSet.addItem( cs2 );
cs3.setName("Amount");
cs3.setExpression( "dataSetRow[\"Amount\"]" );//$NON-NLS-1$
computedSet.addItem( cs3 );
cs4.setName("TotalAmount");
cs4.setExpression( "Total.sum(dataSetRow[\"Amount\"])" );//$NON-NLS-1$
computedSet.addItem( cs4 );
cs5.setName("GroupTotalAmount");
cs5.setExpression( "Total.sum(dataSetRow[\"Amount\"])" );//$NON-NLS-1$
cs5.setAggregateOn("MyGroup");
computedSet.addItem( cs5 );
// Header
RowHandle header = (RowHandle) table.getHeader( ).get( 0 );
CellHandle tcell = (CellHandle) header.getCells( ).get( 0 );
LabelHandle label = elementFactory.newLabel( null );
label.setText( "Month" );//$NON-NLS-1$
tcell.getContent( ).add( label );
tcell = (CellHandle) header.getCells( ).get( 1 );
label = elementFactory.newLabel( null );
label.setText( "Product" );//$NON-NLS-1$
tcell.getContent( ).add( label );
tcell = (CellHandle) header.getCells( ).get( 2 );
label = elementFactory.newLabel( null );
label.setText( "Amount" );//$NON-NLS-1$
tcell.getContent( ).add( label );
// Table Group
TableGroupHandle group = elementFactory.newTableGroup( );
group.setName("MyGroup");
group.setKeyExpr( "row[\"Product\"]" );//$NON-NLS-1$
table.getGroups( ).add( group );
RowHandle groupHeader = elementFactory.newTableRow( 3 );
tcell = (CellHandle) groupHeader.getCells( ).get( 0 );
tcell.setDrop( DesignChoiceConstants.DROP_TYPE_DETAIL );
DataItemHandle data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs2.getName( ) );
tcell.getContent( ).add( data );
group.getHeader( ).add( groupHeader );
RowHandle groupFooter = elementFactory.newTableRow( 3 );
tcell = (CellHandle) groupFooter.getCells( ).get( 2 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs5.getName( ) );
tcell.getContent( ).add( data );
group.getFooter( ).add( groupFooter );
// Detail
RowHandle detail = (RowHandle) table.getDetail( ).get( 0 );
tcell = (CellHandle) detail.getCells( ).get( 0 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs1.getName( ) );
tcell.getContent( ).add( data );
tcell = (CellHandle) detail.getCells( ).get( 1 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs2.getName( ) );
tcell.getContent( ).add( data );
tcell = (CellHandle) detail.getCells( ).get( 2 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs3.getName( ) );
tcell.getContent( ).add( data );
RowHandle footer = (RowHandle) table.getFooter( ).get( 0 );
tcell = (CellHandle) footer.getCells( ).get( 2 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs4.getName( ) );
tcell.getContent( ).add( data );
reportDesignHandle.getBody( ).add( table );
}
}
"Ali Khan" <alikhan@xxxxx.com> wrote in message
news:25817326e7424e57e8daea210bbca32b$1@www.eclipse.org...
> Thanks for the reply Jason!
>
> I did a similar thing as in the example you posted, i.e get the
> columnbindings of the Table and set the expression of teh computed column
> to the Total.count(), but it gives me the total of the number of rows in
> the table.
>
> Then I changed my approach to get the columnbindigs of the group by using
> TableGroupHangle.getColumnBindings(), first of all its deprecated and
> secondly it always returns a null.
>
> Do I have to use the column bindigs of the group or of the table , if I
> need to use the column bindings of the table then how can I modify the
> expression to give me the total count of teh rows in group rather than in
> the whole table.
>
> Please give some ideas.
>
> Thanks again!
>
> Ali Khan.
>
|
|
| |
Goto Forum:
Current Time: Wed May 07 11:36:36 EDT 2025
Powered by FUDForum. Page generated in 0.06804 seconds
|