Right Outer Join

18 August 2010

Chart Customizers in JasperReports, iReport, and JasperServer

Filed under: iReport, JasperReports, JasperServer — Tags: , , , , — mdahlman @ 16:01

Chart Customizers in JasperReports, iReport, and JasperServer

Update: Much of the content of this article should still be useful. But I wrote a more recent article with a much more useful chart customizer attached. So please take a look at that article as well.

When charts in reports designed in iReport don’t look the way that you want them to there are a few common next steps that you can take. Here are the most common:

  • Silently curse those Jaspersoft developers (what a stupid default display style)
  • Silently curse those JFreeChart developers (surely everyone in the world would prefer it differently)
  • Loudly curse them (come on guys! hurry up and add my new feature!)
  • Search jasperforge.org, figure out that you need a chart customizer or a chart theme, speculate that this sounds too difficult, and then finally decide that you don’t really care so much about whatever it was that you really wanted to change afterall.

In this article I’ll take a look at some of the less-used alternative next steps:

  • Refer to the documentation
  • Refer to the samples
  • Create and use a Chart Customizer

Documentation

The JasperReports Ultimate Guide includes the section “Chart Customizer and Chart Theme”. This is a good starting point. It explains the reason these objects exist: to provide full control over chart customization. And it describes the interface that you must implement and the class you should extend in order to create a chart theme. The iReport Ultimate Guide mentions the existence of chart themes, but it doesn’t provide any additional examples or insight.

The online documentation on jasperforge.org provides additional information. The Sample Reference section includes a page on charts which has a section on chart customizers. It has sample chart customizer code that can hard-code a bar chart to ignore the colors that you have chosen when designing the report and to use only green and orange instead. The customizer has no practical value, but it’s nice to have a working sample as a starting point.

Samples

The page mentioned above refers the reader to a resource that should already have been consulted… the samples that ship with JasperReports. If you have only installed JasperServer or iReport, then you’ll need to download JasperReports directly. It includes this folder containing over 50 samples:

…/jasperreports-3.7.4/demo/samples

In the subfolder “charts” you’ll find the aforementioned BarChartCustomizer.java to generate green and orange bars along with reports that use this chart customizer. In the folder “chartthemes” you’ll find sample code for chart themes.

The other sample that everyone has easy access to is the set of chart customizers that ships with JasperServer Professional. Use iReport to browse the JasperServer repository and download the file DemoChartLabels.jar. It contains both the binaries used by the server as well as the source java files.

DemoChartLabels.jar in JasperServer Plugin in iReport

The highlighted file, DemoChartLabels.jar, may be downloaded using the JasperServer Plugin in iReport

Chart Customizer Samples

I’m not a Java developer. I will not be able to create an impressive chart customizer to amaze your friends and family. But I know enough Java to create some very basic chart customizers. I have set my sights slightly higher than just creating alternating green and orange bars in a bar chart. But not much higher.

Pie Chart Labels

The problem:

I want a pie chart with no labels on it. It sounds funny right? But it’s pretty common. It came up in this thread on jasperforge.org. I have wanted it in the past. Most recently, Jaspersoft partner OpenBI asked me about it. Here’s a simple example.

Pie Chart with the Default Look

Pie Chart with the Default Look

The solution:

I need a way to tell the chart not to display labels. iReport (and JasperReports) does not provide a way to do this. The underlying chart engine is JFreeChart. The JFreeChart API provides a mechanism avoid displaying labels. I found the answer on stackoverflow.com. The good news is that it’s a single line of code. Where do I put this code? In a chart customizer! It’s simple enough once you have the idea. Here is the complete code for the chart customizer:

/**
  * OK, it's not really the COMPLETE code. I removed the imports and comments.
  * But those are included in the attachments.
  */
public class PieLabels implements JRChartCustomizer {
  public void customize(JFreeChart chart, JRChart jasperChart) {
    Plot plot = chart.getPlot();
    if (plot instanceof PiePlot) {
      PiePlot piePlot = (PiePlot)plot;
      piePlot.setLabelGenerator(null);
    }
  }
}

Next, I compiled this class into the very small file DemoChartCustomizers.jar. Finally, I need to tell the report to use the chart customizer. Select the chart and set the property “Customizer Class”.

Set the Customizer Class property

Set the Customizer Class property

Remember to add the JAR file to iReport’s classpath.

Set the iReport Classpath

Set the iReport Classpath

The final result is shown below. It’s modest, but it’s certainly nicer than the original.

Pie Chart with the Customized Look

Pie Chart with the Customized Look

Bar Chart Labels

The problem:

This one has annoyed me for years. But I never quite got around to attempting to fix it. I went with option number two from my list above: I’ve been silently cursing those JFreeChart developers. JFreeChart’s default way of arranging labels on bar charts puts them above the bars. But its default way of rendering the chart doesn’t leave enough room above the tallest bar to display the label. This old thread discusses the problem. The project leader of JFreeChart, David Gilbert, writes, “Eventually, it would be nice if the renderer could calculate an appropriate range so that the item labels are always visible.” Here’s a typical example of the issue. (This one ships with JasperServer Professional.)

Bar Chart Original Labels

Bar Chart Original Labels

The average sale price for All Customers is 27.54. But what is the average sale price for M. Benigar? It’s significantly higher than the average… but where is its label? This annoys me to no end. Perhaps the problem has already been solved in JFreeChart’s default settings. The thread identifying the problem is more than three years old as I write this article. But as far as I can tell, there have been no changes in this regard.

The solution:

Until JFreeChart cleans this up internally, we have to manually calculate a better margin to allow space for the label to be displayed. I use a somewhat crude calculation: hard-code the margin to 40%. Here is the result.

Bar Chart with Customized Labels

Bar Chart with Customized Labels

Woo hoo! Now we can see the exact value of the tallest bar.

Summary

iReport gives good control over chart options for dozens of properties. But if you want complete control over the display options, you need to use JasperReport’s chart customizers. This requires Java coding. Sorry. But it’s not much coding. My two chart customizers really have only one functional line each.

Next Steps

You can download my ChartCustomizerSamples to try it yourself. The download file is ChartCustomizerSamples.odt. You need to rename this to ChartCustomizerSamples.zip. (WordPress won’t let me post a .zip file.) The JAR file included inside will let you run the pie chart reports in iReport. It also includes the source code. The reports use the FoodMart sample database that ships with JasperServer.

Additional Note: I have conveniently ignored Chart Themes. In fact, chart themes are newer and better than chart customizers. Chart customizers have been deprecated in favor of chart themes. Hopefully I’ll have time to investigate and write about chart themes someday in the future.

Advertisements

26 Comments »

  1. Can you please check the ‘sample reports and code’ attachment.
    Seems to not be correctly available for download.
    Under Chrome, it saves, but even after changing extension to .zip, it does not work.
    IE says it is not available.

    thx.

    Comment by Scott — 12 November 2010 @ 06:20

    • Thanks for posting! I confirm that the file went away. I’m not sure what happened, since I had not made any changes. I reposted the file, and the new link is working for me. Please let me know if it’s working for you as well.

      Comment by mdahlman — 12 November 2010 @ 14:49

      • I need “ChartCustomizerSamples”‘s link

        Comment by Bruno Mignoni — 31 January 2011 @ 15:30

      • Wow… I have no idea how or why WordPress would lose the attachment [again]. I just uploaded the file again. Please try the link in the article now and let me know if it’s working for you.
        Regards,
        Matt

        Comment by mdahlman — 31 January 2011 @ 16:06

  2. It’s gone again 😦

    Comment by majki — 17 February 2011 @ 04:01

    • It’s working for me. I tested a few minutes ago. Please try again and let me know if it’s still not working for you.

      Comment by mdahlman — 28 February 2011 @ 14:55

  3. AWESOME!!! Thank you so much. Works like a champ!

    Comment by cash shurley — 18 February 2011 @ 14:12

  4. […] a previous article I described some reasons that chart customizers are useful and gave some links to useful […]

    Pingback by Useful Chart Customizers in JasperReports, iReport, and JasperReports Server « Right Outer Join — 17 April 2011 @ 22:46

  5. Matt, Looks like the link for this jar was removed again, could you upload again pleas, I really need solve this problem. Thanks

    Comment by Miguel Angarita — 17 May 2011 @ 08:35

    • It’s a bummer that WordPress loses my attachments so regularly. But have you looked at the follow-up article here? That has a better version of the same chart customizer. And it’s hosted on GoogleDocs, so it shouldn’t keep getting lost. Please let me know how it works for you.

      Comment by mdahlman — 17 May 2011 @ 10:07

  6. On what object should I set the margin for the bar chart label don’t cut off?

    Comment by Joe — 22 July 2011 @ 10:06

    • You cannot set the margin yourself with the sample in this article. The chart customizer hard-codes the values. But the follow-up article linked in both the first line of this article and in the comments above has a version where you can set the properties on the chart when you design the report.

      Comment by mdahlman — 22 July 2011 @ 12:25

  7. […] Deja un comentario Insertar gráficos en Jasperreports es muy sencillo usando iReport, sin embargo, para hacer algunos cambios de estilo en los gráficos se necesitan algunas lineas de código y algunas librerías extra. Los mejores tutoriales al respecto los encuentran aqui. […]

    Pingback by jasper chart themes « Vida Np — 27 September 2011 @ 08:40

  8. Matt, this is a great example on chart customization. One question, the Customizer Class property is set to “com.jaspersoft.demo.PieLabels”, what/where does the “com.jaspersoft.demo.” come from?

    Comment by Calvin Krzebietke — 30 November 2011 @ 09:31

    • Calvin, that’s just the arbitrary package name that I chose for the class (and for my other chart customizers). If you wanted to change it, you would modify the package name in the Java source.

      Comment by mdahlman — 30 November 2011 @ 13:30

  9. Matt, As a beginner in Jasper reports, I have learnt that Chart Customizers are of great help. I use iReport 3.7.2 and from the user manual (3.7.0) was able to use a Chart theme. There is no mention of Chart Customizers in it. I tried following the online documentation you referred to, but was not able to understand it. Could you please point me toward right direction. Thanks

    Comment by bigM — 25 January 2012 @ 16:59

    • This is a very good question. In principle Chart Themes are preferred over Chart Customizers. The documentation reflects this. But I haven’t gotten around to working with Chart Customizers much yet. There are a couple of samples that ship with JasperReports and with JasperReports Server. Someday I would love to explore them a bit more and write an article, but I’m not sure when that will be.

      Comment by mdahlman — 1 February 2012 @ 17:38

  10. Thanks for the NoPieLabels.
    Awesome. :_)

    Comment by Owen Roberts — 22 March 2012 @ 21:10

  11. The download link is not working. Can you fix it?

    Comment by aysegulyazici — 31 August 2012 @ 01:45

    • The download link is working for me. I have had lots of trouble with wordpress.com, so it’s not completely surprising that it failed for you. But it’s a bummer, sorry about that. If you try again it may work.

      But a better solution is to get the download from this follow-up article:
      chart-customizers-2

      It has all of the same things; plus it has more things; plus it’s hosted as a Google doc so you can actually get it.

      Comment by mdahlman — 31 August 2012 @ 09:55

      • I download it now.Thanks for your work. 🙂

        Comment by aysegulyazici — 31 August 2012 @ 23:31

      • Hi matt,
        i am trying to navigate from a html 5 column chart to the adhoc designer using hyperlink properties in the measures.
        i was able to go if i use the absolute path like http://localhost:8084/jasperserver-pro/flow.html?_flowId=adhocFlow&resource=/adhoc/Ad_Hoc_View2&j_username=jasperadmin|hcl&j_password=jasperadmin

        then i was able to go when i clicked on a specific bar from the column chart to the adhoc designer.

        how can i do that using the relative path like this /adhoc/Ad_Hoc_View1. this is the path of the view under my organization.

        one more thing i am really finding clueless is how can i embed a javascript in an html 5 charts so that when clicked on any bar i should be able to show a report or view in an modal window or popup window.

        Comment by chandu — 3 October 2013 @ 05:34

      • It sounds like a reasonable question. Sorry, I have no idea about it. You’ll need to post to the Jaspersoft community site to get help on this one.

        Comment by mdahlman — 3 October 2013 @ 09:09

  12. I need “ChartCustomizerSamples”‘s link

    Comment by diegoqueres — 28 April 2016 @ 10:58


RSS feed for comments on this post. TrackBack URI

Go on... leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: