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(0×677E52),
  17.     new Color(0xB7CA79),
  18.     new Color(0xF6E8B1),
  19.     new Color(0×89725B)
  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

3 Responses to “Charting with JFreeChart”

  1. tarawat Says:

    nice tips!
    Thanks for share it.

  2. Chandana Says:

    I am using netbeans 5.5

    I am attempting to run the following jFreeChart demo but in netbeans.
    Original demo located here: http://www.java2s.com/Code/Java/Chart/JFreeChartPieChartDemo7.htm

    Instead of programically creating the jPanel like the demo does, i would like to do it using the netbeans GUI and then add the code to create the jFreeChart.In netbeans i create a new project, create a new jFrame and place a new jPanel on the form and add the JTabpane in to Jpanel. After that I add two Jpanel in to Jtabpanes.

    that one of Jpane i want to Show chart using Jfreecharts. I try to do it. But it never show my Tabpane and Charts. it open new Jframe and show charts. But I want to show that Charts in the Jpanel.

    If Any one know how to do it. If u can reply this.
    Thank you…..

  3. Tommi Laukkanen Says:

    @Chandana: One of the most common mistake I usually have made is that I don’t have specified any layout manager for the components. You will have to use some layout manager so that Swing knows how it should layout the components on the screen.

    When using NetBeans IDE you can do it for example in the Inspector window that is shown when you are designing your component. Right click the component (eg. JPanel) and select: Set Layout > GridLayout (or any other).

    Maybe this will solve your problem?

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word