jfreechart是一个免费开源的图表工具 先到www.jfree.org下载jfree的jar包(jfreechart-1.0.13.zip、jcommon-1.0.16.zip、jfreechart-1.0.13-javadocs.zip)。 1、拷贝相应目录下的两个库文件到项目的lib目录下 先看一个java文件测试例题:
packagecom.test.jfreechat; importjava.awt.Font; importorg.jfree.data.general.DefaultPieDataset; importorg.jfree.chart.*; importorg.jfree.chart.plot.PiePlot; importorg.jfree.chart.title.TextTitle; publicclassJfreechattest{ publicstaticvoidmain(String[]args){ //默认的饼图结果集 DefaultPieDatasetdpd=newDefaultPieDataset(); dpd.setValue("管理人员",10); dpd.setValue("开发人员",26); dpd.setValue("行政人事",10); dpd.setValue("编辑人员",18); dpd.setValue("拓展项目",8); dpd.setValue("设计部",6); JFreeChartjc=ChartFactory.createPieChart("国通集团",dpd,true,true,false); //解决标题乱码 TextTitlett=jc.getTitle(); tt.setFont(newFont("宋体",0,20)); //这句代码解决了底部汉字乱码的问题 jc.getLegend().setItemFont(newFont("宋体",Font.PLAIN,12)); //解决图形上的标识乱码 PiePlotpieplot=(PiePlot)jc.getPlot(); pieplot.setLabelFont(newFont("宋体",0,30)); //窗体上的标题显示 ChartFramecf=newChartFrame("国通",jc); cf.pack(); cf.setVisible(true); } }
2、如何将生成的图产生一个文件保存在硬盘上(基于javaapplication) /** *转换成图片,并保存在本地硬盘上.注意org.jfree.chat.ChartUtilities类尤为重要。 */ packagecom.test.jfreechat;
importjava.awt.Font; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.OutputStream;
importorg.jfree.chart.*; importorg.jfree.chart.plot.PiePlot; importorg.jfree.chart.title.LegendTitle; importorg.jfree.chart.title.TextTitle; importorg.jfree.data.general.DefaultPieDataset;
publicclassJfreechatTest2{ publicstaticvoidmain(String[]args){ DefaultPieDatasetdpd=newDefaultPieDataset(); dpd.setValue("设计部",8); dpd.setValue("编辑部",12); dpd.setValue("开发部",16); dpd.setValue("行政人事部",8); dpd.setValue("拓展部",6); dpd.setValue("管理层",10); JFreeChartjfc=ChartFactory.createPieChart("公司部门分布图",dpd,true,true,true); //JFreeChartjfc=ChartFactory.createRingChart("公司部门分布图",dpd,true,true,true); jfc.setTitle(newTextTitle("分布图")); PiePlotpieplot=(PiePlot)jfc.getPlot(); pieplot.setLabelFont(newFont("宋体",Font.ITALIC,16)); LegendTitlelt=jfc.getLegend(0); lt.setItemFont(newFont("黑体",Font.BOLD,13)); try{ OutputStreamos=newFileOutputStream("company.jpeg");
ChartUtilities.writeChartAsJPEG(os,jfc,800,600); os.close(); }catch(Exceptione){ e.printStackTrace(); } ChartFramecf=newChartFrame("部门分布图",jfc); cf.pack(); cf.setVisible(true); } }
3、如何在web页面中显示图片(比如jsp页面中显示图形) 首先需要在web.xml中配置一个servlet. <servlet> 有两个重要的类需要导入:org.jfree.chart.servlet.DisplayChart,org.jfree.chart.servlet.ServletUtilities
会在应用服务器的temp目录下产生临时图片文件。(比如tomcat)。 jfreechart.jsp: <%@pagelanguage="java"contentType="text/html;charset=GB18030" pageEncoding="GB18030"%> <%@pageimport="org.jfree.chart.*,org.jfree.data.general.*,org.jfree.chart.servlet.*,org.jfree.chart.title.*,org.jfree.chart.plot.*,java.awt.*"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www./TR/html4/loose.dtd"> <html> <head> <metahttp-equiv="Content-Type"content="text/html;charset=GB18030"> <title>Inserttitlehere</title> </head> <body> <% DefaultPieDatasetdpd=newDefaultPieDataset(); dpd.setValue("设计部",8); dpd.setValue("编辑部",12); dpd.setValue("开发部",16); dpd.setValue("行政人事部",8); dpd.setValue("拓展部",6); dpd.setValue("管理层",10); JFreeChartjfc=ChartFactory.createPieChart("组织架构",dpd,true,true,false); jfc.setTitle(newTextTitle("分布图")); PiePlotpieplot=(PiePlot)jfc.getPlot(); pieplot.setLabelFont(newFont("宋体",Font.ITALIC,16)); LegendTitlelt=jfc.getLegend(0); lt.setItemFont(newFont("黑体",Font.BOLD,13)); StringfileName=ServletUtilities.saveChartAsJPEG(jfc,800,600,session); <%--下面一句的参数filename不能更改别的名字,否则会报错这是由DisplayChart类内部决定的--%> Stringurl=request.getContextPath()+"/DisplayChart?filename="+fileName; %> <imgsrc="<%=url%>"width="800"height="600"> </body> </html>
4、struts2框架如何整合jfreeChart? *将struts2源码包lib目录下的struts2-jfreechart-plugin-2.0.14.jar dataset.setValue((Integer)map.get("football"),"","足球"); struts2与JFreeChart插件(struts2-jfreechart-plugin-2.0.14.jar)
|
|