JFreeChart教程 一、JFreeChart获取。 JFreeChart是JFreeChart公司在开源网站SourceForge.net上的一个项目,该公司的主要产品有如下: 1、JFreeReport:报表解决工具 2、JFreeChart:Java图形解决方案(Application/Applet/Servlet/Jsp) 3、JCommon:JFreeReport和JFreeChart的公共类库 4、JFreeDesigner:JFreeReport的报表设计工具 我们可以从jfree官方网站上获取最新版本(最新的版本是1.0.5)和相关资料(但是jfree的Developer Guides是需要花美金才能获取), 获取地址:http://www.jfree.org/jfreechart/index.html(同时可以获得简明介绍) 我们以当前版本:jfreechart-1.0.0-rc3.tar.gz为例子进行说明 二、JFreeChart配置安装 1、解压jfreechart_0.9.21.zip到指定位置,其中source是jfreechart的源码,jfreechart-0.9.21-demo.jar 是例子程序(该部分留给大家自己去研究) 2、为了配置成功,我们需要关注的文件有如下三个:jfreechart-0.9.21.jar、lib\jcommon-0.9.6.jar、 lib\gnujaxp.jar 3、如果是Application开发,把上述三个文件拷贝到%JAVA_HOME%\LIB中,同时在环境变量CLASSPATH中加入 二、JFreeChart配置安装 1、解压jfreechart_0.9.21.zip到指定位置,其中source是jfreechart的源码,jfreechart-0.9.21-demo.jar 是例子程序(该部分留给大家自己去研究) 2、为了配置成功,我们需要关注的文件有如下三个:jfreechart-0.9.21.jar、lib\jcommon-0.9.6.jar、 lib\gnujaxp.jar 3、如果是Application开发,把上述三个文件拷贝到%JAVA_HOME%\LIB中,同时在环境变量CLASSPATH中加入 如果是WEB开发,以TOMCAT中的一个WEB项目TEST为例子说明: 把上述三个文件拷贝到TEST\WEB-INF\LIB中,然后修改TEST\WEB-INF\web.xml文件,在其中加入如下代码: <servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern> </servlet-mapping> 至此jfreechart的配置就完成了,下面就可以进行jfreechart的开发了。这里值得提出的是jfreechart的类 结构设计前后兼容性不是很好,不同版本的jfreechart中类库结构可能不一样,有时候可能需要查源码。如果 是中文显示的时候可能依据观感需要改变源码的字体,不过我个人觉得这个版本比以前版本要好一些。 三、JFreeChart功能介绍 JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求,主要包括如下几个方面: pie charts (2D and 3D):饼图(平面和立体) bar charts (regular and stacked, with an optional 3D effect):柱状图 line and area charts:曲线图 scatter plots and bubble charts time series, high/low/open/close charts and candle stick charts:时序图 combination charts:复合图 Pareto charts Gantt charts:甘特图 wind plots, meter charts and symbol charts wafer map charts (态图表,饼图(二维和三维) , 柱状图 (水平,垂直),线图,点图,时间变化图,甘特图, 股票行情图,混和图, 温度计图, 刻度图等常用商用图表) 图形可以导出成PNG和JPEG格式,同时还可以与PDF和EXCEL关联 JFreeChart核心类库介绍: 研究jfreechart源码发现源码的主要由两个大的包组成:org.jfree.chart,org.jfree.da 核心类主要有: org.jfree.chart.JFreeChart:图表对象,任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象 org.jfree.da org.jfree.chart.plot.XXXPlot:图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持 org.jfree.chart.axis.XXXAxis:用于处理图表的两个轴:纵轴和横轴 org.jfree.chart.render.XXXRender:负责如何显示一个图表对象 org.jfree.chart.urls.XXXURLGenerator:用于生成Web图表中每个项目的鼠标点击链接 XXXXXToolTipGenerator:用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类 四、JFreeChart开发(Application/Applet) on /** * JfreeChartTest.java * author:longlong * May 31, 2007 */ package com.ghg.java; imp imp public class JfreeChartTest { public static void main(String[] args) { //创建饼图数据对象 DefaultPieDataset dfp=new DefaultPieDataset(); dfp.setValue("管理人员", 25); dfp.setValue("市场人员", 35); dfp.setValue("开发人员", 20); dfp.setValue("后勤人员", 5); dfp.setValue("财务人员", 15); //Create JFreeChart object JFreeChart a =ChartFactory.createPieChart("CityInfoPort公司组织架构图",dfp,true,true,true); ChartFrame frame=new ChartFrame ("CityInfoPort公司组织架构图 ",a,true); frame.pack(); frame.setVisible(true); } } Refactor on /** * JfreeChartOne.java * author:longlong * May 31, 2007 */ package com.ghg.java; imp imp imp imp imp imp imp imp imp imp imp imp public class JfreeChartOneextends ApplicationFrame { private static final long serialVersionUID = 1L; public JfreeChartOne(String s) { super(s); setContentPane(createJPanel()); } public static void main(String[] args) { JfreeChartOne on on on } //利用静态方法设定数据源(饼状图) public static PieDataset createPieDataset() { DefaultPieDataset defaultpiedataset =new DefaultPieDataset(); defaultpiedataset.setValue("管理人员", 10.02D); defaultpiedataset.setValue("市场人员", 20.23D); defaultpiedataset.setValue("开发人员", 60.02D); defaultpiedataset.setValue("OEM人员", 10.02D); defaultpiedataset.setValue("其他人员", 5.11D); return defaultpiedataset; } //通过ChartFactory创建JFreeChart的实例 public static JFreeChart createJFreeChart(PieDataset p) { JFreeChart a =ChartFactory.createPieChart("CityInfoPort公司组织架构图",p,true,true,true); PiePlot pie=(PiePlot )a.getPlot(); pie.setLabelFont(new Font("SansSerif",Font.BOLD,12)); pie.setNoDataMessage("No da pie.setCircular(true); pie.setLabelGap(0.01D);//间距 return a; } public static JPanel createJPanel (){ JFreeChart jfreechart = createJFreeChart(createPieDataset()); return new ChartPanel(jfreechart); } } 效果如图所示: two:创建类别图 /** * JfreeChartTest.java * author:longlong * May 31, 2007 */ package com.ghg.java; imp imp imp imp public class JfreeChartTest { public static void main(String[] args) { // 创建类别图(Category)数据对象 DefaultCategoryDataset dataset =new DefaultCategoryDataset(); dataset.addValue(100,"北京","苹果"); dataset.addValue(100,"上海","苹果"); dataset.addValue(100,"广州","苹果"); dataset.addValue(200,"北京","梨子"); dataset.addValue(200,"上海","梨子"); dataset.addValue(200,"广州","梨子"); dataset.addValue(300,"北京","葡萄"); dataset.addValue(300,"上海","葡萄"); dataset.addValue(300,"广州","葡萄"); dataset.addValue(400,"北京","香蕉"); dataset.addValue(400,"上海","香蕉"); dataset.addValue(400,"广州","香蕉"); dataset.addValue(500,"北京","荔枝"); dataset.addValue(500,"上海","荔枝"); dataset.addValue(500,"广州","荔枝"); JFreeChart chart=ChartFactory.createBarChart3D("水果产量图","水果","水果", dataset, PlotOrientation.VERTICAL,true,true,true); ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true); frame.pack(); frame.setVisible(true); } } 按照on three:创建折线图 //创建折线图(Category)数据对象 String series1 ="First"; String series2 ="Second"; String series3 ="Third"; String type1 ="2001"; String type2 ="2002"; String type3 ="2003"; String type4 ="2004"; String type5 ="2005"; String type6 ="2006"; String type7 ="2007"; String type8 ="2008"; DefaultCategoryDataset defaultcategorydataset =new DefaultCategoryDataset(); defaultcategorydataset.addValue(1.0D, series1, type1); defaultcategorydataset.addValue(4D, series1, type2); defaultcategorydataset.addValue(3D, series1, type3); defaultcategorydataset.addValue(5D, series1, type4); defaultcategorydataset.addValue(5D, series1, type5); defaultcategorydataset.addValue(7D, series1, type6); defaultcategorydataset.addValue(7D, series1, type7); defaultcategorydataset.addValue(8D, series1, type8); defaultcategorydataset.addValue(5D, series2, type1); defaultcategorydataset.addValue(7D, series2, type2); defaultcategorydataset.addValue(6D, series2, type3); defaultcategorydataset.addValue(8D, series2, type4); defaultcategorydataset.addValue(4D, series2, type5); defaultcategorydataset.addValue(4D, series2, type6); defaultcategorydataset.addValue(2D, series2, type7); defaultcategorydataset.addValue(1.0D, series2, type8); defaultcategorydataset.addValue(4D, series3, type1); defaultcategorydataset.addValue(3D, series3, type2); defaultcategorydataset.addValue(2D, series3, type3); defaultcategorydataset.addValue(3D, series3, type4); defaultcategorydataset.addValue(6D, series3, type5); defaultcategorydataset.addValue(3D, series3, type6); defaultcategorydataset.addValue(4D, series3, type7); defaultcategorydataset.addValue(3D, series3, type8); JFreeChart jfreechart = ChartFactory.createLineChart("折线图 Demo 1", "Type","Value", defaultcategorydataset,PlotOrientation.VERTICAL, true,true,false); CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setRangeGridlinePaint(Color.white); ChartFrame frame=new ChartFrame ("折线图 ",jfreechart,true); frame.pack(); frame.setVisible(true); four:折线图 // //折线图2 XYSeries xyseries =new XYSeries("First");//先产生XYSeries对象 xyseries.add(1.0D, 1.0D); xyseries.add(2D, 4D); xyseries.add(3D, 3D); xyseries.add(4D, 5D); xyseries.add(5D, 5D); xyseries.add(6D, 7D); xyseries.add(7D, 7D); xyseries.add(8D, 8D); XYSeries xyseries1 =new XYSeries("Second"); xyseries1.add(1.0D, 5D); xyseries1.add(2D, 7D); xyseries1.add(3D, 6D); xyseries1.add(4D, 8D); xyseries1.add(5D, 4D); xyseries1.add(6D, 4D); xyseries1.add(7D, 2D); xyseries1.add(8D, 1.0D); XYSeries xyseries2 =new XYSeries("Third"); xyseries2.add(3D, 4D); xyseries2.add(4D, 3D); xyseries2.add(5D, 2D); xyseries2.add(6D, 3D); xyseries2.add(7D, 6D); xyseries2.add(8D, 3D); xyseries2.add(9D, 4D); xyseries2.add(10D, 3D); XYSeriesCollection xyseriescollection =new XYSeriesCollection();//再用XYSeriesCollection添加入XYSeries对象 xyseriescollection.addSeries(xyseries); xyseriescollection.addSeries(xyseries1); xyseriescollection.addSeries(xyseries2); JFreeChart jfreechart = ChartFactory.createXYLineChart("Line Chart Demo 2", "X", "Y", xyseriescollection, PlotOrientation.VERTICAL, true, true, false); ChartFrame frame=new ChartFrame ("折线图 ",jfreechart,true); frame.pack(); frame.setVisible(true); five:时间序列图 //时间序列图 TimeSeries timeseries =new TimeSeries("L&G European Index Trust",Month.class); timeseries.add(new Month(2, 2001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class等等 timeseries.add(new Month(3, 2001), 167.3D); timeseries.add(new Month(4, 2001), 153.8D); timeseries.add(new Month(5, 2001), 167.6D); timeseries.add(new Month(6, 2001), 158.8D); timeseries.add(new Month(7, 2001), 148.3D); timeseries.add(new Month(8, 2001), 153.9D); timeseries.add(new Month(9, 2001), 142.7D); timeseries.add(new Month(10, 2001), 123.2D); timeseries.add(new Month(11, 2001), 131.8D); timeseries.add(new Month(12, 2001), 139.6D); timeseries.add(new Month(1, 2002), 142.9D); timeseries.add(new Month(2, 2002), 138.7D); timeseries.add(new Month(3, 2002), 137.3D); timeseries.add(new Month(4, 2002), 143.9D); timeseries.add(new Month(5, 2002), 139.8D); timeseries.add(new Month(6, 2002), 137D); timeseries.add(new Month(7, 2002), 132.8D); TimeSeries timeseries1 =new TimeSeries("L&G UK Index Trust",Month.class); timeseries1.add(new Month(2, 2001), 129.6D); timeseries1.add(new Month(3, 2001), 123.2D); timeseries1.add(new Month(4, 2001), 117.2D); timeseries1.add(new Month(5, 2001), 124.1D); timeseries1.add(new Month(6, 2001), 122.6D); timeseries1.add(new Month(7, 2001), 119.2D); timeseries1.add(new Month(8, 2001), 116.5D); timeseries1.add(new Month(9, 2001), 112.7D); timeseries1.add(new Month(10, 2001), 101.5D); timeseries1.add(new Month(11, 2001), 106.1D); timeseries1.add(new Month(12, 2001), 110.3D); timeseries1.add(new Month(1, 2002), 111.7D); timeseries1.add(new Month(2, 2002), 111D); timeseries1.add(new Month(3, 2002), 109.6D); timeseries1.add(new Month(4, 2002), 113.2D); timeseries1.add(new Month(5, 2002), 111.6D); timeseries1.add(new Month(6, 2002), 108.8D); timeseries1.add(new Month(7, 2002), 101.6D); TimeSeriesCollection timeseriescollection =new TimeSeriesCollection(); timeseriescollection.addSeries(timeseries); timeseriescollection.addSeries(timeseries1); timeseriescollection.setDomainIsPointsInTime(true);//domain轴上的刻度点代表的是时间点而不是时间段 JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date", "Price Per Unit", timeseriescollection, true, true, false); jfreechart.setBackgroundPaint(Color.white); XYPlot xyplot = (XYPlot)jfreechart.getPlot();//获得 plot : XYPlot!! xyplot.setBackgroundPaint(Color.lightGray); xyplot.setDomainGridlinePaint(Color.white); xyplot.setRangeGridlinePaint(Color.white); xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); xyplot.setDomainCrosshairVisible(true); xyplot.setRangeCrosshairVisible(true); ChartFrame frame=new ChartFrame ("折线图 ",jfreechart,true); frame.pack(); frame.setVisible(true); |