天津市处理违章地点:struts2+hibernate整合jfreechart

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 20:02:53

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.


DisplayChart
org.jfree.chart.servlet.DisplayChart


DisplayChart
/DisplayChart

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

Inserttitlehere

    <%

    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;

    %>

        "width="800"height="600">

 

4、struts2框架如何整合jfreeChart?

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

2.Action

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

privateListinterest;//接受页面传递过来的所有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;
}

publicListgetInterest()
{
returninterest;
}

publicvoidsetInterest(Listinterest)
{
this.interest=interest;
}

@Override
publicStringexecute()throwsException
{
returnSUCCESS;
}

/**
*处理list,其结果重新放到ActionContext中
*如果足球被选中,将其票数加一,修改后的数据仍然在ActionContext里,相当于修改了静态的变量
*@paramlist
*/
@SuppressWarnings("unchecked")
privatevoidincreaseResult(Listlist)
{
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,仅仅用标签就可以渲染出这个图标
而不需要定义任何的显示页面


600
800



*需要修正一个小的bug

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


看这个-->
600
800



这个标签是在struts2中没有定义,是在JFreeChart的jar包中的配置
文件里定义的:
解压这个jar包,在struts-plugin.xml里如下定义:




150
200




如果要使用这个标签,那么可以使我们的struts.xml继承这个package

可是正常情况下我们他是继承自:extends="struts-default"
如此一来,他就失去了struts2的所有功能,又不可以多继承,
所以解决办法是:让struts.xml继承struts-plugin.xml,再使struts-plugin.xml
继承struts-default,如此我们继承成了两者的所有功能,可以使用
标签,也获得了struts2的所有功能
如下:
1.解压struts2-jfreechart-plugin-2.0.14,它在struts2的源码包的lib下
找到struts-plugin.xml,修改此文件,添加: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使用标签了,struts.xml由此就集成了struts-plugin.xml,和truts-defaul.xml的功能