Chart a new course with JFreeChart(JFreeChart Tutorial)
As a developer, I am often asked to demonstrate new applications. After doing many demos, I noticed that users are often initially more interested in what an application looks like than what it does. I have also noticed that one of the best ways to make a good first impression is with a colorful, three-dimensional chart.
JFreeChart is a popular open source Java charting library that can generate most common chart types, including pie, bar, line, and Gantt charts. In addition, the JFreeChart API supports many interactive features, such as tool tips and zooming. JFreeChart provides an excellent choice for developers who need to add charts to Swing- or web-based applications.
Note: The following examples are based on JFreeChart version 0.9.4. To compile and run the code included with this column, you must have two jar files from the JFreeChart distribution, jfreechart-0.9.4.jar and jcommon-0.7.1.jar, in your classpath.
Charts and datasets
To create a chart using JFreeChart, you must create a Dataset, which you then use to create a JFreeChart. A Dataset contains the data that displays in the chart. JFreeChart features many different Dataset objects, which you can use to create assorted types of charts. Once you create a Dataset, you next create the actual chart. JFreeChart uses an object appropriately named JFreeChart to represent charts. You create JFreeChart objects from Dataset objects with the ChartFactory class. In the following examples, we will create pie, XY, and bar charts along with their corresponding Dataset objects.
Pie chart
A pie chart is created from a PieDataset. The following example creates a PieDataset using the DefaultPieDataset class, adds two values via the setValue() method, and then creates a pie chart with the ChartFactory's createPieChart() method. This example will create a pie chart with the title "Sample Pie Chart," a legend, and two slices: JavaWorld with 75 percent of the pie, and Other with the other 25 percent:
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));
JFreeChart chart = ChartFactory.createPieChart
("Sample Pie Chart", // Title
pieDataset, // Dataset
true // Show legend
);
XY chart
An XYDataset can create area, line, and step XY charts. The following example creates an XYDataset from a series of data containing three XY points. Next, ChartFactory's createAreaXYChart() method creates an area XY chart. In addition to parameters for title, dataset, and legend, createAreaXYChart() takes in the labels for the X and Y axes:
XYSeries series = new XYSeries("Average Size");
series.add(20.0, 10.0);
series.add(40.0, 20.0);
series.add(70.0, 50.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createAreaXYChart
("Sample XY Chart", // Title
"Height", // X-Axis label
"Weight", // Y-Axis label
xyDataset, // Dataset
true // Show legend
);
Bar chart
A CategoryDataset can create numerous different charts, including horizontal and vertical bar charts. The following example creates a CatagoryDataset with two series of data and two categories, and then creates a 3D vertical bar chart from this dataset. This example creates a chart that compares the sales growth in two quarters over two years:
String[] seriesNames = new String[] {"2001", "2002"};
String[] categoryNames = new String[] {"First Quater",
"Second Quater"};
Number[][] categoryData = new Integer[][] {{new Integer(20),
new Integer(35)},
{new Integer(40),
new Integer(60)}
};
CategoryDataset categoryDataset = new DefaultCategoryDataset
(seriesNames,
categoryNames,
categoryData);
JFreeChart chart = ChartFactory.createVerticalBarChart3D
("Sample Category Chart", // Title
"Quarters", // X-Axis label
"Sales", // Y-Axis label
categoryDataset, // Dataset
true // Show legend
);
Integrate JFreeChart
Integrating JFreeChart into a Swing application is relatively easy. Just create a BufferedImage from the chart and use the image as an icon for a JLabel:
BufferedImage image = chart.createBufferedImage(500,300);
JLabel lblChart = new JLabel();
lblChart.setIcon(new ImageIcon(image));
JFreeChart also includes a class named ChartUtilities that provides several methods for saving charts to files or writing them out to streams in JPEG or PNG format. For example, the following piece of code can export a chart to a JPEG:
ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500, 300);
The methods in the ChartUtilities class can be used to create JPEGs for use in a static Webpage, or used in a jsp (JavaServer Pages)/servlet-based application to dynamically stream charts to Webpages.
On your mark, get set, chart!
This article showed you how to create some common datasets and charts with the JFreeChart library. JFreeChart has much more to offer, including many chart types and advanced features such as combined charts and chart mouse events. Visit the JFreeChart homepage for the software's latest version, a servlet-based demo, links to projects using JFreeChart, and more
分享到:
- 2007-10-10 10:33
- 浏览 2226
- 评论(0)
- 论坛回复 / 浏览 (0 / 3769)
- 查看更多
相关推荐
**JFreeChart** 是一个广泛使用的Java库,用于创建各种图表,包括柱状图、饼图、线形图、散点图以及更多复杂类型的图表。它在Java应用程序、Web应用程序和移动应用程序中都有广泛的应用,提供了丰富的图形定制选项,...
1. [JasperReports官方文档](http://jasperreports.sourceforge.net/tutorial/index.html) 2. [iReport官方文档](http://ireport.sourceforge.net/docs.html) 3. [JFreeChart官方文档]...
jfreechart-1.5.2.jar,jfreechart|jfreechart
《JFreeChart:Java 图形绘制的利器》 在Java编程世界中,高效且美观的图形展示是数据可视化的重要组成部分。JFreeChart作为一款强大的开源图表库,为开发者提供了丰富的图表类型和高度自定义的能力,使得Java应用...
"JFreeChart 官方例子 JFreeChart Dome" 提供了一系列官方示例,帮助开发者更好地理解和运用JFreeChart库的各种功能。 "ChangeLog" 文件通常记录了软件的更新历史,其中包括了每次版本升级中添加的新特性、修复的...
**JFreeChart 演示程序详解** JFreeChart 是一个强大的 Java 图表库,它提供了丰富的图表类型,包括柱状图、饼图、线图、散点图、甘特图等,适用于各种数据可视化需求。这篇内容将深入探讨 JFreeChart 的基本使用...
jfreechart绘制的风速风向玫瑰图 jfreechart是Java中一个流行的图表库,它提供了许多种类的图表,包括柱状图、折线图、饼图、雷达图等。本文主要介绍如何使用jfreechart绘制风速风向玫瑰图。 首先,了解jfreechart...
JFreeChart 是一个流行的 Java 库,用于创建各种类型的图表,包括饼图、折线图、柱状图等。在使用 JFreeChart 进行数据可视化时,可能会遇到乱码的问题,这通常与字体设置不当有关。本文将详细分析 Jfreechart 乱码...
JFreeChart是一款强大的Java图表库,它允许开发者创建多种类型的2D图表,包括折线图、柱状图、饼图、散点图等。在Java应用程序中,JFreeChart经常被用于数据分析和可视化,尤其在报表系统、数据分析工具或者任何需要...
JFreeChart是一款强大的Java库,用于创建各种类型的图表,包括柱状图、饼图、线图、散点图等。在“JFreeChart混合图表演示”中,我们重点探讨如何利用JFreeChart来创建一个包含多种图表类型的混合图表,以提供更丰富...
JFreeChart 是一个强大的 Java 图形库,它为开发者提供了丰富的图表类型,包括柱状图、折线图、饼状图等,用于在 Java 应用程序中进行数据可视化。在 Java 后台开发中,数据的图形化表示能够帮助用户更直观地理解...
标题 "jfreechart" 指的是 JFreeChart,这是一个流行的开源 Java 图形库,用于创建高质量的图表。它在各种应用中广泛使用,包括报表、仪表盘、科学出版物等,提供了丰富的图表类型,如饼图、柱状图、线图、散点图和...
**JFreeChart 1.0.9:一个强大的Java图表库** JFreeChart是一个流行的开源Java库,用于创建各种高质量的图表,包括折线图、柱状图、饼图、散点图、甘特图等。它在描述复杂数据集时提供了一种直观的方式,广泛应用于...
**JFreeChart库详解** JFreeChart是一款强大的Java图表库,它允许开发者在Java应用程序或网页中创建多种类型的图表,包括雷达图(也称为蜘蛛网图)。本篇将深入探讨如何利用JFreeChart生成雷达图以及相关的数据导出...
**JFreeChart 使用详解** JFreeChart 是一个用于 Java 的开源图表库,它提供了一种简单的方法来创建各种复杂的图表,如折线图、柱状图、饼图、散点图以及更专业的图表类型,如甘特图和雷达图。在Java应用中,如果你...
### jfreechart中文学习文档 #### 一、引言 ##### 1.1 JFreeChart 是什么? **1.1.1 概览** JFreeChart 是一款为 Java 平台设计的免费图表库。这款工具非常适合在各种 Java 应用程序、Applet 和 Servlet 中使用...
JFreeChart是一款功能强大的Java图表库,它能够生成多种图表,如柱状图、折线图、饼图、时间序列图等,常用于Java应用程序中。而Eclipse是一个开源的集成开发环境,广泛用于Java语言的开发。在Eclipse中配置...