您的位置:首页 > 编程语言 > Java开发

jfreechart 结合 struts2

2016-04-21 00:00 531 查看
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>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: