How would you feel if you knew you could each of the million features of JFreeChart to work within the Pentaho Reporting System? After all, it is a ugly thing for a consultant or system developer to say “no, sorry, can’t do that” to a customer or client. Especially when you have that shiny new JFreeChart Developer Guide on your desk telling you that there is a way.
Imagine a world without sentences like “Uh .. sure JFreeChart supports this feature, but it is not exposed in the Report Designer”.
Well, with Pentaho Reporting 3.7 you can live in that world right now.
All chart expressions now have the “Chart Post-Processing Script Language” and “Chart Post-Processing Script” properties. The “Script Language” property defines the scripting language in which the actual script is written (so that the more masochistic ones among our users can continue to use JavaScript all day long) and the “Script” property itself then contains the script.
Like the well-known BeanShell- and BSH-Expressions, a set of predefined variables grants access to the outside world.
“chart” is the fully initialized JFreeChart object.
“dataRow” grants access to the values of the other named expressions and the data-table
“runtime” grants access to the current ExpressionRuntime instance. The runtime itself allows the use of the ResourceBundles (for internationalization) and grants access to the report’s defined DataFactories (to get more data from the outside world).
A simple post processing script could look like this (Language = BeanShell):
import java.util.Date; chart.setTitle("My Title: " + new Date());
But with the ability to fire queries, you can easily add your own line or item marker to explain significant details or enrich the plots with your own styling.
Your imagination (and coding abilities) are the limit now!
good news Thomas! Looking forward to your little demo showing how to build Bullet Graphs in PRD this way. You are going to show this at the community meeting, aren’t you? 😉
Thomas, what do you mean with “ability to fire queries”? Do you mean that we can use the functions provided by JfreeChart to retrieve additional db data. Or can we access the prd datasources and have it fire an additional query to the db?
THere are two ways: In PRD-3.7 I added a new formula function to access the report datasources (called SINGLEVALUEQUERY).
And beginning with 3.7 the ExpressionContext now grants access to the datafactory so that you can fire queries on the report’s datasources.
Could this be used to set the label font on a point?
e.g. chart.setLabelFont(…)
It doesn’t seem like it works for this property like this or from the Report Design. We’re using a bar chart and trying to size the values that hover above each bar.
Thanks for the great post!
The label font on a data point is a tick-label, and is usually set deep inside the chart-renderer/plot. Have a look at the JFreeChart API and/or the jfreechart-documentation for more details on that.
Perfect. The beanshell script we ended up using:
import java.awt.Font;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
Font ft = new Font(Font.SANS_SERIF,Font.PLAIN, 6);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryItemRenderer renderer = (CategoryItemRenderer)plot.getRenderer();
renderer.setBaseItemLabelFont(ft);
Thanks!
This is a great feature – though it’s easy to get carried away! Are there any examples around of using the ExpressionRuntime to get hold of data from datasources other than the primary datasource?