Creating A 3D Pie Chart In Java Using JFreeChart [Example Source Code]

JFreeChart is an open-source framework for the programming language Java, which allows the creation of complex charts in a simple way.

JFreeChart also works with GNU Classpath, a free software implementation of the standard class library for the Java programming language.

Following chart types are supported:

  • * X-Y charts (line, spline and scatter)
  • * Pie charts
  • * Gantt charts
  • * Bar charts (horizontal and vertical, stacked and independent)
  • * Single valued (thermometer, compass, speedometer)

In the following tutorial, I will show you how to create a 3D Pie Chart using JFreeChart. If you do not have the JFreeChart library and API, you can download it from their website www.jfree.org

package com.kushal.charts;
/**
 * @Author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modfied On: 2009-09-23
 * 
 * Using JFreeChart to create 3D Pie Chart
 */
import java.awt.Color;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class My3DPieChart {
	private static final long serialVersionUID = -8625975218180144099L;

	private PieDataset createPieDataSet() {
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		/**
		 * Filling the data Visit of Sanjaal.com on a random day by Browser
		 */
		pieDataset.setValue("MS Internet Explorer", 184707);
		pieDataset.setValue("Firefox", 144556);
		pieDataset.setValue("Google Chrome", 21638);
		pieDataset.setValue("Opera", 21609);
		pieDataset.setValue("Unknown", 17916);
		pieDataset.setValue("Safari", 10850);
		pieDataset.setValue("Mozilla", 7081);
		pieDataset.setValue("Netscape", 1010);
		pieDataset.setValue("Curl", 405);
		pieDataset.setValue("LibWWW", 121);

		return pieDataset;

	}

	private JFreeChart create3DPieChart(PieDataset dataset) {

		/**
		 * createPieChart3D method Parameters: --java.lang.String title,
		 * --PieDataset dataset, --boolean legend, --boolean tooltips, --boolean
		 * urls
		 */
		JFreeChart chart = ChartFactory.createPieChart3D(
				"Sanjaal.com Visits By Browser", dataset, true, true, true);

		PiePlot3D p = (PiePlot3D) chart.getPlot();
		p.setForegroundAlpha(0.5f);
		p.setBackgroundAlpha(0.2f);

		chart.setBackgroundPaint(Color.white);
		chart.setAntiAlias(true);
		chart.setBorderVisible(true);

		return chart;

	}

	public void saveChart(JFreeChart chart, String fileLocation) {
		String fileName = fileLocation;
		try {
			/**
			 * This utility saves the JFreeChart as a JPEG First Parameter:
			 * FileName Second Parameter: Chart To Save Third Parameter: Height
			 * Of Picture Fourth Parameter: Width Of Picture
			 */
			ChartUtilities.saveChartAsJPEG(new File(fileName), chart, 800, 600);
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Problem occurred creating chart.");
		}
	}

	public static void main(String[] args) {
		My3DPieChart chartCreator = new My3DPieChart();

		/** Create a PieDataSet* */
		PieDataset pieDataset = chartCreator.createPieDataSet();

		/** Create 3D Pie Chart based on this dataset* */
		JFreeChart chart = chartCreator.create3DPieChart(pieDataset);

		/** Define a file location to save this chart */
		String fileLocation = "C:/temp/my3DPieChart.jpg";

		/** Save the chart to the file system* */
		chartCreator.saveChart(chart, fileLocation);

		System.out.println("3D Pie Chart has been created successfully");
		System.out.println("Chart has been saved to: " + fileLocation);
	}

	/*
	 * SANJAAL CORPS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
	 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
	 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
	 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SANJAAL CORPS SHALL NOT BE
	 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
	 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
	 * 
	 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
	 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
	 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
	 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
	 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
	 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
	 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SANJAAL CORPS
	 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
	 * HIGH RISK ACTIVITIES.
	 */
}

The following is the output if this program:

3D Pie Chart has been created successfully
Chart has been saved to: C:/temp/my3DPieChart.jpg

Generated PieChart for the data provided looks like this:

  1. Creating Time Series Chart With JFree Chart
  2. Creating A 3D Category Bar Chart In Java Using JFreeChart [Example Source Code]
  3. Java Object Serialization and Deserialization In MySQL Database
  4. How To Set Conditional Debug Breakpoints in Eclipse or IBM RAD?
  5. Rotating An Image In Java Graphics 2D By Specified Degree
  6. Java – How To Overlay One Image Over Another Using Graphics2D [Tutorial]
  7. Creating 3D Pie Chart using JFreeChart with chart data read from file
  8. Creating Gantt Chart In Java Using JFreeChart API [Example/Tutorial]
  9. Quick SQL Reference – Find Duplicate Data In A Table Using Having Clause
  10. Splitting PDF File Using Java iText API Into Multiple PDFs
  11. Object Serialization And De-Serialization In Java To Filesystem
  12. Finding Java Image Pixels Information – ARGB (Alpha, Red, Green Blue)
  13. Creating A 3D Pie Chart In Java Using JFreeChart [Example Source Code]
  14. Java Tutorial – Swing Text Drag And Drop – Sample Example
  15. Serialzing A Java Object Into XML and De-Serialzing using XMLEncoder And XMLDecoder
  16. Six Stages Of Debugging in Software Engineering
  17. Java 2D Graphics Example Tutorial On Paint, Gradient and Stroke
  18. Complete Tutorial On Using SOAP-UI to Mock Web Service Request / Response
  19. Listing The Content Of Zip File With Zip Information In Java
  20. Joke – Why do Java Programmers wear glasses?
  21. How to receive files via bluetooth in your MacBook Pro
  22. Creating Category Chart Using JFreeChart
  23. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

5 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

  5. Neethu says:

    Thank you very much for the solution. This is the one which i was searching.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.