7.2 使用categoryDataset数据集创建折线图
7.2.1 概述
使用CategoryDataset创建的折线图将每个数据点(种类,值)使用一条直线连接起来。本章讲的一个简单应用产生如下界面,如图7.1:
图7.1 一个简单的折线图
全部的代码简JFreeChart开发指南一并下载的demo(参考:LineChartDemo1.java)。
7.2.2 CategoryDataset
正如其他图表一样,创建折线图的第一步是创建第一个dataset。在本例子中,使用DefaultCategoryDataset,代码如下:
private static CategoryDataset createDataset() {
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(212.0, "Classes", "JDK 1.0");
defaultcategorydataset.addValue(504.0, "Classes", "JDK 1.1");
defaultcategorydataset.addValue(1520.0, "Classes", "JDK 1.2");
defaultcategorydataset.addValue(1842.0, "Classes", "JDK 1.3");
defaultcategorydataset.addValue(2991.0, "Classes", "JDK 1.4");
defaultcategorydataset.addValue(3500.0, "Classes", "JDK 1.5");
return defaultcategorydataset;
}
注意:你可以使用任何实现Category接口的数据集。
7.2.3 创建图表
ChartFactory类提供了一个便利的方法createLineChart()创建折线图。代码如下:
JFreeChart jfreechart = ChartFactory.createLineChart(
"Java Standard Class Library",// 图表标题
null, // 主轴标签
"Class Count",// 范围轴标签
categorydataset, // 数据集
PlotOrientation.VERTICAL,// 方向
false, // 是否包含图例
true, // 提示信息是否显示
false// 是否使用urls
);
该方法构建了一个带有标题、图例、和相应的数轴和心态提示产生器的JFreeChart对象。创建Dataset数据集的过程见上节。
7.2.4 定制图表
折线图表将使用大部分缺省的属性来进行初始化。当然了,我们也可以随意修改折线图的属性,来改变我们图表的外观。在本例中,我们通过下面的方式定制折线图:
- 在图表上添加两个副标题;
- 图表的背景颜色设成白色;
- 图区背景颜色设成亮灰色;
- 网格线颜色改变成白色;
- 范围轴修改成仅显示整数数值;
- renderer使用白色填充的形状。
首先,将副标题添加在缺省的位置(主标题下方),代码如下:
jfreechart.addSubtitle(new TextTitle("Number of Classes By Release"));
第二个副标题加入了一些额外的代码,来改变字体,并放置在图表的下方,并且靠右对其,代码如下:
TextTitle texttitle = (new TextTitle("Source: Java In A Nutshell (5th Edition) by David Flanagan (O'Reilly)"));
texttitle.setFont(new Font("SansSerif", 0, 10));
texttitle.setPosition(RectangleEdge.BOTTOM);
texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
jfreechart.addSubtitle(texttitle);
改变图表的背景颜色非常简单,因为JFreeChart类就有设置背景颜色的属性。代码如下:
//改变图表的背景颜色
jfreechart.setBackgroundPaint(Color.white);
如果改变其他属性的,则需要首先获得图表CategoryPlot对象的引用,然后对该引用进行相应属性的设置。获得对象引用的代码如下:
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
使用CategoryPlot设置图区的背景颜色为亮灰色,设置网格线颜色为白色的代码如下:
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
图区负责在图表上画出数据和轴。其中一部分工作由renderer来完成,我们可以通过getRender()来获得一个renderer。renderer维护大部分与图表内数据项的显示相关的属性。代码如下:
LineAndShapeRenderer renderer = (LineAndShapeRenderer) categoryplot.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
同时图区也管理着图表所有的轴。在本实例中,修改范围轴以便范围轴的刻度标签显示为整数值。
NumberAxis rangeAxis = (NumberAxis) categoryplot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
定制图表还有很多其他的方式。具体的内容详见本文档相关章节,API文档以及源代码实例。
7.2.5 程序的全部代码
/* -------------------
* LineChartDemo1.java
* -------------------
* (C) Copyright 2002-2005, by Object Refinery Limited.
*
*/
package demo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
public class LineChartDemo1 extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = -6354350604313079793L;
/* synthetic */static Class class$demo$LineChartDemo1;
public LineChartDemo1(String string) {
super(string);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}
private static CategoryDataset createDataset() {
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(212.0, "Classes", "JDK 1.0");
defaultcategorydataset.addValue(504.0, "Classes", "JDK 1.1");
defaultcategorydataset.addValue(1520.0, "Classes", "JDK 1.2");
defaultcategorydataset.addValue(1842.0, "Classes", "JDK 1.3");
defaultcategorydataset.addValue(2991.0, "Classes", "JDK 1.4");
defaultcategorydataset.addValue(3500.0, "Classes", "JDK 1.5");
return defaultcategorydataset;
}
private static JFreeChart createChart(CategoryDataset categorydataset) {
JFreeChart jfreechart = ChartFactory.createLineChart(
"Java Standard Class Library",// 图表标题
null, // 主轴标签
"Class Count",// 范围轴标签
categorydataset, // 数据集
PlotOrientation.VERTICAL,// 方向
false, // 是否包含图例
true, // 提示信息是否显示
false// 是否使用urls
);
// 添加主标题
jfreechart.addSubtitle(new TextTitle("Number of Classes By Release"));
TextTitle texttitle = (new TextTitle(
"Source: Java In A Nutshell (5th Edition) by David Flanagan (O'Reilly)"));
texttitle.setFont(new Font("SansSerif", 0, 10));
texttitle.setPosition(RectangleEdge.BOTTOM);
texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
jfreechart.addSubtitle(texttitle);
// 改变图表的背景颜色
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
categoryplot.setRangeGridlinesVisible(false);
URL url = (class$demo$LineChartDemo1 == null ? class$demo$LineChartDemo1 = class$("demo.LineChartDemo1")
: class$demo$LineChartDemo1).getClassLoader().getResource(
"OnBridge11small.png");
if (url != null) {
ImageIcon imageicon = new ImageIcon(url);
jfreechart.setBackgroundImage(imageicon.getImage());
categoryplot.setBackgroundPaint(new Color(0, 0, 0, 0));
}
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
lineandshaperenderer.setShapesVisible(true);
lineandshaperenderer.setDrawOutlines(true);
lineandshaperenderer.setUseFillPaint(true);
lineandshaperenderer.setBaseFillPaint(Color.white);
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3.0F));
lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
lineandshaperenderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0,
10.0, 10.0));
LineAndShapeRenderer renderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
return jfreechart;
}
public static JPanel createDemoPanel() {
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String[] strings) {
LineChartDemo1 linechartdemo1 = new LineChartDemo1(
"JFreeChart - Line Chart Demo 1");
linechartdemo1.pack();
RefineryUtilities.centerFrameOnScreen(linechartdemo1);
linechartdemo1.setVisible(true);
}
/* synthetic */
static Class class$(String string) {
Class var_class;
try {
var_class = Class.forName(string);
} catch (ClassNotFoundException classnotfoundexception) {
throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
return var_class;
}
}