Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Extending the report on runtime
kosta
Hello BIRT community,
I have another question about the report engine.
We want to use BIRT for many hundred different kind reports. They design of the report's is almost similar but only almost. We don't want to make for every case one rptdesign file because if one day the main layout should change then we must change many hundersts rptdesign files. On other side putting all extra design components in one rptdesign file and hiding them will make the file very huge.
Is there any solution where I can use the general information in one rptdesign file and then for every case adding the extra component's on runtime.
Is there maybe some other solution for this issue?
Thanks in advance.
Find more posts tagged with
Comments
JasonW
You could always call the de api from the beforeFactory script. Take a look at the attached example.
Jason
kosta
Thank you Jason for the example,<br />
<br />
i tired to use the example where dynamically are added new columns.<br />
<a class='bbc_url' href='
http://www.birt-exchange.org/org/devshare/designing-birt-reports/1099-add-column-to-table-dynamically/'>http://www.birt-exchange.org/org/devshare/designing-birt-reports/1099-add-column-to-table-dynamically/</a><br
/>
<br />
The script used there is like this:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage( Packages.org.eclipse.birt.report.model.api );
elementFactory = reportContext.getDesignHandle().getElementFactory()
var mytable = reportContext.getDesignHandle().findElement("mytable");
var colbinds = mytable.getColumnBindings( );
var cs1 = StructureFactory.createComputedColumn( );
cs1.setName("ORDERLINENUMBER");
cs1.setExpression( "dataSetRow[\"ORDERLINENUMBER\"]" );
colbinds.addItem( cs1 );
var colPosition = 2;
var afterBeforeFlag = 1; // -1 or 1
var cellPosition = 0;
if(afterBeforeFlag == -1) {
cellPosition = colPosition - 1;
} else {
cellPosition = colPosition;
}
//second parameter is before(-1) or after(1)
mytable.insertColumn(colPosition,afterBeforeFlag);
//get first detail row
mydetail = mytable.getDetail( ).get( 0 );
//get first column and add detail data item
var tcell = mydetail.getCells( ).get( cellPosition );
var mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "ORDERLINENUMBER");
tcell.getContent( ).add( mydata );
//get header and add label
var myheader = mytable.getHeader( ).get( 0 );
tcell = myheader.getCells( ).get( cellPosition );
var mylabel = elementFactory.newLabel( null );
mylabel.setText( "ScriptColumn" );//$NON-NLS-1$
tcell.getContent( ).add( mylabel );
var colPosition = 4;
var afterBeforeFlag = 1; // -1 or 1
var cellPosition = 0;
if(afterBeforeFlag == -1) {
cellPosition = colPosition - 1;
} else {
cellPosition = colPosition;
}
//second parameter is before(-1) or after(1)
mytable.insertColumn(colPosition,afterBeforeFlag);
//get first detail row
mydetail = mytable.getDetail( ).get( 0 );
//get first column and add detail data item
var tcell = mydetail.getCells( ).get( cellPosition );
var mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "PRODUCTCODE");
tcell.getContent( ).add( mydata );
//get header and add label
var myheader = mytable.getHeader( ).get( 0 );
tcell = myheader.getCells( ).get( cellPosition );
var mylabel = elementFactory.newLabel( null );
mylabel.setText( "ScriptColumn" );//$NON-NLS-1$
tcell.getContent( ).add( mylabel );
</pre>
<br />
Can you tell me how can I set the width of the old columns and then on the new columns as well.<br />
<br />
And how can I put the contents into a text report items in HTML mode, because the content is too large and new line should be created for it.<br />
<br />
Thank you.
JasonW
Kosta,
Use a ColumnHandle to set width. Like:
ColumnHandle ch = (ColumnHandle)tablehandle.getColumns().get(0);
ch.setProperty("width", "50%");
or
ColumnHandle ch = (ColumnHandle)tablehandle.getColumns().get(0);
ch.setProperty("width", "1in");
To create a text item and set its type to html do this:
TextItemHandle text = elementFactory.newTextItem( null );
text.setContentType( DesignChoiceConstants.TEXT_CONTENT_TYPE_HTML );
text.setContent( "<br><b>My HTML</b></br>" );
Jason
kosta
Thank you Jason,<br />
<br />
I could use the code in the rptdesign script area like this:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
mytable.getColumns().get(0).setProperty("width", "7%");
mytable.getColumns().get(0).setProperty("textAlign", "center");
mytable.getColumns().get(1).setProperty("width", "35%");
mytable.getColumns().get(2).setProperty("width", "7%");
mytable.getColumns().get(2).setProperty("textAlign", "center");
...
</pre>
<br />
What about number formatting. I tried to use something like this:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
mytable.getColumns().get(2).setNumberFormat("###0.##");
</pre>
but this doesn't work.<br />
<br />
I know the following script works when used in the script area (onCreate) of the "Data"-Element<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
if (params["test"] == "1" || params["test"] == "2") {
this.getStyle().numberFormat = "###0.##";
} else {
this.getStyle().numberFormat = "###0";
}
</pre>
<br />
Which element's can have defined number format and how do I do that?<br />
<br />
Also it would be cool to know if and how charts can be created on runtime.<br />
<br />
Thank you in advance.
JasonW
Kosta,
You need to set the number formatting on the data element not the column.
To build a report with a chart in it use code like:
package DEAPI;
import java.io.IOException;
import java.util.Iterator;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.DataType;
import org.eclipse.birt.chart.model.attribute.GroupingUnitType;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.SortOption;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
import org.eclipse.birt.chart.model.data.Query;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.SeriesGrouping;
import org.eclipse.birt.chart.model.data.impl.QueryImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
import org.eclipse.birt.chart.model.layout.Plot;
import org.eclipse.birt.chart.model.type.LineSeries;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.script.ScriptExpression;
import org.eclipse.birt.data.engine.api.querydefn.ComputedColumn;
import org.eclipse.birt.report.model.api.ComputedColumnHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignFileException;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.SimpleMasterPageHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
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.structures.PropertyBinding;
import com.ibm.icu.util.ULocale;
public class SimpleChart
{
private ReportDesignHandle reportDesignHandle = null;
private ElementFactory elementFactory = null;
private OdaDataSourceHandle odaDataSourceHandle = null;
private String dataSourceName = "datasource";
private String dataSetName = "maindataset";
private SessionHandle sessionHandle =null;
private String query = "SELECT SIMS.ROLES.\"CREATOR\", SIMS.ROLES.\"RID\" FROM SIMS.ROLES";
org.eclipse.birt.report.model.api.elements.structures.ComputedColumn cs1, cs2 = null;
public static void main(String args[])
{
try {
new SimpleChart().createReport();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createReport() throws SemanticException, IOException
{
System.out.println("Start");
init();
createMasterPages();
//createDataSources();
//createDataSets();
buildDataSource();
buildDataSet();
createBody();
String outputPath = "C:/work";
reportDesignHandle.saveAs("output/desample/simplechart.rptdesign");
try
{
reportDesignHandle = sessionHandle.openDesign( "output/desample/simplechart.rptdesign" );//$NON-NLS-1$
}
catch ( DesignFileException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}
ExtendedItemHandle eih = (ExtendedItemHandle) reportDesignHandle.getBody( )
.getContents( ).get( 0 );
for ( Iterator iter = eih.getColumnBindings( )
.iterator( ); iter.hasNext( ); )
{
ComputedColumnHandle cch = (ComputedColumnHandle) iter.next( );
//if( cch.getExpression().contains("_Sum") ){
//cch.drop();
//}else{
System.out.println("opened report "+cch.getExpression());
//}
}
//reportDesignHandle.save();
System.out.println("End");
}
private void init(){
DesignConfig config = new DesignConfig( );
config.setBIRTHome("C:/birt/birt-runtime-2_5_0/birt-runtime-2_5_0/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();
}
// we need a handle of session of design engine
sessionHandle = engine.newSessionHandle(ULocale.ENGLISH);
reportDesignHandle = sessionHandle.createDesign();
elementFactory = reportDesignHandle.getElementFactory();
}
private void createMasterPages() throws ContentException, NameException
{
SimpleMasterPageHandle simpleMasterPage =
elementFactory.newSimpleMasterPage("Master Page");
reportDesignHandle.getMasterPages().add(simpleMasterPage);
}
void buildDataSource( ) throws SemanticException
{
OdaDataSourceHandle dsHandle = elementFactory.newOdaDataSource(
"Data Source", "org.eclipse.birt.report.data.oda.jdbc" );
dsHandle.setProperty( "odaDriverClass",
"org.eclipse.birt.report.data.oda.sampledb.Driver" );
dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" );
dsHandle.setProperty( "odaUser", "ClassicModels" );
dsHandle.setProperty( "odaPassword", "" );
PropertyBinding pb = new PropertyBinding();
reportDesignHandle.getDataSources( ).add( dsHandle );
}
void buildDataSet( ) throws SemanticException
{
OdaDataSetHandle dsHandle = elementFactory.newOdaDataSet( dataSetName,
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle.setDataSource( "Data Source" );
String qry = "Select PRODUCTCODE, QUANTITYORDERED from orderdetails where ordernumber = 10104";
dsHandle.setQueryText( qry );
reportDesignHandle.getDataSets( ).add( dsHandle );
}
private void createBody() throws SemanticException
{
ExtendedItemHandle extendedItemHandle = elementFactory.newExtendedItem("Simple Chart", "Chart");
extendedItemHandle.setWidth("700px");
extendedItemHandle.setHeight("500px");
extendedItemHandle.setProperty(ExtendedItemHandle.DATA_SET_PROP, dataSetName);
extendedItemHandle.setProperty("outputFormat","GIF");
Chart c = createChart();
extendedItemHandle.getReportItem().setProperty( "chart.instance", c );
reportDesignHandle.getBody().add(extendedItemHandle);
//PropertyHandle computedSet = extendedItemHandle.getColumnBindings( );
//computedSet.clearValue();
cs1 = StructureFactory.createComputedColumn( );
cs1.setName( "PRODUCTCODE" );
cs1.setExpression( "dataSetRow[\"PRODUCTCODE\"]");
cs1.setDataType( "string" );
cs1.setAggregateOn(null);
cs2 = StructureFactory.createComputedColumn( );
cs2.setName( "QUANTITYORDERED" );
cs2.setExpression( "dataSetRow[\"QUANTITYORDERED\"]");
cs2.setDataType( "integer" );
extendedItemHandle.addColumnBinding(cs1, true);
extendedItemHandle.addColumnBinding(cs2, true);
}
private Chart createChart() {
ChartWithAxes cwaLine = ChartWithAxesImpl.create();
cwaLine.setType( "Line Chart" ); //$NON-NLS-1$
cwaLine.setSubType( "Overlay" ); //$NON-NLS-1$
cwaLine.getBlock().getBounds().setWidth(600);
cwaLine.getBlock().getBounds().setHeight(400);
// Plot
cwaLine.getBlock().setBackground( ColorDefinitionImpl.WHITE() );
Plot p = cwaLine.getPlot();
p.getClientArea().setBackground( ColorDefinitionImpl.create( 255, 255,
225 ) );
// Title
cwaLine.getTitle().getLabel().getCaption().setValue("Overlay test Line Chart" );
cwaLine.getTitle().setVisible(true);
// Legend
cwaLine.getLegend().setVisible( true );
// X-Axis
Axis xAxisPrimary = cwaLine.getPrimaryBaseAxes()[0];
xAxisPrimary.setType( AxisType.TEXT_LITERAL );
//xAxisPrimary.getMajorGrid().setTickStyle( TickStyle.BELOW_LITERAL );
//xAxisPrimary.getOrigin().setType( IntersectionType.MIN_LITERAL );
// Y-Axis
Axis yAxisPrimary = cwaLine.getPrimaryOrthogonalAxis( xAxisPrimary );
yAxisPrimary.setType(AxisType.LINEAR_LITERAL);
//yAxisPrimary.getMajorGrid().setTickStyle( TickStyle.RIGHT_LITERAL );
//yAxisPrimary.getLabel().getCaption().setValue("TEST");
yAxisPrimary.getLabel().setVisible(true);
// X-Series
Series seCategory = SeriesImpl.create( );
// seCategory.setDataSet( categoryValues );
// Set category expression.
seCategory.getDataDefinition( )
.add( QueryImpl.create( "row[\"PRODUCTCODE\"]" ) );
SeriesDefinition sdX = SeriesDefinitionImpl.create( );
sdX.getSeriesPalette( ).shift( 0 );
sdX.setSorting(SortOption.ASCENDING_LITERAL);
// Set default grouping.
SeriesGrouping grouping = sdX.getGrouping( );
grouping.getAggregateExpression();
grouping.setEnabled( true );
grouping.setGroupType( DataType.TEXT_LITERAL );
grouping.setGroupingUnit( GroupingUnitType.STRING_PREFIX_LITERAL );
grouping.setGroupingInterval( 1 );
grouping.setAggregateExpression( "Sum" ); // Set Count aggregation. //$NON-NLS-1$
xAxisPrimary.getSeriesDefinitions( ).add( sdX );
sdX.getSeries( ).add( seCategory );
// Y-Series
LineSeries bs1 = (LineSeries) LineSeriesImpl.create( );
bs1.getDataDefinition( ).add( QueryImpl.create( "row[\"QUANTITYORDERED\"]" ) );
bs1.getLabel( ).setVisible( true );
SeriesDefinition sdY = SeriesDefinitionImpl.create( );
sdY.getGrouping().setEnabled(false);
yAxisPrimary.getSeriesDefinitions( ).add( sdY );
sdY.getSeries( ).add( bs1 );
return cwaLine;
}
}
Jason
kosta
Hi Jason,<br />
<br />
I tried to put the "numberFormat" to some data item but it don't worked for me.<br />
This I ll get when I am using the Report Design<br />
the rptdesign file as XML<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
...
<cell id="931">
<data id="950">
<structure name="numberFormat">
<property name="category">Fixed</property>
<property name="pattern">###0{RoundingMode=HALF_UP}</property>
</structure>
<property name="whiteSpace">nowrap</property>
<property name="resultSetColumn">RawPosition</property>
</data>
</cell>
...
</pre>
<br />
The "numberFormat" is not directly to under the data tag but in "structure".<br />
I couldn't find out what kind of BIRT Object the "structure" is.<br />
<br />
I have tried with propertyMask like:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
PropertyMask pm = StructureFactory.createPropertyMask();
pm.setProperty("category", "Fixed");
pm.setProperty("pattern", "###0{RoundingMode=FLOOR}");
data.setProperty("numberFormat", pm);
</pre>
and also PropertyHandle. I have searched for more "StructureFactory" methods but no one makes the rptdesign file like when I am using the Report Design.<br />
<br />
How can I create the structure-Tag with the properties into it?<br />
Which Object is the right one for doing this.<br />
<br />
Thanks in advance.
JasonW
Try:
FormatValue fv = structFactory.newFormatValue();
fv.setPattern("#,##0.00{RoundingMode=HALF_UP}");
fv.setCategory("custom");
data.setProperty("numberFormat", fv);
Jason
kosta
Thank you Jason,<br />
<br />
this one did it.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
FormatValue fv = StructureFactory.newFormatValue();
fv.setPattern("#,##0.00{RoundingMode=HALF_UP}");
fv.setCategory("custom");
data.setProperty("numberFormat", fv);
</pre>
kosta
Hi, Jason.<br />
<br />
what about hiding rows on some rule?<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
HideRule hr = StructureFactory.createHideRule();
tabledetail.setProperty("visibility", hr );
</pre>
<br />
gives error "invalid with the type 'structure'.". I have also tried Rule and ExtendedProperty but also got same errors.<br />
Do you have some example on this one.<br />
<br />
Thanks.
JasonW
Kosta,
I am attaching an example that I use to test de api calls. It has an example of adding a highlight rule.
Jason
kosta
Thank you for the script.
Lots of thing can be found there like embedding image or creating different rules.
In code I saw you are adding "test.js" to the report as "includeScripts" property.
What is that and when can I use this.
Does this means that I can use some JavaScript functionality from external files?
What about css files. I also saw some rptdesign files with css in a zip. How can I use the css classes in BIRT. Can they be used only in HTML text fields or also in the report items?
Thank you.
JasonW
Kosta,
The includeScripts allows me to include .js files from the resource folder in the execution of the report. The js file can contain variables and functions, but keep in mind these are for server side running, not client side.
With css files you can import them to a report design or link to them externally. They effect all birt outputs but not all css properties are supported.
Jason