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:

  1. Anonymous9:47 AM

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

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

    ReplyDelete
  3. 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

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

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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

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

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

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

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

    ReplyDelete
  13. 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

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

    ReplyDelete
  15. 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

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

    ReplyDelete
  17. 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

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

    ReplyDelete