Saturday, July 26, 2008

Struts2, JFreeChart - Transparent Charts

For my Struts 2 application I needed to create a chart using
JFreechart
that has a transparent background (rendered as png). Well, I thought
this would be trivial to do as Struts 2 ships with a
plugin
for it...

I created a simple action that creates a chart:

     public final String execute() throws Exception {

final DefaultCategoryDataset categorydataset = new DefaultCategoryDataset();

this.chart = ChartFactory.createLineChart("MyTitle",
"categoryLabel", "valueLabel", categorydataset,
PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(new Color(255,255,255,0));

return SUCCESS;
}



In my Struts XML file I put this:


<br /><action name="createChart"
class="com.hillert.sandbox.ChartAction"><br /> <result
name="success" type="chart"><br /> <param name="width">200</param><br /> <param
name="height">150</param><br /> </result><br /></action>



Well, the result is that my chart gets generated and rendered but
without transparency.


After asking the internet's oracle for guidance, Google returns a fews
results, e.g
this
one

that indicate that JFreeChart by default does not generate Alpha
Transparency for Pngs.


The solution however lies in customizing the jFreeChart Struts plugin.
In it's shipped version the plugin is calling:

ChartUtilities.writeChartAsPNG(os, chart, width, height);



For transparency you need to use the
extended
method call

. Simply change the method call above to:



ChartUtilities.writeChartAsPNG(os, chart, width, height, true, 0);



As a result aplha transparency is enabled and my chart is generated with
a transparent background.


Just for completeness, here is my action configuration using the
customized jFreeChart plugin:

<package name="chart"
extends="jfreechart-default" namespace="/chart"><br /><result-types><br /><result-type
name="customChart" class="org.jrecruiter.web.CustomChartResult"><br /></result-type><br /><br /><action
name="createChart" class="com.hillert.sandbox.ChartAction"><br /><result
name="success" type="customChart"><br /> <param name="width">200</param><br /> <param
name="height">150</param><br /></result><br /></action></result-types></package>

Tuesday, July 1, 2008

Book Review: Effective Java, Second Edition

Effective Java, Second Edition by Joshua Bloch is certainly the best Java book I have read in a long time. As a disclaimer, I never read the first edition and I am thus unable to compare the two editions. Effective Java, Second Edition is a mostly easy and fun read providing you with many insights and best practices on how to use Java effectively. It certainly is not a book for the beginner just starting out learning Java. For that purpose you may want to take a look at Thinking in Java by Bruce Eckel instead. Nevertheless, Effective Java would serve as an excellent follow-up.

In Effective Java, Joshua Bloch does a great job describing best practices that you as developer will find useful on a daily basis. For example, I really found his description of the builder pattern (Item 2, page 11) quite interesting. Another Item that fascinated me, was Item 15 (page 73) - "Minimize mutability". Both items are part of a broader theme throughout the book that promotes creating code that is as immutable as possible. In that regard, reading the book will enable you to simply write better and safer code. The book also leads the way towards promoting functional programming techniques which will come in quite handily when developing multithreaded applications. Therefore, as a next book I may recommend reading Java Concurrency in Practice by Brian Goetz.

Even for the experienced Java developer, Effective Java contains quite a few little eye openers. I for example was previously unaware of how static factory methods can simplify the creation of parameterized type instances using "type inference". This example is described on page 9 (Item 1). In the past I had always used something like this:

List users = new ArrayList();

But by using a static factory method you can do:

List users = Helper.newArrayList();

I thought that this was a pretty nifty example that may help making code a bit cleaner. What I also very much liked about Effective Java was that Joshua points out certain short-comings of the Java language itself and its APIs whenever applicable. For example, page 64 describes the inconsistent behavior between BigDecimal's 'equals' method and its 'compareTo' method, and in item 41 (page 194) Joshua details the shortcomings of the List interface when using Autoboxing.

While the vast majority of the book was very easy to read and to understand, I found that the chapter about bounded wildcards using generics (item 28) was a little difficult to grasp and I wished it were a bit more extensive. On the other side, the provided mnemonic is quite helpful: PECS - Producer-extends, Consumer-super.

Overall, I highly recommend Effective Java, Second Edition which will continue to serve me, and likely you too, as an excellent reference resource.