jfreechart_tutorial.pdf

(528 KB) Pobierz
ClassContribution
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 from these 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 Java project 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 the Package 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 and click 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 -
1196889.014.png 1196889.015.png 1196889.016.png 1196889.017.png 1196889.001.png 1196889.002.png 1196889.003.png 1196889.004.png
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 org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.data.general.DefaultPieDataset;
import java.io.File;
public class PieChartExample {
public static void main(String[] args) {
// Create a simple pie chart
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("A", new Integer(75));
pieDataset.setValue("B", new Integer(10));
pieDataset.setValue("C", new Integer(10));
pieDataset.setValue("D", new 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 -
1196889.005.png 1196889.006.png
Explanation:
To define a data set for a 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 set
as 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().
ChartUtilities.saveChartAsJPEG(new File(”C:\\chart.jpg”), chart, 500, 300);
Modification: To generate a PNG image use the method saveChartAsPNG().
2. XY Chart Example
Suppose that we want to construct a line with the following set of (x, y) coordinates:
{(1, 1), (1, 2), (2, 1), (3, 9), (4, 10)}.
Code:
public class XYChartExample {
public static void main(String[] args) {
// Create a simple XY chart
XYSeries series = new XYSeries("XYGraph");
series.add(1, 1);
series.add(1, 2);
series.add(2, 1);
series.add(3, 9);
series.add(4, 10);
// Add the series to your data set
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
// Generate the graph
JFreeChart chart = ChartFactory.createXYLineChart(
"XY Chart", // Title
"x-axis", // x-axis Label
"y-axis", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
try {
ChartUtilities.saveChartAsJPEG(new File("C:\\chart.jpg"), chart, 500, 300);
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
}
}
}
- 3 -
1196889.007.png
Graph:
Explanation:
To define a set of (x, y) coordinates, use an object of class XYSeries .
XYSeries series = new XYSeries("XYGraph");
Add the series to your data set of type XYSeriesCollection .
dataset.addSeries(series);
Note: Multiple series (lines) can be added to one data set. Use this feature if you want more than
one line to appear on the same graph.
A graph object of type JFreeChart is generated using the ChartFactory method createXYLineChart().
Note the extra arguments that did not appear for a pie chart. The 2nd and 3rd arguments specify
the label for the x and y axes.
Meaning of Plot Orientation:
PlotOrientation.VERTICAL - y-axis is displayed as the vertical axis
PlotOrientation.HORIZONTAL - x-axis becomes the vertical axis
Modification:
In the current code, we used the createXYLineChart() method to display a line of data in the graph.
As an alternative, the method createXYAreaChart() could be used to display the area under the line
of data.
Change to
Area Chart
It is possible to modify the graph to show each of the 5 data points:
XYItemRenderer rend = chart.getXYPlot().getRenderer();
StandardXYItemRenderer rr = (StandardXYItemRenderer)rend;
rr.setPlotShapes(true);
Mark data
points
- 4 -
1196889.008.png 1196889.009.png 1196889.010.png 1196889.011.png
3. Bar Chart Example
Suppose that we want to construct a bar graph which compares the profits taken in
by the following salesmen: Jane, Tom, Jill, John, Fred.
Code:
public class BarChartExample {
public static void main(String[] args) {
// Create a simple Bar chart
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(6, "Profit", "Jane");
dataset.setValue(7, "Profit", "Tom");
dataset.setValue(8, "Profit", "Jill");
dataset.setValue(5, "Profit", "John");
dataset.setValue(12, "Profit", "Fred");
JFreeChart chart = ChartFactory.createBarChart("Comparison between Salesman",
"Salesman", "Profit", dataset, PlotOrientation.VERTICAL,
false, true, false);
try {
ChartUtilities.saveChartAsJPEG(new File("C:\\chart.jpg"), chart, 500, 300);
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
}
}
}
Graph:
Explanation:
To define a data set for a bar graph use an object of class DefaultCategoryDataset .
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Values can be added to the data set using the setValue() method.
dataset.setValue(6, “Profit”, “Jane”);
The first argument specifies the level of profit achieved by Jane. The second argument specifies
what will appear in the legend for the meaning of a bar.
To generate a bar graph object of class JFreeChart , the method createBarChart() of
ChartFactory is used. It takes the same set of arguments as that required by
createXYLineChart(). The 1st argument denotes the title of the graph, the second
the label for the x-axis, the third the label for the y-axis.
JFreeChart chart = ChartFactory.createBarChart("Comparison between Salesman",
"Salesman", "Profit", dataset, PlotOrientation.VERTICAL, false, true, false);
Modification: As was the case with pie charts, it is possible to display the bars in 3D using the createBarChart3D() method.
- 5 -
1196889.012.png 1196889.013.png
Zgłoś jeśli naruszono regulamin