分享

struts2+hibernate整合jfreechart

 英昌知识汇聚馆 2011-11-21

jfreechart是一个免费开源的图表工具

先到www.jfree.org下载jfreejar(jfreechart-1.0.13.zipjcommon-1.0.16.zipjfreechart-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>
<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>

有两个重要的类需要导入: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>

 

4struts2框架如何整合jfreeChart?

*struts2源码包lib目录下的struts2-jfreechart-plugin-2.0.14.jar
添加到项目的lib库中即完成了整合
--
统计投票的例子:
1.jsp:
<s:checkboxname="interest"label="
足球"
fieldValue="football"
设置他在HTML中的value,必须用fieldValue,不是
value
,这是struts2标签库的特性
labelposition="left">
</s:checkbox>
2.Action

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

privateList<String>interest;//
接受页面传递过来的所有name="interest"value

//
得到JFreeChart对象的实例
publicJFreeChartgetChart()
{
chart=ChartFactory.createBarChart3D("
兴趣统计结果","项目","结果",this
.getDataset(),PlotOrientation.VERTICAL,false,false,false);
//
标题,横轴标题,纵轴标题,竖直的还是水平的,图例,提示,URL
chart.setTitle(newTextTitle("
兴趣统计结果",newFont("黑体",Font.BOLD,22)));


CategoryPlotplot=(CategoryPlot)chart.getPlot();


//
设置水平轴各个详细属性
CategoryAxiscategoryAxis=plot.getDomainAxis();
categoryAxis.setLabelFont(newFont("
宋体",Font.BOLD,22));
//
设置文字倾斜度
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);


//
设置纵轴各个详细属性
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,其结果重新放到ActionContext
*
如果足球被选中,将其票数加一,修改后的数据仍然在ActionContext里,相当于修改了静态的变量
*@paramlist
*/
@SuppressWarnings("unchecked")
privatevoidincreaseResult(List<String>list)
{
ActionContextcontext=ActionContext.getContext();
//map
key="足球"value=票数
Mapmap=context.getApplication();
/*
*
比方说list中,有“足球”、“篮球”、“排球”
*
则我们遍历list,拿出来“足球”,自定义票数,以“足球”和“票数”给ActionContext
*
中的Map赋值
*/

for(Stringstr:list)
{
if(null==map.get(str))//
表明此项目(例如足球)还没投过票,这一次是第一次投票
{
map.put(str,1);//
记足球为一票
}
else
{
map.put(str,(Integer)map.get(str)+1);//
在原来的基础上加一票
}
}
}
/**
*
生成柱状图的数据集
*@return
*/
@SuppressWarnings("unchecked")
privateCategoryDatasetgetDataset()
{
DefaultCategoryDatasetdataset=newDefaultCategoryDataset();
this.increaseResult(this.getInterest());

//
ActionContext中取出数据
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

在这里我们不用跳转到jsp里显示chart,仅仅用<result>标签就可以渲染出这个图标
而不需要定义任何的显示页面
<actionname="viewResult"class="com.test.action.ViewResultAction">
<resultname="success"type="chart">
<paramname="height">600</param>
<paramname="width">800</param>
</result>
</action>

*
需要修正一个小的bug

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

<actionname="viewResult"class="com.test.action.ViewResultAction">
看这个<result>--><resultname="success"type="chart">
<paramname="height">600</param>
<paramname="width">800</param>
</result>
</action>

这个<result>标签是在struts2中没有定义,是在JFreeChartjar包中的配置
文件里定义的:
解压这个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>
如果要使用这个<result>标签,那么可以使我们的struts.xml继承这个package
<packagename="struts2"extends="jfreechart-default">
可是正常情况下我们他是继承自:extends="struts-default"
如此一来,他就失去了struts2的所有功能,又不可以多继承,
所以解决办法是:让struts.xml继承struts-plugin.xml,再使struts-plugin.xml
继承struts-default,如此我们继承成了两者的所有功能,可以使用
<result>
标签,也获得了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.
在解压后的目录下,一定要在这个目录下,将修改后的文件重新打包成struts2-jfreechart-plugin-2.0.14.jar
jar –cvf struts2-jfreechart-plugin-2.0.14.jar –C *
3.
在项目中引入这个修改的jar
4.
修改struts.xml文件,使其继承jfreechart-default
--
完成,至此可以在struts.xml使用<result>标签了,struts.xml由此就集成了struts-plugin.xml,和truts-defaul.xml的功能

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约