zoukankan      html  css  js  c++  java
  • 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>

     
  • 相关阅读:
    vue项目搭建过程2 -- 使用 vue cli 4.0 搭建 vue 项目
    vue项目搭建过程1 -- 环境搭建
    升级node.js版本
    git的初步了解
    期末总结
    四则运算的封装
    用户故事
    0~10的随机整数运算
    创业近一年在博客园总结一下,希望给来者一点借鉴
    PV与并发之间换算的算法换算公式
  • 原文地址:https://www.cnblogs.com/kite/p/3726566.html
Copyright © 2011-2022 走看看