Charting with JFreeChart

I’ve been coding dashboard applications lately with Java SE. I have used JFreeChart library to generate all pie, line and bar charts. The library is awesome and is also Open Source. It would be very nice to see similar projects in the .NET world.

It was very easy to create first charts with JFreeChart mostly because it is so commonly used so there are many resources available in the Internet including tutorials and tips&tricks.

Here is a small sample how to create a simple pie chart and use it in your Swing application:

  1. // First create dataset
  2. DefaultPieDataset dataset = new DefaultPieDataset();
  3. dataset.setValue("Finland", 5300367);
  4. dataset.setValue("Sweden", 9107649);
  5. dataset.setValue("Norway", 4606363);
  6. dataset.setValue("Denmark", 5427459);
  7. dataset.setValue("Iceland", 309699);
  8.  
  9. // Create chart using the ChartFactory
  10. JFreeChart chart = ChartFactory.createPieChart(
  11. "the Nordic countries", // Title
  12. dataset, // Dataset
  13. false, // Don’t show legend
  14. false,
  15. false);

The following code adjusts chart appearance and section colors. This is not mandatory but shows how easily you can customize the visual parts of the chart.

  1. // Adjust appearance (optional)
  2. chart.getTitle().setPaint(Color.LIGHT_GRAY);
  3. chart.setAntiAlias(true);
  4. PiePlot plot = (PiePlot)chart.getPlot();
  5. plot.setShadowPaint(Color.DARK_GRAY);
  6. plot.setBackgroundPaint(
  7. new GradientPaint(0,0,Color.DARK_GRAY,0,100,Color.GRAY) );
  8. plot.setLabelBackgroundPaint(Color.GRAY);
  9. plot.setLabelLinkPaint(Color.LIGHT_GRAY);
  10. plot.setLabelOutlinePaint(Color.LIGHT_GRAY);
  11. plot.setLabelShadowPaint(Color.DARK_GRAY);
  12.  
  13. // Adjust section colors (optional)
  14. Color[] colors = {
  15.     new Color(0xB0CC99),
  16.     new Color(0x677E52),
  17.     new Color(0xB7CA79),
  18.     new Color(0xF6E8B1),
  19.     new Color(0x89725B)
  20. };
  21. for(int i=0; i<dataset.getItemCount(); i++) {
  22.     Color sectionColor = colors[i%colors.length];
  23.     plot.setSectionPaint(i, sectionColor);
  24. }

That is all you have to do to create a chart object. Then if you wish to display this chart for example in your Swing application you’ll need to do the following:

  1. ChartPanel chartPanel = new ChartPanel( chart );
  2. yourPanel.add( chartPanel );

And this is the result:

JFreeChart example of PieChart

Fix for Sony Ericsson phones (MTE Beta)

Lots of Mobile Trail Explorer users have complained that application doesn’t work with Sony Ericcson phones. Latest version (1.8) doesn’t even seem to start. Phone only displays “Operation Failed” error message.

Fixed Sony Ericsson bug

Fortunately there are some active people in the community as one of the users discovered the source for the problem. He wrote the solution to the Google Code’s project site describing that the use of Samsung and Siemens specific APIs for the backlight feature was causing the trouble. When references were removed from the code base the application worked without problems.

import com.samsung.util.LCDLight;
import com.siemens.mp.game.Light;
...

Shortly after this I made the same changes to the code and I uploaded the fixed version for people to try out and I have already received multiple positive feedbacks from this change. Please try it out yourself and report back if the problems with the v1.8 are gone. Note: Unfortunately OutOfMemory exceptions are still there but hopefully we are also able to fix those in the future releases.

Download the beta v1.9 build here.