Struts2.1.8整合jfreechart

需要的jar

jfreechart的jar

Struts的jar

Struts整合jfreechart的插件包 struts2-jfreechart-plugin-2.1.8.1.jar

1.struts.xml配置

<package name="char" namespace="/char" extends="jfreechart-default">
     <action name="*-*" class="com.wepull.bookSys.action.{1}Action" method="{2}">
           <result name = "success" type = "chart" >    
                <param name = "width" > 600 </param >    
                <param name = "height" > 400 </param >    
            </result >    

    </action>
</package>

这个配置查看struts2-jfreechart-plugin-2.1.8.1.jar包中的struts-plugin.xml 文件(如下)

!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    
<struts>
    <package name="jfreechart-default" extends="struts-default">
    
     <result-types>
      <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">
       <param name="height">150</param>
       <param name="width">200</param>
      </result-type>
     </result-types>
    </package>

</struts>

 

2. action配置

public class CharAction extends ActionSupport {
    private  JFreeChart chart;  //名字必须为char 
    
    
 public String showChart() {
  chart = CreateChart.exportPiechart();
  return SUCCESS;
 }

 public  JFreeChart getChart() {  //必须提供get方法 
          return  chart;   
     }
 
}

  

3.CreateChart 类

public class CreateChart {
 public static JFreeChart exportPiechart() {
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue("经济舱总售票" + 105, 105);
  dataset.setValue("商务舱总售票" + 30, 30);
  dataset.setValue("头等舱总售票" + 50, 50);
  JFreeChart chart = ChartFactory.createPieChart3D("客户统计图", dataset,
    true, true, true);
  chart.setTitle(new TextTitle("售票统计图", new Font("黑体", Font.ITALIC, 14)));// 标题字体
  // 设置图例部分
  LegendTitle legend = chart.getLegend(0);
  // 设置图的部分
  PiePlot plot = (PiePlot) chart.getPlot();
  plot.setLabelFont(new Font("宋体", Font.BOLD, 12));// 设置实际统计图的字体
  plot.setBackgroundAlpha(0.9f);
  plot.setForegroundAlpha(0.50f);
  return chart;
 }

猜你喜欢

转载自wlt2008-com.iteye.com/blog/1042581