Pentaho Report Designer and Bean Shell Scripting – Chart Post-Processing Script

This is my last post about Pentaho Report Designer and Bean Shell Scripting. So far we covered an introduction to bean shell in pentaho and how to obtain useful debug information from Pentaho Report Designer, PRD from now on.

Chart Post-Processing Script

For our third post we can use the report attached in our first post. We are going to modify our dataset, of course you are free to work with the same table, but I will modify it so the data and the chart will be more consistent. As you can see below the only change I made is the type of the second column, from type String to Date. So now we will identify how many friends a specific character had during a period. 

Image new Table

Pentaho provides a nice way to include charts in our reports. In the palette just click and drag a Chart into the Report Footer band, double-click the sample chart and configure it. For this example I will add a Line Chart for the value-columns Friends and AvgFriendsBSH.

Image new Chart

Parameters that I used for this example:

    Common: Defined the category and the values

  • Category-column: Date
  • Value-columns: Friends, AvgFriendsBSH
    Series: Specified the data name for the legend

  • Series-by-value: Friends, Average
    X-Axis: Turned 90º the X-Axis lables

  • x-axis-label-rotation: 90
    Values: Show the labels in our data

  • Show-item-labels: Show Labels

We should see something like:

Image Chart Problems

As you can see the there are some problems some labels overlays between series: some labels are partialy shown, and it does not make much sense to have labels for each value of the average since they are always the same.

A first step to solve this problem is to resize the item-label-font in Value section to 6px so all the labels will fit in the canvas, but we still have all the labels in our average line. A way to solve this without delete the labels in our series Friends is by applying a Chart Post-Processing Script. Chart Post-Processing Script property gives you access to JFreeChart APIs so you can modify the chart after the report renders.

We want to modify the visibility of the labels item. This can be accomplished by Chart Post-Processing Script Language to beanshell and adding the following line to Chart Post-Processing Script chart.getCategoryPlot().getRenderer().setSeriesItemLabelsVisible(1, Boolean.FALSE);

Image Chart Script

The mentioned code returns the renderer for the primary dataset and changes the default value of setSeriesItemLabelsVisible which allows us to set the visibility of item labels for the series, in our case to FALSE. In our example setSeriesItemLabelsVisible(1, Boolean.FALSE)  corresponds to the average series.

The final result:

Till here the basics about Pentaho Report Designer and Bean Shell Scripting, I hope you have enjoyed the posts. Please I will be really glad to read your suggestions, comments about any errors or ways to improve this post.

Pentaho – BeanShell and Charts.prpt

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *