久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

struts2+hibernate整合jfreechart

 英昌知識(shí)匯聚館 2011-11-21

jfreechart是一個(gè)免費(fèi)開(kāi)源的圖表工具

先到www.jfree.org下載jfreejar(jfreechart-1.0.13.zipjcommon-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>
<servlet-name>DisplayChart</srvlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</srvlet-class>
</srvlet>
<servlet-mapping>
<servlet-name>DisplayChart</srvlet-name>
<url-pattern>/DisplayChart</url-pattern>
</srvlet-mapping>

有兩個(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
添加到項(xiàng)目的lib庫(kù)中即完成了整合
--
統(tǒng)計(jì)投票的例子:
1.jsp:
<s:checkboxname="interest"label="
足球"
fieldValue="football"
設(shè)置他在HTML中的value,,必須用fieldValue,不是
value
,,這是struts2標(biāo)簽庫(kù)的特性
labelposition="left">
</s:checkbox>
2.Action

publicclassViewResultActionextendsActionSupport
{
privateJFreeChartchart;//
私有的chart屬性

privateList<String>interest;//
接受頁(yè)面?zhèn)鬟f過(guò)來(lái)的所有name="interest"value

//
得到JFreeChart對(duì)象的實(shí)例
publicJFreeChartgetChart()
{
chart=ChartFactory.createBarChart3D("
興趣統(tǒng)計(jì)結(jié)果","項(xiàng)目","結(jié)果",this
.getDataset(),PlotOrientation.VERTICAL,false,false,false);
//
標(biāo)題,,橫軸標(biāo)題,縱軸標(biāo)題,,豎直的還是水平的,,圖例,提示,,URL
chart.setTitle(newTextTitle("
興趣統(tǒng)計(jì)結(jié)果",newFont("黑體",Font.BOLD,22)));


CategoryPlotplot=(CategoryPlot)chart.getPlot();


//
設(shè)置水平軸各個(gè)詳細(xì)屬性
CategoryAxiscategoryAxis=plot.getDomainAxis();
categoryAxis.setLabelFont(newFont("
宋體",Font.BOLD,22));
//
設(shè)置文字傾斜度
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);


//
設(shè)置縱軸各個(gè)詳細(xì)屬性
NumberAxisnumberAxis=(NumberAxis)plot.getRangeAxis();
numberAxis.setLabelFont(newFont("
宋體",Font.BOLD,22));

returnchart;
}

publicList<String>getInterest()
{
returninterest;
}

publicvoidsetInterest(List<String>interest)
{
this.interest=interest;
}

@Override
publicStringexecute()throwsException
{
returnSUCCESS;
}

/**
*
處理list,,其結(jié)果重新放到ActionContext
*
如果足球被選中,將其票數(shù)加一,,修改后的數(shù)據(jù)仍然在ActionContext里,,相當(dāng)于修改了靜態(tài)的變量
*@paramlist
*/
@SuppressWarnings("unchecked")
privatevoidincreaseResult(List<String>list)
{
ActionContextcontext=ActionContext.getContext();
//map
key="足球"value=票數(shù)
Mapmap=context.getApplication();
/*
*
比方說(shuō)list中,,有“足球”、“籃球”,、“排球”
*
則我們遍歷list,,拿出來(lái)“足球”,,自定義票數(shù),以“足球”和“票數(shù)”給ActionContext
*
中的Map賦值
*/

for(Stringstr:list)
{
if(null==map.get(str))//
表明此項(xiàng)目(例如足球)還沒(méi)投過(guò)票,,這一次是第一次投票
{
map.put(str,1);//
記足球?yàn)橐黄?span lang="EN-US">
}
else
{
map.put(str,(Integer)map.get(str)+1);//
在原來(lái)的基礎(chǔ)上加一票
}
}
}
/**
*
生成柱狀圖的數(shù)據(jù)集
*@return
*/
@SuppressWarnings("unchecked")
privateCategoryDatasetgetDataset()
{
DefaultCategoryDatasetdataset=newDefaultCategoryDataset();
this.increaseResult(this.getInterest());

//
ActionContext中取出數(shù)據(jù)
ActionContextcontext=ActionContext.getContext();
Mapmap=context.getApplication();

dataset.setValue((Integer)map.get("football"),"","足球");
dataset.setValue((Integer)map.get("basketball"),"","
籃球");
dataset.setValue((Integer)map.get("volleyball"),"","
排球");
dataset.setValue((Integer)map.get("badminton"),"","
羽毛球");

returndataset;
}

}
3.struts.xml

在這里我們不用跳轉(zhuǎn)到jsp里顯示chart,,僅僅用<result>標(biāo)簽就可以渲染出這個(gè)圖標(biāo)
而不需要定義任何的顯示頁(yè)面
<actionname="viewResult"class="com.test.action.ViewResultAction">
<resultname="success"type="chart">
<paramname="height">600</param>
<paramname="width">800</param>
</result>
</action>

*
需要修正一個(gè)小的bug

struts2JFreeChart插件(struts2-jfreechart-plugin-2.0.14.jar)
整合時(shí)需要在struts.xml中配置:

<actionname="viewResult"class="com.test.action.ViewResultAction">
看這個(gè)<result>--><resultname="success"type="chart">
<paramname="height">600</param>
<paramname="width">800</param>
</result>
</action>

這個(gè)<result>標(biāo)簽是在struts2中沒(méi)有定義,是在JFreeChartjar包中的配置
文件里定義的:
解壓這個(gè)jar包,在struts-plugin.xml里如下定義:
<struts>
<packagename="jfreechart-default">
<result-types>
<result-type  name="chart"class="org.apache.struts2.dispatcher.ChartResult">
<paramname="height">150</param>
<paramname="width">200</param>
</result-type>
</result-types>
</package>
</struts>
如果要使用這個(gè)<result>標(biāo)簽,,那么可以使我們的struts.xml繼承這個(gè)package
<packagename="struts2"extends="jfreechart-default">
可是正常情況下我們他是繼承自:extends="struts-default"
如此一來(lái),,他就失去了struts2的所有功能,,又不可以多繼承,,
所以解決辦法是:讓struts.xml繼承struts-plugin.xml,再使struts-plugin.xml
繼承struts-default,,如此我們繼承成了兩者的所有功能,,可以使用
<result>
標(biāo)簽,,也獲得了struts2的所有功能
如下:
1.
解壓struts2-jfreechart-plugin-2.0.14,它在struts2的源碼包的lib
找到struts-plugin.xml,,修改此文件,添加:extends="struts-default"
即:修改<packagename="jfreechart-default">
為:<packagename="jfreechart-default" extends="struts-default">
2.
在解壓后的目錄下,一定要在這個(gè)目錄下,將修改后的文件重新打包成struts2-jfreechart-plugin-2.0.14.jar
jar –cvf struts2-jfreechart-plugin-2.0.14.jar –C *
3.
在項(xiàng)目中引入這個(gè)修改的jar
4.
修改struts.xml文件,使其繼承jfreechart-default
--
完成,,至此可以在struts.xml使用<result>標(biāo)簽了,struts.xml由此就集成了struts-plugin.xml,和truts-defaul.xml的功能

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約