Writing a Java chart event handler
Write a Java chart event handler in the same way as a Java event handler for any other kind of report item. The only difference is that the chart event handler must contain all the implemented event handlers in a single Java class; note that the examples provided are all implemented as individual classes. The Java chart event handler also requires additional Java archive (.jar) files in the build path.
Setting up the chart event handler project
To create a new Java chart event handler project requires the following JAR files in the build path and classpath:
*chartengineapi.jar
*org.eclipse.emf.ecore_<version>.jar
*org.eclipse.emf.common_<version>.jar
*org.eclipse.birt.report.engine_<version>.jar to access the reportContext
*Optionally, com.ibm.icu_<version>.jar, to use the ULocale methods
To write a Java event handler for a chart, implement the IChartEventHandler interface or extend the ChartEventHandlerAdapter. The examples in this chapter extend the adapter.
To apply a Java chart event handler to the chart, select the chart in the report design. In Properties, select the event handler property and type the fully qualified class name or choose Browse to locate the class. The browser displays only classes in your workspace or classpath that implement IChartEventHandler or extend ChartEventHandlerAdapter.
Debug chart event handlers written in Java in the same way as other Java event handlers.
Java chart event handler examples
This section provides some examples of common uses of Java chart event handlers.
The code in Listing 39‑20 uses the beforeDrawBlock event handler to outline the legend block in red and the plot block in green, and set the background color of the title block to cream and its outline to blue.
Listing 39‑20 Setting outline and background colors in a chart
package my.chart.events;
 
import org.eclipse.birt.chart.script.ChartEventHandlerAdapter;
import org.eclipse.birt.chart.model.layout.Block;
import org.eclipse.birt.chart.script.IChartScriptContext;
import org.eclipse.birt.chart.model.attribute.impl
.ColorDefinitionImpl;
 
public class BlockScript extends ChartEventHandlerAdapter
{
public void beforeDrawBlock( Block block,
IChartScriptContext icsc )
{
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 );
}
};
Listing 39‑21 uses the beforeDrawSeries event handler to set the series labels to red and apply a curve fitting line to the series. If the series is a line series, the code changes the marker type to a triangle.
Listing 39‑21 Setting a series label color and applying a curve fitting line
package my.chart.events;
 
import org.eclipse.birt.chart.script.ChartEventHandlerAdapter;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.script.IChartScriptContext;
import org.eclipse.birt.chart.render.ISeriesRenderer;
import org.eclipse.birt.chart.model.type.LineSeries;
import org.eclipse.birt.chart.model.attribute.Marker;
import org.eclipse.birt.chart.model.attribute.MarkerType;
import org.eclipse.birt.chart.model.component.impl
.CurveFittingImpl;
 
public class SeriesScript extends ChartEventHandlerAdapter
{
public void beforeDrawSeries( Series series,
ISeriesRenderer isr, IChartScriptContext icsc )
{
if( series instanceof LineSeries ) {
Marker mk = ( ( Marker )( ( LineSeries )series )
.getMarkers( ).get( 0 ) );
mk.setType( MarkerType.TRIANGLE_LITERAL );
}
series.setCurveFitting( CurveFittingImpl.create( ) );
series.getLabel().getCaption().getColor().set( 255, 0, 0 );
}
}
In Listing 39‑22, the code creates event handlers for the beforeGeneration event and for the beforeDrawSeries event. The beforeGeneration event handler sets the plot background color to gray. If the chart has axes, the event handler sets the x‑axis labels to an angle of 45 degrees and places the labels below the axis. This event handler also makes the major grid lines for the y‑axis visible. The beforeDrawSeries event handler adds a mouse-over event to the series to show the value of the current data point.
Listing 39‑22 beforeGeneration and beforeDrawSeries event handlers
package my.chart.events;
 
import org.eclipse.birt.chart.script.ChartEventHandlerAdapter;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.script.IChartScriptContext;
import org.eclipse.birt.chart.model.attribute.impl
.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.attribute.TooltipValue;
import org.eclipse.birt.chart.model.attribute.impl
.TooltipValueImpl;
import org.eclipse.birt.chart.model.data.Action;
import org.eclipse.birt.chart.model.data.impl.ActionImpl;
import org.eclipse.birt.chart.model.attribute.ActionType;
import org.eclipse.birt.chart.model.data.impl.TriggerImpl;
import org.eclipse.birt.chart.model.attribute.TriggerCondition;
 
public class ChartModScript extends ChartEventHandlerAdapter
{
public void beforeGeneration( Chart cm,
IChartScriptContext icsc)
{
cm.getPlot( ).getClientArea( ).setBackground(
ColorDefinitionImpl.GREY( ) );
if ( cm instanceof ChartWithAxesImpl ) {
// x axis
Axis xaxis = ( ( ChartWithAxesImpl ) cm)
.getPrimaryBaseAxes( )[ 0 ];
xaxis.setLabelPosition( Position.BELOW_LITERAL );
xaxis.getLabel( ).getCaption( ).getFont( )
.setRotation( 45 );
Axis yaxis = ( ( ChartWithAxesImpl ) cm )
.getPrimaryOrthogonalAxis( xaxis );
yaxis.getMajorGrid( ).getLineAttributes( )
.setVisible( true );
}
}
public void beforeDrawSeries( Series series,
ISeriesRenderer isr, IChartScriptContext icsc )
{
TooltipValue tt = TooltipValueImpl.create( 500, null );
Action ac = ActionImpl.create(
ActionType.SHOW_TOOLTIP_LITERAL, tt );
series.getTriggers( ).add( TriggerImpl.create(
TriggerCondition.ONMOUSEOVER_LITERAL, ac ) );
}
}