jfreechart是一個(gè)免費(fèi)開(kāi)源的圖表工具 先到www.jfree.org下載jfree的jar包(jfreechart-1.0.13.zip、jcommon-1.0.16.zip,、jfreechart-1.0.13-javadocs.zip),。 1、拷貝相應(yīng)目錄下的兩個(gè)庫(kù)文件到項(xiàng)目的lib目錄下 先看一個(gè)java文件測(cè)試?yán)}:
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){ //默認(rèn)的餅圖結(jié)果集 DefaultPieDatasetdpd=newDefaultPieDataset(); dpd.setValue("管理人員",10); dpd.setValue("開(kāi)發(fā)人員",26); dpd.setValue("行政人事",10); dpd.setValue("編輯人員",18); dpd.setValue("拓展項(xiàng)目",8); dpd.setValue("設(shè)計(jì)部",6); JFreeChartjc=ChartFactory.createPieChart("國(guó)通集團(tuán)",dpd,true,true,false); //解決標(biāo)題亂碼 TextTitlett=jc.getTitle(); tt.setFont(newFont("宋體",0,20)); //這句代碼解決了底部漢字亂碼的問(wèn)題 jc.getLegend().setItemFont(newFont("宋體",Font.PLAIN,12)); //解決圖形上的標(biāo)識(shí)亂碼 PiePlotpieplot=(PiePlot)jc.getPlot(); pieplot.setLabelFont(newFont("宋體",0,30)); //窗體上的標(biāo)題顯示 ChartFramecf=newChartFrame("國(guó)通",jc); cf.pack(); cf.setVisible(true); } }
2,、如何將生成的圖產(chǎn)生一個(gè)文件保存在硬盤(pán)上(基于javaapplication) /** *轉(zhuǎn)換成圖片,,并保存在本地硬盤(pán)上.注意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("設(shè)計(jì)部",8); dpd.setValue("編輯部",12); dpd.setValue("開(kāi)發(fā)部",16); dpd.setValue("行政人事部",8); dpd.setValue("拓展部",6); dpd.setValue("管理層",10); JFreeChartjfc=ChartFactory.createPieChart("公司部門(mén)分布圖",dpd,true,true,true); //JFreeChartjfc=ChartFactory.createRingChart("公司部門(mén)分布圖",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("部門(mén)分布圖",jfc); cf.pack(); cf.setVisible(true); } }
3,、如何在web頁(yè)面中顯示圖片(比如jsp頁(yè)面中顯示圖形) 首先需要在web.xml中配置一個(gè)servlet. <servlet> 有兩個(gè)重要的類需要導(dǎo)入:org.jfree.chart.servlet.DisplayChart,org.jfree.chart.servlet.ServletUtilities
會(huì)在應(yīng)用服務(wù)器的temp目錄下產(chǎn)生臨時(shí)圖片文件,。(比如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("設(shè)計(jì)部",8); dpd.setValue("編輯部",12); dpd.setValue("開(kāi)發(fā)部",16); dpd.setValue("行政人事部",8); dpd.setValue("拓展部",6); dpd.setValue("管理層",10); JFreeChartjfc=ChartFactory.createPieChart("組織架構(gòu)",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); <%--下面一句的參數(shù)filename不能更改別的名字,否則會(huì)報(bào)錯(cuò)這是由DisplayChart類內(nèi)部決定的--%> 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)
|
|
來(lái)自: 英昌知識(shí)匯聚館 > 《struts》