Dynamcially generating a crosstab (dimensions, measures and adding totals)
Hello to all,<br />
<br />
I recently started working with the open source version of BIRT 2.5.x so I am really new therefore forgive if the question is not appropriate, but I am kind of lost.<br />
<br />
I am trying to develop a ?dynamic crosatab? (ie scripted). Reading through the forum I was able to piece together the javascript code to allow at runtime inserting a crosstab, dimensions X, Y and measures. In nutshell the user selects dimension and measures on a web pages which in turn are passed as parameters to the BIRT report. The data source, data set and cube is already predefined in the report. When the user clicks execute code in the ?beforeFactory? is that creates the crosstab on the fly. <br />
<br />
Now I have some additional requirement but for some reason I am not able to find helpful information or maybe I just don?t get it. I am sure someone must have had a similar requirement in the past<br />
<br />
<span style='color: #8B0000'>The user also needs the ability to decide in the web page whether a particular dimension need to have ?aggregate totals? for ?row area? and ?column area?. I cannot figure out what objects and methods sequence I need to invoke in order to create at runtime the aggregate rows or columns.</span><br />
<br />
Thank you in advance for any guidance or sample code that can point me in the right direction<br />
<br />
Thanks<br />
<br />
Max<br />
<br />
Here is the code I have pieced together located in the ?beforeFactory? factory event<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage( Packages.org.eclipse.birt.report.model.api );
importPackage( Packages.org.eclipse.birt.report.item.crosstab.core.util);
importPackage( Packages.org.eclipse.birt.report.item.crosstab.core.de);
importPackage( Packages.org.eclipse.birt.report.item.crosstab.core);
importPackage( Packages.java.io );
var out = new PrintWriter( new FileWriter( "c:/log.txt", false ) );
out.println("start...");
try{
var reportDesignHandle = reportContext.getReportRunnable().designHandle.getDesignHandle();
// Extract report parameters used in creating X, Y Diemnsions and measures
var s = reportContext.getParameterValue("rp_xDimensions");
var xDimensions = s.split(",");
var s = reportContext.getParameterValue("rp_yDimensions");
var yDimensions = s.split(",");
var s = reportContext.getParameterValue("rp_measures");
var measures = s.split(",");
var elementFactory = reportDesignHandle.getElementFactory();
var cubeHandle = reportDesignHandle.getCubes().get(0);
var xtab = CrosstabExtendedItemFactory.createCrosstabReportItem(reportDesignHandle, cubeHandle, "MyCrosstab" );
reportDesignHandle.getBody().add(xtab);
reportItem = xtab.getReportItem( );
xTabHandle = reportItem;
// Create X dimensions
for (i = 0; i < xDimensions.length; i++) {
insertDimension(xTabHandle, cubeHandle, ICrosstabConstants.ROW_AXIS_TYPE, xDimensions[i], i);
}
// Create X dimensions
for (i = 0; i < yDimensions.length; i++) {
insertDimension(xTabHandle, cubeHandle, ICrosstabConstants.COLUMN_AXIS_TYPE, yDimensions[i], i);
}
// Create measures
for (i = 0; i < measures.length; i++) {
insertMeasure(xTabHandle, cubeHandle, measures[i], i);
}
out.println("end...");
}catch(e){
out.println(e);
}
out.close();
// *****************************************
// * Create xTab dimensions for rows (X - ROW_AXIS_TYPE) and columns (Y - COLUMN_AXIS_TYPE)
// *
// *****************************************
function insertDimension(xTabHandle, cubeHandle, dimensionType, dimensionName, order) {
out.println("Begin insertDimension...");
var dimension = cubeHandle.getDimension(dimensionName);
var level = dimension.getDefaultHierarchy().getLevel(0);
var dvh = xTabHandle.insertDimension(dimension, dimensionType, order);
var levelViewHandle = dvh.insertLevel(level, order);
var cellHandle = levelViewHandle.getCell( );
var eii = xTabHandle.getModelHandle( );
var bindingColumn = StructureFactory.newComputedColumn( eii, level.getName( ) );
var bindingHandle = eii.addColumnBinding( bindingColumn,false );
bindingColumn.setDataType( level.getDataType( ) );
var exp = "dimension['" + dimension.getName()+"']['"+level.getName()+"']";
out.println("exp: " +exp);
bindingColumn.setExpression( exp);
var dataHandle = elementFactory.newDataItem( level.getName( ) );
dataHandle.setResultSetColumn( bindingHandle.getName( ) );
dataHandle.setProperty("style", "xDimensionStyle");
cellHandle.addContent( dataHandle );
out.println("End insertDimension...");
}
// *****************************************
// * Insert measure
// *****************************************
function insertMeasure(xTabHandle, cubeHandle, measureName, order) {
out.println("Begin insertMeasure...");
var measure = cubeHandle.getMeasure(measureName);
var mvh = xTabHandle.insertMeasure(measure, order);
mvh.getCell().getContents().get(0).setProperty("style", "myValues");
mvh.addHeader( );
labelHandle = elementFactory.newLabel( null );
labelHandle.setText( measure.getDisplayName() );
mvh.getHeader( ).addContent( labelHandle );
out.println("End insertMeasure...");
}
</pre>