· 一: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./jfreechart/samples.html sourceforge有一个基于jfreechart的项目Cewolf可以很方便的在jsp/servlet中创建图片 jfreechart目前(2003-05-08)版本为0.98 希望得到详细的信息或下载jfreechart请访问如下站点: http://www./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.99vALIC, 15))); //chart.addSubtitle(new TextTitle("2002财年分析", new Font("隶书", Font.99vALIC, 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创建图片很简单,不同的的图片类型区别在于设置数据集。 |
|
来自: jiashengfan > 《open source》