Custom Label Background in Pie Chart in Pentaho Report Designer

Hello folks!! It’s been quite a while since I wrote my last blog, and it’s always enthralling to be back to writing. Pentaho Report Designer, a tool which I love to explore, more specifically, on the charting front, as it is the least documented and most useful part of any reporting requirement.

This blog is about doing an interesting task in the PRD with pie chart. Recently, after looking at my blogs, one of my friend asked me about the possibility of a requirement in PRD, which was changing the background of labels in Pie Chart in PRD. At once, I thought it should be a configurable part, but I was not amazed on finding it missing from the configurable options. But nonetheless, I thought of doing it with Beanshell scripting (which I love the most in PRD), and got it working in just a little time. That friend of mine after getting the solution, asked me to write a blog about it as he wanted to help people with this knowledge and I’m more than happy to do so.

So here’s what you have to do in order to get the custom background of Pie chart labels in PRD. In chart properties, go to the Post Processing script section and select Beanshell from the dropdown, and write the following code in the pop up to get it working :

import java.awt.Color;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PiePlot;

PiePlot plot = (PiePlot) chart.getPlot();

One can easily customize this code to suit their need, as in changing the color to some other instead of white and much more. Below is the screenshot of what output will look like without this custom code.

Without Custom Code

Without Custom Code

With this custom code

With Custom Code

With Custom Code

Hope this helps someone and saves somebody’s valuable time. Please feel free to leave comments and requests for more such functionalities with Beanshell scripting in PRD.

Here is the sample prpt with Pentaho’s sample data.

X-Axis Label Wrap in Pentaho Report Designer

I am back with yet again an interesting possibility in Pentaho Report Designer using Beanshell Script. As I said in my earlier post that Beanshell Script is a blessing in PRD, this post will strengthen the belief of many in this fact. So let’s have a look at the new possibility.

I was working with PRD in previous month, when I was needed to do an interesting stuff in PRD which is to wrap X-axis label for bar chart in Pentaho Report Designer. PRD does not have any configuration for this. I searched about this possibility through many of the forums and blogs but all in vain. Meanwhile, I came across a link ( of official pentaho jira request regarding the same which was opened nearly 34 months back and is still open.

I was amazed looking at this so old Jira request open. I thought that it might be possible using the Beanshell scrtip. I hardly spent my 30 minutes to get this job done using Beanshell script and walah!! I got the desired output.For those who don’t know about Beanshell script, here is the link to my previous blog Custom Value Axis Scale in Pentaho Report Desinger, where you will get a short brief about Beanshell Script and where to write Beanshell Script in PRD.

Currently PRD does not wrap a big label on X-Axis of a chart. Whenever a label is bigger it just dotify it and displays it like “labelnotw….”. But sometimes in practical implementations, the label may be as big as 30 characters and the client does not want it to be dotify. Below are the two sample images, of what is the current problem and what is to be the expected output, for a better clarification on the usability of this blog.

Current Result:


Labels Without Wrapping

Desired Result:


Labels After Wrapping

PRD don’t have any by default feature to achieve this but having said that PRD has a feature using which you can achieve this and that is Chart-Post Processing Scripting. Beanshell is also one of the chart post processing script available in PRD.

To get the above output you need to write the following few lines of code in PRD’s chart post processing script having beanshell as the type.

import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.axis.CategoryAxis;

CategoryPlot myPlot = chart.getCategoryPlot();
CategoryAxis myAxis = myPlot.getDomainAxis();

And here you go. It seems to be a very simple thing when you see the output but believe me when you search through the internet you will not get any of the satisfactory solution to achieve this. But it is a very useful feature and is often demanded by the customers.

Here is the sample report to download

Hope this post helps someone and feel free to give comments and suggestion. I’ll be writing more and more to help and share the bit of my knowledge with people on global platform.

Custom Value Axis Scale in Pentaho Report Designer

The Pentaho Report Designer (PRD) has evolved as a very feature-rich product over the last few years. But still there are some features, that are not documented anywhere. You come to know these features as you dig more and more into the tool.

In this blog, I want to share one of those many unsung possibilities in PRD. While working with charts in PRD, sometimes it is required to have a custom range on the value axis(Y Axis). This seems to be a very common need to customize the scale as required but, PRD does not have any default option for this functionality, but still there is a out of the box work around which can make this happen and i.e. Beanshell Script.

For those who are new to Beanshell Script, Beanshell Script is like a blessing in PRD, especially when you are working with charts. It is one of the Chart-Post Processing Script that you can write in PRD. To write a Beanshell Script in PRD just select the type of Chart-Post Processing script as “beanshell” from the available values and write the script in the next field. Refer to the image for more clarity on where to write the beanshell script.

Pentaho Report Designer

Pentaho Report Designer

You can write the script in the new window which pops up. There are two ways you can customize the value axis range using beanshell script:

1)      Setting the fixed upper and lower bound for the range

2)      Taking the upper bound and lower bound from the minimum and maximum values of the chart

Fixed Upper and Lower Bound:
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;

CategoryPlot chartPlot = chart.getCategoryPlot();
ValueAxis yAxis= chartPlot.getRangeAxis();

Where “maxValue” and “minValue” can be any values that you want to set as upper and lower bound respectively.

Setting Bounds Based on the Values of Chart:
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.axis.ValueAxis;
import java.util.*;
import java.math.*;
import java.util.List;
import java.util.ArrayList;

CategoryPlot chartPlot = chart.getCategoryPlot();
ValueAxis yAxis= chartPlot.getRangeAxis();
data = chartPlot.getDataset();
keys = data.getRowKeys();
ArrayList dataVals = new ArrayList();
for (int i=0;i<keys.size();i++)
Double maxVale=(Collections.max(dataVals)).doubleValue();
Double minVale=(Collections.min(dataVals)).doubleValue();

And here you go. This is how you can achieve this.Here is the sample output.

Custom Value Axis Scale

Custom Value Axis Scale

And here is the link to download the sample prpt for this.

Trust me,  it is one of the many possibilities using Beanshell Script.Hope this post helps people and soon will be coming with many more possibilities in PRD.