使用JFreeChart创建图象

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 作者:孤魂一笑(bingo_ge@hotmail.com) 日期:2003-05-08 一:jfreechart介绍 jfreechart是一个免费创建图片的java工具.可以创建如下图形: 饼图(pie charts;) 曲线图(line charts ) 柱状图(horizontal/vertical bar charts) 甘特图(Gantt charts; ) XY plots and scatter plots; time series, high/low/open/close charts and candle stick charts; combination charts; Pareto charts; bubble charts; wind plots, meter charts and symbol charts; 从以下地址可以看到jfreechart可以创建的图形类型 http://www.jfree.org/jfreechart/samples.html sourceforge有一个基于jfreechart的项目Cewolf可以很方便的在jsp/servlet中创建图片 jfreechart目前(2003-05-08)版本为0.98 希望得到详细的信息或下载jfreechart请访问如下站点: http://www.jfree.org/jfreechart/ 二:特别说明: jfreechart是一个开源项目,但是文档是需要40美金去购买的。 还有一个很重要的问题,jfreechart如果使用中文,他使用的默认字体 显示出来的中文会很模糊,你可能需要修改源代码。 下面我就举几个简单的例子说明一下如何使用jfreechart创建图片 在开发中有可能会导入以下的类 import com.jrefinery.chart.ChartFactory; import com.jrefinery.chart.ChartUtilities; import com.jrefinery.chart.JFreeChart; import com.jrefinery.chart.TextTitle; import com.jrefinery.chart.axis.NumberAxis; import com.jrefinery.chart.plot.CategoryPlot; import com.jrefinery.chart.plot.PiePlot; import com.jrefinery.data.Day; import com.jrefinery.data.DefaultCategoryDataset; import com.jrefinery.data.DefaultPieDataset; import com.jrefinery.data.TimeSeries; import com.jrefinery.data.TimeSeriesCollection; import com.jrefinery.data.TimeSeriesDataPair; 在0.98以后包由com.jrefinery.*改变为:org.jfree 三:创建饼图 //图片标题 String title = "空调2002年市场占有率"; //设定数据源 DefaultPieDataset piedata = new DefaultPieDataset(); //第一个参数为名称,第二个参数是double数 piedata.setValue("联想", 27.3); piedata.setValue("长城", 12.2); piedata.setValue("海尔", 5.5); piedata.setValue("美的", 17.1); piedata.setValue("松下", 9.0); piedata.setValue("科龙", 19.0); //创建JFreeChart,都使用ChartFactory来创建JFreeChart,很标准的工厂设计模式 JFreeChart chart = ChartFactory.createPieChart(title, piedata, true, true, true); //设定图片标题 chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15))); //chart.addSubtitle(new TextTitle("2002财年分析", new Font("隶书", Font.ITALIC, 12))); //设定背景 chart.setBackgroundPaint(Color.white); //chart.s //饼图使用一个PiePlot PiePlot pie = (PiePlot)chart.getPlot(); //pie.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS); pie.setSectionLabelType(PiePlot.NAME_AND_VALUE_LABELS); //设定显示格式(名称加百分比或数值) pie.setPercentFormatString("#,###0.0#%"); //设定百分比显示格式 pie.setBackgroundPaint(Color.white); pie.setSectionLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12)); //设定背景透明度(0-1.0之间) pie.setBackgroundAlpha(0.6f); //设定前景透明度(0-1.0之间) pie.setForegroundAlpha(0.90f); //输出文件到指定目录 String rfname = MathUtil.getRoundCode(12) ".jpeg"; String fileName = "d:/test/" rfname; try { //可以保存文件为jpg或png格式。 ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600); //第一个参数为文件名 //第二个参数质量 //第三个参数为哪个chart创建图片 //第四个宽度 //第五个高度 } catch (IOException exz) { System.out.print("....Cant't Create image File"); } 其实使用JFreeChart创建图片很简单,不同的的图片类型区别在于设置数据集 四:创建曲线图 // create a default chart based on some sample data... //曲线图标题 String title = "趋势分析"; //曲线图X轴提示 String domain = "月份走势"; //曲线图Y轴提示 String range = "应收余额"; //曲线图自标题 String subtitleStr = "2003财年分析"; //创建时间数据源 //每一个TimeSeries在图上是一条曲线 TimeSeries ca = new TimeSeries("用友"); for (int i = 1999; i < 2005; i ) { for (int mon = 0; mon < 12; mon ) { //ca.add(new Month(mon 1, i), new Double(500 Math.random() * 100)); //TimeSeriesDataPair是一个时间点的数值体现 ca.add( new TimeSeriesDataPair( new Day(1, mon 1, i), new Double(500 Math.random() * 100))); } } TimeSeries ibm = new TimeSeries("金碟"); for (int i = 1999; i < 2005; i ) { for (int mon = 0; mon < 12; mon ) { //ibm.add(new Month(mon 1,i),new Double(400-Math.random()*100)); ibm.add( new TimeSeriesDataPair( new Day(1, mon 1, i), new Double(400 - Math.random() * 100))); } } TimeSeries king = new TimeSeries("东软"); for (int i = 1999; i < 2005; i ) { for (int mon = 0; mon < 12; mon ) { //ibm.add(new Month(mon 1,i),new Double(400-Math.random()*100)); king.add( new TimeSeriesDataPair( new Day(1, mon 1, i), new Double(300 - Math.random() * 100))); } } //时间曲线数据集合 TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(ca); dataset.addSeries(ibm); dataset.addSeries(king); //dataset.addSeries(jpy); //dataset.addSeries(mav); //时间曲线元素 JFreeChart chart = ChartFactory.createTimeSeriesChart( title, domain, range, dataset, true, true, false); // then customise it a little... TextTitle subtitle = new TextTitle(subtitleStr, new Font("黑体", Font.BOLD, 12)); chart.addSubtitle(subtitle); chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15))); //pie.setSeriesLabelFont(new Font("黑体", Font.BOLD, 15)); chart.setBackgroundPaint( new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue)); //sysout //输出文件到指定目录 String rfname = MathUtil.getRoundCode(22) ".jpeg"; String fileName = "d:/test/" rfname; try { //for //System.out.println(); ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600); // log.info("....Create image File:" fileName); } catch (IOException exz) { System.out.print("....Cant't Create image File"); } 五:创建柱状图 String title = "柱状图测试"; String domain = "单位比较"; String range = "数值"; //CategoryDataset data = DemoDatasetFactory.createCategoryDataset(); DefaultCategoryDataset data = new DefaultCategoryDataset(); for (int r = 0; r < 5; r ) { String rowKey = "单位 [" (r 1) "]" ; //第一层循环:分析对象 for (int c = 0; c < 6; c ) { //第二层循环:分析对象在时间点上的数据 String columnKey = "2001年" (c 1) "月"; data.addValue(new Double(r * c 5), rowKey, columnKey); } } JFreeChart chart = ChartFactory.createVerticalBarChart( title, domain, range, data, true, true, false); // then customise it a little... chart.setBackgroundPaint( new GradientPaint(0, 0, Color.white, 1000, 0, Color.red)); chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15))); CategoryPlot plot = (CategoryPlot)chart.getPlot(); plot.setForegroundAlpha(0.9f); plot.setValueLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12)); //plot.setSectionLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12)); //注意以下代码 NumberAxis verticalAxis = (NumberAxis)plot.getRangeAxis(); verticalAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // 输出文件到指定目录 String rfname = MathUtil.getRoundCode(22) "b.jpeg"; String fileName = "d:/test/" rfname; try { ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600); // log.info("....Create image File:" fileName); } catch (IOException exz) { System.out.print("....Cant't Create image File"); } 六:结束语 个人感觉JFreeChart可以满足大部分图片创建的需要,美中不足的是:对字体的设置做的 不够好,特别是使用中文的时候字体很不清晰。因为这个原因建议你自己去修改他的源代码 最好使用properties文件去设置字体.还有就是文档要钱所以要多花点时间去看源代码。或多 上社区.因为时间等原因我只介绍了三种图片的创建,其他类型的图片可以参考jfreechart提 供的例子。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值