`

引用 JFreeChart在struts2中实现饼状图

阅读更多

原文地址:http://dai1889.blog.163.com/blog/static/53161889201048101019420/

 

candy 的 JFreeChart在struts2中实现饼状图

使用的是JFreeChart在struts2中的插件

1.将jfreechart的jar包放到项目中的lib文件夹下,servlet.jar和gnujaxp.jar不必放,第一个没有什么用处,因为tomcat中已有,第二个如果放进去了发布的时候可能会出现xml解析之类的错误,原因是由于你的SSH项目中可能已经有解析xml的jar文件了,产生冲突,这时首先:右击项目-->从configure build path中将其移除,再从lib下删除重新发布即可。

2.struts2与jfreechart的整合还需要struts2-jfreechart-plugin-2.1.8.jar,尽量下新版本的,这样查看其中的struts-plugin.xml,有extends="struts-default"。

3.假如出现找不到xwork下的LoggerFactory的异常,去下载一个高版本的xwork的jar文件就OK了。

好了,可以在struts2中制作图形报表了。

PieChartAction.java:

public class PieChartAction extends ActionSupport {
 
 private static final long serialVersionUID = 5752180822913527064L;
 private JFreeChart chart;
  
 public String execute(){
   
       //生成JFreeChart对象    
       chart = ChartFactory.createPieChart3D( 
             "饼状图",  // 图表标题 
              getDataSet(), //数据 
              true, // 是否显示图例 
              false, //是否显示工具提示 
              false //是否生成URL 
           ); 
       //重新设置图标标题,改变字体 
       chart.setTitle(new TextTitle("饼状图", new Font("黑体", Font.ITALIC , 22))); 
       
       //取得统计图标的第一个图例 
       LegendTitle legend = chart.getLegend(0); 
       //修改图例的字体 
       legend.setItemFont(new Font("宋体", Font.BOLD, 14)); 
       //获得饼图的Plot对象 
       PiePlot plot = (PiePlot)chart.getPlot(); 
       //设置饼图各部分的标签字体 
       plot.setLabelFont(new Font("隶书", Font.BOLD, 10)); 
       //设定背景透明度(0-1.0之间) 
       plot.setBackgroundAlpha(0.9f); 
       //设定前景透明度(0-1.0之间) 
       plot.setForegroundAlpha(0.50f); 
       
       // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
       plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}({2})", NumberFormat.getNumberInstance(),
               new DecimalFormat("0.00%")));
       // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
       
       plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
       
       return SUCCESS;
 
 }
 
 public DefaultPieDataset getDataSet(){
   DefaultPieDataset data = new DefaultPieDataset();
      data.setValue("Java", new Double(43.2));
      data.setValue("Visual Basic", new Double(1.0));
      data.setValue("C/C++", new Double(17.5));
      data.setValue("tangjun书", new Double(60.0));
      return data;
 }
 
 public JFreeChart getChart() {
  return chart;
 }


}

当然了,可以直接将execute方法去掉,其中的代码挪到getChart中,功能相同,访问时不用指定方法名。

 

chart-struts.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "
http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>
 <package name="jCuckoo" namespace="/jCuckoo"
  extends="jfreechart-default">
 
  <action name="pieChartAction" class="jCuckoo.lee.PieChartAction">
   <result type="chart">
    <param name="width">600</param>
    <param name="height">450</param>
   </result>
  </action>


 </package>
</struts>

 

struts.xml中加入:

<include file="/jCuckoo/chart-struts.xml"></include>

 

好了,现在就可以通过http://localhost:8080/项目名/jCuckoo/pieChartAction.action进行访问了

如果想对生成的图进行更多的操作,可以通过jsp页面进行访问,new一个index.jsp

加入:<img src="<%=path %>/jCuckoo/pieChartAction.action">

然后就可以通过访问index页面看到饼状图了。

 JFreeChart在struts2中实现饼状图 - candy - 好记忆不如烂笔头


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics