You are here: Home // Programming // JfreeChart Series-2: How to create a dynamic Pie Chart using JFreeChart library in Java

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

 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.
Blog Widget by LinkWithin

Related Articles:

  1. 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...
  2. File upload to BLOB field and display attachment dialog box Sometimes a developer needs to provide the feature in their...
  3. Step by step guide to create Singleton design pattern with Java Singleton design pattern is a kind of design pattern...
  4. Creating a struts project using Eclipse Ganymede Here is a short article that may help you while...
  5. Reading user input in java Sometimes in our stand alone java program we need to...
  6. How to load a properties file in Java This tutorial will explain how to access properties file through Java...
  7. How to create Windows executable EXE from a Java Jar Creating Windows executable EXE file from a Jar file: //...
  8. creating non duplicate array from duplicate array in Java This example helps you to generate a fresh and non...
  9. How to test if a String is palindrome or not in Java? Steps to check if a given string is a Palindrome...

Tags: , , , ,

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

  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???

Leave a Reply

Spam Protection by WP-SpamFree

Copyright © 2009 TechCuBeTalk free technology magazine. All rights reserved.
Designed by Theme Junkie. Powered by WordPress.