jfreechart 结合 struts2

action

 1 package kite.struts2.action;
 2 
 3 import java.awt.Font;
 4 import java.io.ByteArrayInputStream;
 5 import java.io.ByteArrayOutputStream;
 6 import java.io.InputStream;
 7 
 8 import javax.annotation.Resource;
 9 
10 import org.jfree.chart.ChartFactory;
11 import org.jfree.chart.ChartUtilities;
12 import org.jfree.chart.JFreeChart;
13 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
14 import org.jfree.chart.plot.PiePlot;
15 import org.jfree.data.general.DefaultPieDataset;
16 import org.springframework.context.annotation.Scope;
17 import org.springframework.stereotype.Controller;
18 
19 import kite.domain.Question;
20 import kite.domain.statistics.OptionStatisticsModel;
21 import kite.domain.statistics.QuestionStatisticsModel;
22 import kite.service.StatisticsService;
23 
24 @Controller("chartOutputAction")
25 @Scope("prototype")
26 public class ChartOutputAction extends BaseAction<Question>
27 {
28     @Resource(name="statisticsService")
29     StatisticsService statisticsService;
30     private Integer qid;
31     
32     private Integer chartType;
33     public String execute() throws Exception
34     {
35         return SUCCESS;
36     }
37     
38     public InputStream getInputStream()
39     {
40         try
41         {
42             QuestionStatisticsModel qsm = statisticsService.statistics(qid);
43             DefaultPieDataset ds = new DefaultPieDataset();
44             for(OptionStatisticsModel osm : qsm.getOsms())
45             {
46                 ds.setValue(osm.getOptionLabel(), osm.getCount());
47             }
48             JFreeChart chart = ChartFactory.createPieChart(qsm.getQuestion().getTitle(), ds, true, false, false);
49             
50             //设置标题和提示条中文
51             chart.getTitle().setFont(new Font("宋体",Font.BOLD, 25));
52             chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 20));
53             
54             PiePlot plot = (PiePlot) chart.getPlot();
55             plot.setLabelFont(new Font("宋体", Font.PLAIN, 15));
56             plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}(选择人数:{1}总数:{3} 占百分比:{2})"));
57             
58             ByteArrayOutputStream baos = new ByteArrayOutputStream();
59             ChartUtilities.writeChartAsJPEG(baos, chart, 800, 600);
60             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
61             return bais;
62         } catch (Exception e)
63         {
64             e.printStackTrace();
65         }
66         return null;
67         
68     }
69 
70     public Integer getQid()
71     {
72         return qid;
73     }
74 
75     public void setQid(Integer qid)
76     {
77         this.qid = qid;
78     }
79 
80     public Integer getChartType()
81     {
82         return chartType;
83     }
84 
85     public void setChartType(Integer chartType)
86     {
87         this.chartType = chartType;
88     }
89 }

struts.xml 文件配置
1 <action name="chartOutputAction"   class="chartOutputAction">
2             <result name="success" type="stream">
3                 <param name="contentType">image/jpeg</param>
4                 <param name="inputName">inputStream</param>//getInputStream inputStream 
5                 <param name="bufferSize">1024</param>   
6             </result>
7 </action>



改进 使用 struts2的 jfreechart插件 ChartResult

修改action 文件
 1 public JFreeChart getChart()
 2     {
 3         try
 4         {
 5             QuestionStatisticsModel qsm = statisticsService.statistics(qid);
 6             DefaultPieDataset ds = new DefaultPieDataset();
 7             for(OptionStatisticsModel osm : qsm.getOsms())
 8             {
 9                 ds.setValue(osm.getOptionLabel(), osm.getCount());
10             }
11             JFreeChart chart = ChartFactory.createPieChart(qsm.getQuestion().getTitle(), ds, true, false, false);
12             
13             //设置标题和提示条中文
14             chart.getTitle().setFont(new Font("宋体",Font.BOLD, 25));
15             chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 20));
16             
17             PiePlot plot = (PiePlot) chart.getPlot();
18             plot.setLabelFont(new Font("宋体", Font.PLAIN, 15));
19             plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}(选择人数:{1}总数:{3} 占百分比:{2})"));
20             
21             return chart;
22         } catch (Exception e)
23         {
24             e.printStackTrace();
25         }
26         return null;
27         
28     }

 

修改xml配置文件   需要继承 jfreechart-default

<action name="chartOutputAction" class="chartOutputAction">
  <result name="success" type="chart">
    <param name="height">600</param>
    <param name="width">800</param>
    <param name="bufferSize">1024</param>
  </result>
</action>

 

 

 

posted @ 2014-05-13 21:59  放手风筝  阅读(240)  评论(0编辑  收藏  举报