Tuesday, July 31, 2012

Using a Global JS Function across BIRT Reports

BIRT provides a scripting model that allows report customizations by implementing event handlers.  These events can be written in Java or JavaScript.  This model is described on the Eclipse Birt Site.


We have written many posts on using scripting to modify BIRT report components.  Below are just a few:

In this post we will describe one way that you can share a server side JavaScript function across reports.  Suppose that you have a JavaScript function to reverse a string like:

//external js function
function reverseMyString( MyString )
{
 var rString = "";
 for (i = 0; i < MyString.length; i++)
 {
  rString = MyString.substring(i, i+1) + rString;
 }

 return rString;
}



This function can be put in a .js file and then placed in the BIRT resource folder.  If you do not have a resource folder configured for your BIRT project, it can be set from the designer window preferences dialog.


The js file can be added to the report by selecting the general properties for the report and clicking on the add file button under Javascript Files.

The global function can now be called in the expression builder or in script.
The evaluate function within the reportContext object can also be used to evaluate your script at runtime.
var testString = "ZYXWVU";
this.text = reportContext.evaluate("reverseMyString('"+testString +"')");
Using this same method, a handle to the function can also be retrieved.
var testString = "ZYXWVU";
//Evaluate Function Name
var myfunc = reportContext.evaluate("reverseMyString");
this.text = myfunc( testString );
When writing Chart script it is important to understand that the Chart Engine’s Script Context is not the same as the reports.  The Chart Engine also does the bulk of its generation and rendering during the report engine’s render phase.  Chart script events can get access to the reportContext object by using the following script.
//get reportContext
var rC = icsc.getExternalContext().getScriptable();
Once the reportContext object is obtained you can make all of the standard calls available to it.  These include getting a report parameters values, getting or setting a global variable, retrieving a localized message or calling the evaluate function.  For example, to reverse the chart title the following script could be used.
function beforeGeneration( chart, icsc )
{
var currChartTitle = chart.getTitle().getLabel().getCaption().getValue()+"";
//get reportContext
var rC = icsc.getExternalContext().getScriptable();
var myFunc = rC.evaluate( "reverseMyString" );
chart.getTitle().getLabel().getCaption().setValue(myFunc( currChartTitle ));
}
Or you could create a global js function in your js file that takes the chart as a parameter and reverses its title like:
//external js function
function reverseMyTitle( chart )
{
    var MyString = chart.getTitle().getLabel().getCaption().getValue() + "";
    var rString = "";
    for (i = 0; i < MyString.length; i++)
    {
        rString = MyString.substring(i, i+1) + rString;
    }
    chart.getTitle().getLabel().getCaption().setValue( rString);
} 
You could then call this function in chart script as shown below.
function beforeGeneration( chart, icsc )
{
//get reportContext
var rC = icsc.getExternalContext().getScriptable();
var myFunc = rC.evaluate( "reverseMyTitle" );
myFunc( chart );
}




18 comments:

Anonymous said...

gives Javascript function reference error when the same is executed in web viewer.
any help is appreciated.
thanks.

Jason Weathersby said...

Do you mean when the viewer is deployed? Did you set the resource folder?

yashikawebdesigninghouse said...

This post provides a very clear explanation of how to use JavaScript functions in BIRT reports. Thanks for the detailed examples!
modular workstation gurgaon
office chair in delhi

muskan said...

Can you provide more information about how to configure the resource folder for a BIRT project?
cafeteria-furniture gurgaon
metal storage system in gurgaon

kajal said...

Sharing JavaScript functions across reports is a powerful feature. Is there a performance impact when using external .js files?
Shrink Packing machine India
Shrink wrapping machine India

abhay said...

The example with reversing strings is simple yet effective. Can you share more real-world use cases for such global functions?
Box Wrapping machine manufacturer
Strapping machine manufacturer

Bhanu said...

What are some common debugging challenges when working with external JavaScript files in BIRT, and how can they be addressed?
franchise Expo
Argan Oil Manufacturer in germany

gautam said...

The script for reversing chart titles is very creative. Could similar scripts be used to dynamically format other chart elements?
Dust collector manufacturer
Office Furniture delhi

karishma said...

Have you tried bundling multiple utility functions into a single .jsbe
Warehouse Storage rack Supplier
mezzanine floor Supplier

manas said...

The ability to call global functions in the expression builder is fantastic. Any tips on testing such integrations?
mobile compactor Manufacturer
fifo flow rack

Naina said...

Are there any security considerations when using external JavaScript files in BIRT reports?
heavy duty rack supplier
Multi tier rack supplier

Web designing house said...

How would you recommend organizing reusable functions in a larger BIRT deployment?
Fabric Roll Racks Manufacturer
Warehouse mezzanine floor manufacturer

varshakush said...

If someone cannot set up a resource folder, are there alternative ways to include JavaScript files?
slotted Angle rack manufacturer delhii
pallet rack in delhi

Tripti said...

Does the report Context support localized JavaScript functions for multilingual reports?
Industrial Storage Rack Manufacturer
Pallet rack hyderabad

SHUBHAM said...

What is the best way to handle errors in global JavaScript functions when integrated into BIRT?
Heavy Duty Racking System delhi
warehouse storage rack Manufacturer

shivaniwebdesigning said...

Can similar techniques be applied to modify data before rendering it in the report?
Centralized Dust collector in dElhi
Rotary Air Locks in delhi

prachi said...

The use of report Context .evaluate opens up a lot of possibilities. Could you provide a few advanced examples?
Dust Collector Manufacturer India
Dust Collector Manufacturer

onlinepromotionhouse22@gmail.com said...

Can this JavaScript approach be extended to integrate BIRT with external data sources or APIs?
Drill Dust collector
Centrifugal Blower in delhi