Upload
tobasayed
View
5.457
Download
12
Tags:
Embed Size (px)
DESCRIPTION
BIRT_Advanced_Scripting_EclipseCon_2008Report ScriptingChart Scripting
Citation preview
© 2008 by Virgil Dodson; made available under the EPL v1.0 | 2/11/2008
Advanced BIRT Report Customization: Report and Chart Scripting
Virgil Dodson – Actuate Corporation
Jason Weathersby – Actuate Corporation
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Agenda
• Report Scripting• Chart Scripting
© 2008 by Virgil Dodson; made available under the EPL v1.0 | 2/11/2008
Report Scripting
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
What is Scripting?
• Custom code to control various aspects of report creation
• Used as custom event handlers• Java or JavaScript
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Why use scripting?
• Maximum Flexibility• Data is rarely is the perfect format• Business rules are rarely an exact science• Use for Exception handling
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Choosing between Java and JavaScript
• Advantages of Scripting with JavaScript Easier for single event Simpler language construct Looser typing Less strict language rules Available to RCP applications
• Advantages of Scripting with Java Can use Java editor Easier to find and view scripts Access to the integrated debugger
Mix and Match! – You can use both Java and JavaScript Events… The JavaScript event overrides the Java event if the same event exist twice.
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Events Overview
• Events are triggered throughout report execution• Understanding the Event Order is important and
depends on: Engine Task Processes BIRT Processing Phases Event Types
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Engine Task Processes
• Report Engine can be used in different ways 3 tasks related to report execution and rendering
RunTask RenderTask RunAndRenderTask
Using RunTask and then RenderTask means multiple processes to generate and view a report.
RunAndRenderTask happens in single process so event firing order is different
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Engine Task Processes (cont.)
• Engine Tasks used with the Example Web Viewer ‘frameset’ mapping uses RunTask and RenderTask… export from Viewer
uses RenderTask ‘run’, ‘preview’ mapping uses RunAndRender Task
• Engine Tasks used with the BIRT Designer Web Viewer Preview uses RunTask and then RenderTask
Preview tab, plus rest of Preview icons use RunAndRenderTask
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
BIRT Processing Phases
• BIRT report processing happens in three phases Preparation Phase Generation Phase Presentation Phase
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Preparation Phase
• Report items are prepared for execution
Preparation Phase
Generation Phase
Presentation Phase
RunTask XRenderTask
RunAndRenderTask X
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Generation Phase
• creates the individual instances of report items• connects to data sources• executes data sets• processes data needed for the report
Preparation Phase
Generation Phase
Presentation Phase
RunTask X XRenderTask
RunAndRenderTask X X
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Presentation Phase
• Selects the proper emitter • Produces the desired output
Preparation Phase
Generation Phase
Presentation Phase
RunTask X XRenderTask XRunAndRenderTask X X X
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
BIRT Event Types
• Parameter events• ReportDesign events• Data Source/Set events• ReportItem events
• Each event type has one ore more events that will fire during report processing.
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Parameter Events
• validate() Used for extra validation or modifying parameter value First event triggered for reports with parameters
After user enters parameter value Before rest of report events run
Only available in JavaScript Expects true or false returned
true – process as normal false – throw parameter not set exception
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Report Design Events
• Fired for all reports• initialize()
once for each RunTask, RenderTask, or RunAndRenderTask
• beforeFactory() Once; after Preparation Phase and before Generation Phase
• afterFactory() Once; after Generation Phase
• beforeRender() Once for each render task, before Presentation Phase
• afterRender() Once for each render task, after Presentation Phase
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Data Source/Set Events
• All data sources and data sets have a common set of event handlers
• A scripted Data Source has two additional event handlers• A scripted Data Set has three additional event handlers• Data source/set events are fired prior to being used on a data
bound item.• If the data is not used on a report, these events will not fire• Not advisable to write event handlers that rely on the data set
event firing order.
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Data Source Events
• beforeOpen()• open() – only for Scripted Data Sources• afterOpen()• beforeClose()• close() - only for Scripted Data Sources• afterClose()
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Data Set Events
• beforeOpen()• open() – only for Scripted Data Sets• afterOpen()• fetch() – required for Scripted Data Sets• onFetch() – as each row of data is retrieved• beforeClose()• close() - only for Scripted Data Sources• afterClose()
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
ReportItem Events
• Triggered for most report items• onPrepare()
Fired at beginning of Preparation Phase before data binding. Can be used to change the design of an item prior to creating
instances of each item
• onCreate() Fired during Generation Phase as item is being created Can be used to change individual instance of item
• onRender() Fired during Presentation Phase Useful for operations regarding the output format
• onPageBreak() Fires for all report items on a page when the break occurs
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Event Order Sequence
Preparation Phase
Generation Phase
Presentation Phase
RunTask X XRenderTask XRunAndRenderTask X X X
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Preparation Phase
• Parameter validation• Initialization• Report element preparation
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Preparation PhaseRunAndRenderTask
ReportDesigninitialize()
ReportItemonPrepare()
ReportItemonPrepare()
ReportItemonPrepare()
ReportDesignbeforeFactory()
RunTask
ReportDesigninitialize()
ReportItemonPrepare()
ReportItemonPrepare()
ReportItemonPrepare()
ReportDesignbeforeFactory()
ReportDesignbeforeRender()
Parametervalidate()
Parametervalidate()
Parametervalidate() Parameter
validate()
Parametervalidate()
Parametervalidate()
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Generation Phase
• Connecting to Data Sources• Executing Data Sets and Data Cubes• Data Binding Evaluation• Creation of Report Items
• MasterPage content first…• …then Top-to-Bottom, Left-to-Right
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Generation PhaseRunTask
MasterPageonCreate()
MasterPageonPageBreak()
MasterPage DataEvents
Report Body DataEvents
Report ItemonCreate()
Report BodyonPageBreak()
Report Body DataEvents
Report ItemonCreate()
Report BodyonPageBreak()
Report Body DataEvents
Report ItemonCreate()
Report BodyonPageBreak()
RunAndRenderTask
MasterPageonCreate()
MasterPageonPageBreak()
MasterPage DataEvents
Report Body DataEvents
Report ItemonCreate()
Report BodyonPageBreak()
Report Body DataEvents
Report ItemonCreate()
Report BodyonPageBreak()
Report Body DataEvents
Report ItemonCreate()
Report BodyonPageBreak()
MasterPageonRender()
Report ItemonCreate()
Report ItemonCreate()
Report ItemonRender()
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Report Element Processing
• Processing is Iterative• Nested Elements are processed
before moving to the next element
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
onPageBreak Event
• Can be set on most elements• Triggered in Generation Phase for RunAndRenderTask• Triggered in Presentation Phase for RenderTask• Only fired for output that supports pagination• onPageBreak event fires just prior to the onCreate event
for the first master page element on the next page
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Table or List Event Processing
• Event order is altered for each row of data returned• Data rows are processed with row containers
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Row Event Order Processing
RunAndRenderTaskRow.onCreate
Row.onRender
Repeat for every cell in a row
Cell.onCreate
Cell.onRender
Repeat for every item in a cell
Item.onCreate
Item.onRender
RunTaskRow.onCreate
Repeat for every cell in a row
Cell.onCreate
Repeat for every item in a cell
Item.onCreate
RenderTaskRow.onRender
Repeat for every cell in a row
Cell.onRender
Repeat for every item in a cell
Item.onRender
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Table or List Order Processing
DataSet.beforeOpen
Table.onCreate
Cell.onRender
DataSet.onFetch
DataSet.afterOpen
DataSet.onFetch
Table.onRender
Process Header Row(s)
Process Groups and Detail Rows
Process Footer Row(s)
For every group level in the table
Process Group Header Row(s)
For every row in the group
Process Group Footer Row(s)
Process Detail RowProcess Detail Row
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Completion of Generation Phase
• Data Sources beforeClose() and afterClose() events are triggered
• afterFactory() event is triggered (RunTask Only)• afterRender() event is triggered (RunAndRenderTask
only)
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Presentation Phase
• onRender events triggered for all report items• Initialize() is triggered RenderTask only• When rendering individual pages, only onRender
events on that page will be triggered
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Report Scripting Examples
Demo
© 2008 by Virgil Dodson; made available under the EPL v1.0 | 2/11/2008
Chart Scripting
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
JavaScript Chart Event Handler
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Java Chart Event Handler
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Report EngineReport Engine Chart EngineChart Engine
Engine Flow vs Report Engine Phases
38
2. bind data2. bind data
3. build3. build
4. render4. render
Device RendererDevice
Renderer
1. prepare1. prepareGeneration Phase
Rendering Phase
onRender
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Generator.bindData
Design Series Runtime Series
Data
NumberDataSet
Runtime Series NumberDataSet
Chart Model In
Chart Model With Bound DataSet Out
beforeDatasetFilled for eachRuntime series
afterDatasetFilled for eachRuntime series
Optional Grouping
Events
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Generator.build
Runtime Series
Legend Rendering Hints
Chart Model Bound to DataSet
GenerateChartState Out
beforeGeneration
afterGeneration
Events
Series Renders
Internally Computed Chart
DisplayServer
Chart Model
Runtime Context
Main Block
Plot Block
Title Block
Legend
Block
Optional Axis With Min Max and Scale Set
Series Rendering Hints
Data Point Hints
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Generator.renderGenerateChartState In
Events See next 2 slides
Display Server*Render Main Block
Device Renderer
*Render Title Block
Render Plot Block
*Render Background
*Render Axis Structure
Render SeriesSeries Specific (ie Bar)
**Render Axis Labels
**Render Legend Block
iterate
For Each Series Renderer
•*Only the first Series Renderer
•**Only the last Series Renderer
Draw Primitives – DrawOval, DrawPolygon, drawText, FillArea, etc
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Render Events
Events
beforeRendering
Next Slide
beforeDrawBlock - Main
afterDrawBlock - Main
beforeDrawBlock - Title
afterDrawBlock - Title
beforeDrawBlock - Plot
beforeDrawMarkerRange
afterDrawMarkerRange
beforeDrawMarkerLine
afterDrawMarkerLine
beforeDrawSeries -base
afterDrawSeries - base
afterDrawBlock - Plot
beforeDrawBlock – Plot – for each series
beforeDrawSeries
beforeDrawDataPoint
afterDrawDataPoint
beforeDrawFittingCurve
afterDrawFittingCurve
afterDrawSeries
afterDrawBlock – Do not call after last series
beforeDrawDataPointLabel
afterDrawDataPointLabel
Series Render Event order vary depending on Renderer – Bar chart shown
iterate
RenderSeries
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Render Events
Events
Previous Slide
afterRendering
For Each Axis
beforeDrawAxisLabel
afterDrawAxisLabel
beforeDrawAxisTitle
afterDrawAxisTitle
afterDrawBlock - Plot
iterate
Chart With Axis
beforeDrawBlock - Legend
beforeDrawLegendItem
afterDrawLegendItem
afterDrawBlock - Legend
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Getting the Report Context from the Chart Context
• In JavaScript rpcntx = context.getExternalContext().getScriptable(); rpcntx.getParameterValue("chartTitle");
• In Java IReportContext rc = (IReportContext)icsc.getExternalContext().getObject(); String mytitle = (String)rc.getParameterValue("chartTitle");
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Iterative script calls
• Many of the script calls are iterative• For example the before and
afterDataSetFilled and before and afterDrawSeries.
• Use series.getSeriesIdentifier() to key on a particular series.
• function beforeDrawSeries( series, seriesRenderer, context )
{
if( series.getSeriesIdentifier() == "series one" ){
context.getChartInstance().
setUnitSpacing(70);
}
}
//This will not work with grouped series definitions
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Iterative script calls .. Blocks Similarpublic void beforeDrawBlock( Block block, IChartScriptContext icsc )
{
if ( block.isCustom( ) )
{
//Main Block
}
if ( block.isLegend( ) )
{
block.getOutline( ).setVisible( true );
block.getOutline( ).getColor( ).set( 255, 0, 0);
}
else if ( block.isPlot( ) )
{
block.getOutline( ).setVisible( true );
block.getOutline( ).getColor( ).set( 0, 255, 0);
}
else if ( block.isTitle( ) )
{
block.getOutline( ).setVisible( true );
block.setBackground( ColorDefinitionImpl.CREAM( ) );
block.getOutline( ).getColor( ).set( 0, 0, 255);
}
}
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Iterative -- Determining Which Axis the event occurs forfunction beforeDrawAxisLabel(axis, label, scriptContext) {
importPackage(Packages.org.eclipse.birt.chart.model.attribute);
if (axis.getType() == AxisType.TEXT_LITERAL){
label.getCaption( ).getColor( ).set( 140, 198, 62 );}
else {label.getCaption().getColor( ).set( 208, 32, 0);}
}function beforeDrawAxisTitle(axis, title, scriptContext){
importPackage(Packages.org.eclipse.birt.chart.model.attribute);if (axis.getType() == AxisType.LINEAR_LITERAL){
title.getCaption( ).setValue( "Y-Axis Title By JavaScript");}title.getCaption( ).getColor( ).set( 32, 168, 255 );
}//Can also use://axis.getTitle().getCaption().getValue()
Supported Types
LINEAR_LITERAL
LOGARITHMIC_LITERAL
TEXT_LITERAL
DATE_TIME_LITERAL
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Getting Axisfunction beforeGeneration(chart, icsc){
importPackage( Packages.org.eclipse.birt.chart.model.attribute.impl );importPackage( Packages.org.eclipse.birt.chart.model.data.impl );
xAxis = chart.getBaseAxes()[0];xAxis.setFormatSpecifier( JavaDateFormatSpecifierImpl.create("MM//dd/yyyy"));
yAxis = chart.getOrthogonalAxes( xAxis, true)[0]yscale = yAxis.getScale(); yscale.setStep (10); yscale.setMin( NumberDataElementImpl.create(1.5) ) yscale.setMax( NumberDataElementImpl.create(100) ) yAxis.setScale(yscale);
}
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Setup Java Project for Java Event Handlers
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
ChartEventHandlerAdapter
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Simple Charting API• New API that can be used in the Report Script. See Java Docs. • Example beforeFactory script• //Simple Chart Api• rptDesignHandle = reportContext.getReportRunnable().designHandle.getDesignHandle();• cht = rptDesignHandle.findElement("Chart1");• cht.setProperty( "style", "NewStyle1" );• var chart1 = this.getReportElement( "Chart1" );• var color1 = chart1.getTitle().getCaption().getColor();
• chart1.setColorByCategory( true );• chart1.getTitle().getCaption().setValue( "My New Title" );
• color1.setRed( 255 );• color1.setGreen( 0 );• color1.setBlue( 0 );• chart1.getTitle().getCaption().setColor( color1 );• chart1.setDimension( "ThreeDimensional" );• chart1.getCategory().setSorting( "Descending" )• chart1.setWidth("6in");• chart1.setHeight("6in");
• //Standard API More Complex than Simple Chart API• importPackage( Packages.org.eclipse.birt.chart.model );• chart = rptDesignHandle.findElement("Chart1" ); • item = chart.getReportItem();• cm = item.getProperty("chart.instance");• cm.getTitle().getLabel().getCaption().setValue("My Test");
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Chart Scripting Examples
• JavaScript Event Handlers• Java Event Handler• Simple Chart API
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Chart Client Side Script - Interactivity
Demo
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.054
Chart Interactivity – Features Report/Web page
Click
Click/ Hover
Any Event Any Action (Script, Callback,…)
Ctrl or Alt Click
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Chart Interactivity Examples
• Drillthrough• Mouseover• SVG
Advanced BIRT Report Customization | Report Scripting | © 2008 by Virgil Dodson; made available under the EPL v1.0
Q&A
Virgil Dodson
Jason Weathersby