jfreechart 结合 struts2
2016-04-21 00:00
531 查看
action
<actionname="chartOutputAction"class="chartOutputAction">
<resultname="success"type="chart">
<paramname="height">600</param>
<paramname="width">800</param>
<paramname="bufferSize">1024</param>
</result>
</action>
1packagekite.struts2.action;2 3importjava.awt.Font;4importjava.io.ByteArrayInputStream;5importjava.io.ByteArrayOutputStream;6importjava.io.InputStream;7 8importjavax.annotation.Resource;9 10importorg.jfree.chart.ChartFactory;11importorg.jfree.chart.ChartUtilities;12importorg.jfree.chart.JFreeChart;13importorg.jfree.chart.labels.StandardPieSectionLabelGenerator;14importorg.jfree.chart.plot.PiePlot;15importorg.jfree.data.general.DefaultPieDataset;16importorg.springframework.context.annotation.Scope;17importorg.springframework.stereotype.Controller;18 19importkite.domain.Question;20importkite.domain.statistics.OptionStatisticsModel;21importkite.domain.statistics.QuestionStatisticsModel;22importkite.service.StatisticsService;23 24@Controller("chartOutputAction")25@Scope("prototype")26publicclassChartOutputActionextendsBaseAction<Question> 27{28@Resource(name="statisticsService")29StatisticsServicestatisticsService;30privateIntegerqid;31 32privateIntegerchartType;33publicStringexecute()throwsException34{35returnSUCCESS;36}37 38publicInputStreamgetInputStream()39{40try 41{42QuestionStatisticsModelqsm=statisticsService.statistics(qid);43DefaultPieDatasetds=newDefaultPieDataset();44for(OptionStatisticsModelosm:qsm.getOsms())45{46ds.setValue(osm.getOptionLabel(),osm.getCount());47}48JFreeChartchart=ChartFactory.createPieChart(qsm.getQuestion().getTitle(),ds,true,false,false);49 50//设置标题和提示条中文 51chart.getTitle().setFont(newFont("宋体",Font.BOLD,25));52chart.getLegend().setItemFont(newFont("宋体",Font.BOLD,20));53 54PiePlotplot=(PiePlot)chart.getPlot();55plot.setLabelFont(newFont("宋体",Font.PLAIN,15));56plot.setLabelGenerator(newStandardPieSectionLabelGenerator("{0}(选择人数:{1}总数:{3}占百分比:{2})"));57 58ByteArrayOutputStreambaos=newByteArrayOutputStream();59ChartUtilities.writeChartAsJPEG(baos,chart,800,600);60ByteArrayInputStreambais=newByteArrayInputStream(baos.toByteArray());61returnbais;62}catch(Exceptione)63{64e.printStackTrace();65}66returnnull;67 68}69 70publicIntegergetQid()71{72returnqid;73}74 75publicvoidsetQid(Integerqid)76{77this.qid=qid;78}79 80publicIntegergetChartType()81{82returnchartType;83}84 85publicvoidsetChartType(IntegerchartType)86{87this.chartType=chartType;88}89} struts.xml文件配置
1<actionname="chartOutputAction"class="chartOutputAction"> 2<resultname="success"type="stream"> 3<paramname="contentType">image/jpeg</param> 4<paramname="inputName">inputStream</param>//getInputStreaminputStream 5<paramname="bufferSize">1024</param> 6</result> 7</action> 改进使用struts2的jfreechart插件ChartResult 修改action文件
1publicJFreeChartgetChart()2{3try 4{5QuestionStatisticsModelqsm=statisticsService.statistics(qid);6DefaultPieDatasetds=newDefaultPieDataset();7for(OptionStatisticsModelosm:qsm.getOsms())8{9ds.setValue(osm.getOptionLabel(),osm.getCount());10}11JFreeChartchart=ChartFactory.createPieChart(qsm.getQuestion().getTitle(),ds,true,false,false);12 13//设置标题和提示条中文 14chart.getTitle().setFont(newFont("宋体",Font.BOLD,25));15chart.getLegend().setItemFont(newFont("宋体",Font.BOLD,20));16 17PiePlotplot=(PiePlot)chart.getPlot();18plot.setLabelFont(newFont("宋体",Font.PLAIN,15));19plot.setLabelGenerator(newStandardPieSectionLabelGenerator("{0}(选择人数:{1}总数:{3}占百分比:{2})"));20 21returnchart;22}catch(Exceptione)23{24e.printStackTrace();25}26returnnull;27 28}
修改xml配置文件需要继承jfreechart-default
<actionname="chartOutputAction"class="chartOutputAction">
<resultname="success"type="chart">
<paramname="height">600</param>
<paramname="width">800</param>
<paramname="bufferSize">1024</param>
</result>
</action>
相关文章推荐
- spring 远程调用
- java 遍历arrayList常用的四种方法
- JAVA编译和运行的过程
- JAVA解耦--入门学生信息管理
- Java Socket探究
- Spring工作原理详解
- struts2、hibernate工作原理和流程
- SpringMVC工作原理和工作流程
- 理解Java中字符流与字节流的区别
- Spring+Quartz实现定时任务 (二)
- Eclipse从服务器导入SVN项目的方式
- JAVA二维码生成
- java线程池和关闭线程池中的线程
- java事务(二)——本地事务
- spring的7个模块
- highcharts加载数据库数据(java版)
- Java基础-对象导论
- Struts标签库详解【3】
- Struts标签库详解【2】
- Java中自己定义缓存方式