Tutorial jfreechart

Solo disponible en BuenasTareas
  • Páginas : 8 (1842 palabras )
  • Descarga(s) : 0
  • Publicado : 11 de mayo de 2011
Leer documento completo
Vista previa del texto
JFreeChart Tutorial
Matthew D’Andrea | E-mail: matthew.dandrea@utoronto.ca

Uses
JFreeChart is an open source library available for Java that allows users to easily generate graphs and charts. It is particularly effective for when a user needs to regenerate graphs that change on a frequent basis (as required for the CSC408 course project). This tutorial will examine how to create images fromthese graphs which can then be displayed on web pages.

Download Location
JFreeChart is available for download from: http://sourceforge.net/project/showfiles.php?group_id=15494&package_id=12428 I recommend downloading either 1.0.0-pre1 or Version 0.9.21. Unpack JFreeChart into a temporary directory.

How To Install / Get Started
I will briefly outline the steps needed to setup a Javaproject in Eclipse so that you can create your own JFreeChart graphs. You will then be able to test out the code provided in the “Example Graphs” section. Steps: 1. Start Eclipse 3.0. (If you do not have Eclipse installed, visit www.eclipse.org.) 2. Go to File -> New -> Project.... 3. Select “Java Project” and click on Next. Enter in a project name and click Finish. 4. Select the project in thePackage Explorer view. Go to Project -> Properties. 5. Click on “Java Build Path” on the left-hand side of the dialog box and on the right-hand side click on the “Libraries” tab. 6. Click on “Add External JARs...” and locate jfreechart-1.0.0-pre1.jar and jcommon-1.0.0-common.jar. Click on OK. 7. Select the project in the Package Explorer view. Go to File -> New -> Package. Give the package a name andclick on Finish. 8. Select the newly-created package in the Package Explorer view. Go to File -> New -> Class. Give the class a name and click on Finish. 9. You are now ready to start using the JFreeChart library!

Types of Graphs Constructed with JFreeChart
We will consider the following graphs that can be created using JFreeChart: - Pie Chart - Time Series Chart - XY Chart - Bar Chart

-1- Example Graphs
1. Pie Chart Example Suppose that we want to construct a Pie Chart that reflects the percentage of marks that are in the A, B, C, and D range for CSC408. (You’ll notice that the distribution is rather hopeful. :)) Code:
package main; import import import import import org.jfree.chart.JFreeChart; org.jfree.chart.ChartUtilities; org.jfree.chart.ChartFactory;org.jfree.data.general.DefaultPieDataset; java.io.File;

public class PieChartExample { public static void main(String[] // Create a simple pie chart DefaultPieDataset pieDataset pieDataset.setValue("A", new pieDataset.setValue("B", new pieDataset.setValue("C", new pieDataset.setValue("D", new

args) { = new DefaultPieDataset(); Integer(75)); Integer(10)); Integer(10)); Integer(5));

JFreeChart chart =ChartFactory.createPieChart ("CSC408 Mark Distribution", // Title pieDataset, // Dataset true, // Show legend true, // Use tooltips false // Configure chart to generate URLs? ); try { ChartUtilities.saveChartAsJPEG(new File("C:\chart.jpg"), chart, 500, 300); } catch (Exception e) { System.out.println("Problem occurred creating chart."); } } }

Graph:

-2-

Explanation:
To define a data set fora pie chart we create an instance of type DefaultPieDataSet. DefaultPieDataset pieDataset = new DefaultPieDataset(); The setValue() method is used to set the name of a piece of the pie (e.g. “A”) and its corresponding percentage (e.g. 75) value. PieDataset.setValue(’A”, new Integer(75)); A graph object of type JFreeChart is generated using one of the ChartFactory methods and passing the data setas one of the arguments. In this case, we use the createPieChart() method to produce a pie chart. Modification: Use createPieChart3D() to produce a corresponding 3D version of the pie chart. JFreeChart chart = ChartFactory.createPieChart3D(”CSC408 Mark Distribution”, pieDataset, true, true, false); A JPEG image of the graph is produced using the ChartUtilities method saveChartAsJPEG()....
tracking img