Wednesday, April 4, 2012

BIRT :: Tips&Tricks

Tips and tricks for using BIRT functions.

Contents

  • 1 Get Parameters Value/Display Text
  • 2 Cascaded Parameters
  • 3 Get query text from data set
  • 4 Dynamic query
  • 5 Get the first day of month
  • 6 Get the last day of previous month
  • 7 Get the previous quarter day (since now)
  • 8 Display the values of a Multiple Values ListBox parameter
  • 9 Pass Multiple values from List Box to a query
  • 10 Increase Page Break Rows
  • 11 Drill-through
  • 12 Dynamic Chart Size
  • 13 Rownum for sorted crosstab
  • 14 Internet explorer (IE) compatibility problem
  • 15 External Links with tips

Get Parameters Value/Display Text


BIRT Parameters are stored in a params table.

You can get a value from the table using

params["parameter_name"].value
You can get the Display Text of a value using

params["parameter_name"].displayText

Cascaded Parameters

When the user selects the first level parameter in a cascaded parameter group the second level in the group is automatically re-queried.

Cascaded Parameters


Get query text from data set

You can get the query string using

this.queryText

Dynamic query


1. On initialize script add the below JavaScript query

function buildWhereClause(paramName, fieldName) {
 var paramVal = reportContext.getParameterValue(paramName); 
 
 if (paramVal == null || paramVal == "ALL" || paramVal == "") {
  return "";
 } else {
      return " where "+ fieldName + "= '" + paramVal +"'"; 
 }
 }

2. On the beforeOpen script of the main data set, where your main query is, add the below JavaScript

// Parameters are used to create a dynamic conditional clause. 
// The buildWhereClause function is in the Initialize of the report. 

var where = " ";

where += buildWhereClause("fieldName", "paramVal");

var sql = "";
 sql += " REWRITE YOUR MAIN QUERY HERE ";
    sql += where;
     
this.queryText = sql;
In fieldName and paramVal add the name of your query filed (participating in the main query) and the name of the Parameter you use for filtering.


Get the first day of month

Use the following expression to get it

BirtDateTime.addDay(BirtDateTime.today(),1-BirtDateTime.day( (BirtDateTime.today()) ))

Get the last day of previous month


Use the following expression to get it

BirtDateTime.addDay(BirtDateTime.today(),-BirtDateTime.day( (BirtDateTime.today()) ))

Get the previous quarter day (since now)

BirtDateTime.addDay(BirtDateTime.addQuarter(BirtDateTime.today(),-2),-BirtDateTime.day( (BirtDateTime.today()) )+1)

Display the values of a Multiple Values ListBox parameter


Just place a data value field in your report, set the data Type of the parameter as Java Object and the parameter expression params["parameter_name"].value.

Pass Multiple values from List Box to a query

On the before open script add the bellow function to rewrite the query with a where clause.

function MultiParameters(parameter){
 var parmcount = parameter.value.length;
 var whereclause = "";

 whereclause = " where code in ( '";
 for ( i=0; i < parmcount; i++ ){
  if( i == 0 ){
   whereclause = whereclause + parameter.value[i];
  } else {
   whereclause = whereclause + "' , '" + parameter.value[i];
  }
 }
 whereclause = whereclause + "' ) ";
 
 return whereclause;
}


this.queryText  = "select * from invoices ";
this.queryText  = this.queryText  + MultiParameters(params["department"]);

Increase Page Break Rows

Select the table from the report, go to Properties -> Page Break and in Page Break Interval add more rows.

Drill-through


Select the element from the table, on property editor Hyperlink clik edit, select Drill-through, select the Report Design, enter the Report Parameters and any other possible options and click ok.

In case the path of the drilled through report is not correct or not working try changing the WORKING_FOLDER_ACCESS_ONLY to false in the web.xml (/var/lib/tomcat5/webapps/birtXXX/WEB-INF/web.xml) and restart tomcat.


This happens because the working directory (defined in web.xml) is not exactly the same case as the actual directory where your reports are located.

Dynamic Chart Size

In this example we resize the chart based on output format.

If you want the chart to resize based on the data populated in the chart data sets it requires a little bit of a work around. First, name the chart as with the simple chart API and enter the following code in your beforeFactory event handler.

var mychart = this.getReportElement( "mychart" );
mychart.setWidth("");
mychart.setHeight("");



After you have done that, enter code similar to the following in your afterDataSetFilled chart event handler.

function afterDataSetFilled(series, dataSet, icsc)
{
if( series.getSeriesIdentifier() == "seriesone" ){
if( dataSet.getValues().length > 4 ){
 icsc.getChartInstance().getBlock().getBounds().setWidth(800);
 icsc.getChartInstance().getBlock().getBounds().setHeight(600);
}else{
 icsc.getChartInstance().getBlock().getBounds().setWidth(400);
 icsc.getChartInstance().getBlock().getBounds().setHeight(300);

}
}
}
In this example we have named our series “seriesone” on the third tab of the chart wizard.

If the category values contain more than four entries we double the size of the chart. The sizing could be more dynamic, for example we could use the list size to increment a delta per category as well.

This example works with BIRT 2.2.2, but will not work with the chart output type set to SVG, when rendering to HTML.

Source: Resizing Charts


Rownum for sorted crosstab

Put something like: var i = 0, in your initialize script.

Then, instead of trying to use your rownum field as a dimension, just put a 2 column / 1 row grid in where your name field is.

Put the name field in the right cell and a dynamic textbox in the left cell. For the dynamic textbox expression, put i++; i;.

Now, for each row, the variable i will increment and then display in the textbox, giving you a row number.

Source: Rownum for sorted crosstab

Internet explorer (IE) compatibility problem


In case Internet explorer needs compatibility mode to show the report, try adding the following code on <head> section to the
webapps/birt/webcontent/birt/pages/layout/FramesetFragment.jsp (line 46)

<!-- Mimics Internet Explorer 7, it just works on IE8. -->
<META HTTP-EQUIV="X-UA-Compatible" CONTENT="IE=EmulateIE7">

External Links with tips

Tips&Tricks

2 comments:

Unknown said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.