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();
plot.setLabelBackgroundPaint(Color.WHITE);
plot.setLabelOutlineStroke(null);
plot.setLabelShadowPaint(Color.WHITE);

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 (http://jira.pentaho.com/browse/PRD-3247) 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:

X-AxisLabelWrap_current

Labels Without Wrapping

Desired Result:

X-AxisLabelWrap_output

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();
myAxis.setMaximumCategoryLabelLines(2);

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 http://tinyurl.com/X-AxisLabelWrap

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();
yAxis.setUpperBound(maxValue);
yAxis.setLowerBound(minValue);

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++)
{
ab=data.getValue(i,i);
dataVals.add(ab);
}
Double maxVale=(Collections.max(dataVals)).doubleValue();
Double minVale=(Collections.min(dataVals)).doubleValue();
yAxis.setUpperBound(maxVale+5);
yAxis.setLowerBound(minVale-5);

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.

Horizontal Gradient Bar chart in Pentaho Reporting Designer

As a hard core UI / UX designer, I always feel color plays a very important role in our life. Without colors anything will be colorless and same I feel when I see any BI dashboards and reports without any color.

I never understand why people want to create a report in Gray scale or in single color only. Reports should have some colors in that, where people can see the details and highlight those points where they need to focus more.

On one of my recent project, I have seen a very beautiful report which was created in PowerPoint and same I need to replicate in Pentaho Report Designer. The report was pretty cool and was having so many details and challenges.

Apart from other challenges like Multiple Language, Dynamic Formatting and complex formulas, I was having big time challenge with Gradient Horizontal Bar Charts. I have never seen gradient color in horizontal bar charts under Pentaho Report Designer, so was little surprised.

But finally understood the way to handle this challenge and now showcasing the same.

For creating a gradient horizontal bar chart in Pentaho report designer I used a beanshell code which works with JFree Charting library.

There is couple of other Post-Processing script also available in Pentaho Report Designer which processes after the chart renders. I created this with beanshell because JFree has good support with beanshell rather than other Post-Processing script.

You can add the beanshell script on your chart in report designer, by double clicking on your chart and selecting beanshell under Scripting > Chart Post-Processing Script in left panel.

Pentaho Report Designer

Pentaho Report Designer

Then write the following beanshell script for gradient horizontal bar chart

import java.util.*;
import java.awt.Paint;
import java.awt.Color;
import java.awt.Stroke;
import java.awt.BasicStroke;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.BarRenderer;
CategoryPlot myPlot = chart.getCategoryPlot();
BarRenderer categoryItemRenderer = myPlot.getRenderer();
categoryItemRenderer.setShadowVisible(false);
plot = chart.getPlot();
renderer = plot.getRenderer();
data = chart.getPlot().getDataset();
keys = data.getRowKeys();
Color[] c={
new Color(0x4784D7),
new Color(0x1F4F8F),
new Color(0xDBE7A5),
new Color(0xC8DB78),
new Color(0x7D838F),
new Color(0x272B39)
};
for (int i=0;i<keys.size();i++)
{
int j=(i*2);
g = new GradientPaint(0, 0,c[j],10, 0, c[j+1],true);
renderer.setSeriesPaint(i,g);
}

Here is the final result

Horizontal Bar Chart

Horizontal Bar Chart

Download the .PRPT file from here: http://tinyurl.com/gradientbar