JfreeChart Series-2: How to create a dynamic Pie Chart using JFreeChart library in Java

Posted on Jan 17, 2009 | 4 Comments

 In the earlier tutorial I showed you how to create Bar chart using JFreeChart library. In this second article I’ll explain how to create pie chart. Everything is same like the previous tutorial, except the servlet code will be changed.


So let me first put the servlet code here:

package com.cts.chartgen.servlets;
 
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DefaultPieDataset;
 
public class PieChartGen extends HttpServlet {
 
      public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            genPieChart(req, resp);
      }
 
      public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            genPieChart(req, resp);
      }
 
      public void genPieChart(HttpServletRequest req,
                              HttpServletResponse resp) {
            try {
                  OutputStream out = resp.getOutputStream();
                  // Create a simple Bar chart
                  DefaultPieDataset dataset = new DefaultPieDataset();
 
                  dataset.setValue(“SCIENCE CLASS”, 65);
                  dataset.setValue(“ECONOMICS CLASS”, 25);
                  dataset.setValue(“LANGUAGE CLASS”, 10);
 
                  JFreeChart chart =
                        ChartFactory.createPieChart(
                              “Students by Classes”,
                              dataset,
                              true,
                              true,
                              false);
                  chart.setBackgroundPaint(Color.white);
                  // Set the background colour of the chart
 
                  resp.setContentType(“image/png”);
                  ChartUtilities.writeChartAsPNG(out, chart, 625, 500);
 
            } catch (Exception e) {
                  System.err.println(
                        “Problem occurred while creating chart.” + e.getMessage());
            }
      }
}
 
 
And the output is:

 piechartgen

 

 

 

Let me explain-
  • An instance of DefaultPieDataset is created and this is populated with the statistical data. The data is set by invoking setValue() method.
  • Now the createPieChart() method of ChartFactory is invoked which creates the actual pie chart object. The first argument is the name of the statement to be displayed, 2nd argument is the dataset itself, 3rd argument as true specifies if legends to be displayed, 4th argument holds true if we want to display tooltip and false as the 5th argument specifies if we need to place URL in the chart image.
  • We set the background color of the chart as white. The default color is grey. This again can be customized.
  • Next we generate the chart to be displayed using writeChartAsPNG() method of ChartUtilities class.

Related Articles:

  1. JfreeChart Series-3: How to create a dynamic 3D Pie Chart using JFreeChart library in Java You may refer to the earlier tutorials if you have...
  2. JfreeChart Series-1: How to create a dynamic Bar Chart using JFreeChart library in Java In Java creating any graph or chart like bar chart...
  3. File upload to BLOB field and display attachment dialog box Sometimes a developer needs to provide the feature in their...
  4. Step by step guide to create Singleton design pattern with Java Singleton design pattern is a kind of design pattern which...
  5. Creating a struts project using Eclipse Ganymede Here is a short article that may help you while...
  6. Reading user input in java Sometimes in our stand alone java program we need to...
  7. How to create Windows executable EXE from a Java Jar Creating Windows executable EXE file from a Jar file: //...
  8. Java Comparator example Sometimes we need to sort objects based on certain attributes...
  9. How to load a properties file in Java This tutorial will explain how to access properties file through Java...
  10. creating non duplicate array from duplicate array in Java This example helps you to generate a fresh and non...

4 comments

  1. Jorge says:

    Nice tutorial… I’m a newby… can you help me if instead of inserting the values directly… you colect the values from a txt file with this kind of layout…

    error number, error description, values

    Basicaly.. the program will perform a scan and when finding the error number.. it will collect the the value situated in the 2nd position…

    thanks

  2. Mitch R says:

    Thanks for the tutorial. I came here because all my pie charts have a grey background and I can’t change it to white. I ran the code in this tutorial and unfortunately I still get the grey background unlike the screenshot you have.

    Are any of you getting the same? Its really annoying!

  3. Luis says:

    Sorry for the post before I didn’t realize..
    I said that it doesn’t work for me…it doesn’t reply anything

    Maybe is because you don’t implement the getplot function???

  4. Eric says:

    I had this problem also. I am guessing you are trying to change the grey background to white by calling:

    chart.setBackgroundPaint(Color.white)

    as shown above. Funny, that they would show this example, but it doesn’t work! You have to modify the chart’s plot object as in the following code:

    chart.getPlot().setBackgroundPaint(Color.white);
    chart.getPlot().setOutlineVisible(false);

    Good luck!
    Eric

Leave a comment

Spam Protection by WP-SpamFree

Advertisement

Subscription

You can subscribe by e-mail to receive news updates and breaking stories.