码迷,mamicode.com
首页 > 其他好文 > 详细

jfreechart 的一般配置显示

时间:2014-05-15 15:55:26      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

bubuko.com,布布扣
  1 package kite.struts2.action;
  2 
  3 
  4 import java.awt.Font;
  5 
  6 import javax.annotation.Resource;
  7 
  8 import kite.domain.Question;
  9 import kite.domain.statistics.OptionStatisticsModel;
 10 import kite.domain.statistics.QuestionStatisticsModel;
 11 import kite.service.StatisticsService;
 12 
 13 import org.jfree.chart.ChartFactory;
 14 import org.jfree.chart.JFreeChart;
 15 import org.jfree.chart.plot.PiePlot;
 16 import org.jfree.chart.plot.PlotOrientation;
 17 import org.jfree.data.category.DefaultCategoryDataset;
 18 import org.jfree.data.general.DefaultPieDataset;
 19 import org.jfree.util.Rotation;
 20 import org.springframework.context.annotation.Scope;
 21 import org.springframework.stereotype.Controller;
 22 
 23 
 24 /**
 25  * 针对非矩阵式题型的设计,可有饼图 柱状图 折线图等
 26  */
 27 @Controller("chartOutputAction")
 28 @Scope("prototype")
 29 public class ChartOutputAction extends BaseAction<Question> {
 30     private static final long serialVersionUID = -9021947290801678287L;
 31     /* 平面饼图 */
 32     private static final int CHARTTYPE_PIE_2D = 0;
 33     /* 立体饼图 */
 34     private static final int CHARTTYPE_PIE_3D = 1;
 35     /* 水平平面柱状图 */
 36     private static final int CHARTTYPE_BAR_2D_H = 2;
 37     /* 竖直平面柱状图 */
 38     private static final int CHARTTYPE_BAR_2D_V = 3;
 39     /* 水平立体柱状图 */
 40     private static final int CHARTTYPE_BAR_3D_H = 4;
 41     /* 竖直立体柱状图 */
 42     private static final int CHARTTYPE_BAR_3D_V = 5;
 43     /* 平面折线图 */
 44     private static final int CHARTTYPE_LINE_2D = 6;
 45     /* 立体折线图 */
 46     private static final int CHARTTYPE_LINE_3D = 7;
 47 
 48     // 图表类型
 49     private int chartType;
 50     // 对哪个问题进行统计
 51     private Integer qid;
 52 
 53     // 注入统计服务
 54     @Resource
 55     private StatisticsService ss;
 56 
 57     /**
 58      * 生成图表并输出到浏览器
 59      */
 60     public String execute() {
 61         return SUCCESS;
 62     }
 63 
 64     @SuppressWarnings("deprecation")
 65     public JFreeChart getChart() {
 66         JFreeChart chart = null ;
 67         try {
 68             Font font = new Font("宋体", 0, 20);// 字体
 69             QuestionStatisticsModel qsm = ss.statistics(qid);
 70             DefaultPieDataset pieds = null;// 饼图的数据集
 71             DefaultCategoryDataset cateds = null;// 种类数据集
 72             //构造数据集
 73             if(chartType < 2){
 74                 pieds = new DefaultPieDataset();
 75                 for (OptionStatisticsModel om : qsm.getOsms()) {
 76                     pieds.setValue(om.getOptionLabel(), om.getCount());
 77                 }
 78             }
 79             else{
 80                 cateds = new DefaultCategoryDataset();
 81                 for (OptionStatisticsModel osm : qsm.getOsms()) {
 82                     cateds.addValue(osm.getCount(), osm.getOptionLabel(), "");
 83                 }
 84             }
 85             
 86             // 判断要求的图形
 87             switch (chartType) {
 88                 case CHARTTYPE_PIE_2D:// 平面饼图
 89                     chart = ChartFactory.createPieChart(qsm.getQuestion().getTitle(), pieds, true, false, false);
 90                     break ;
 91                 case CHARTTYPE_PIE_3D:// 立体饼图
 92                     chart = ChartFactory.createPieChart3D(qsm.getQuestion().getTitle(), pieds, true, true, true);
 93                     //设置前景色透明度
 94                     chart.getPlot().setForegroundAlpha(0.6f);
 95                     break ;
 96                 case CHARTTYPE_BAR_2D_H:// 平面条形图
 97                     chart = ChartFactory.createBarChart(qsm.getQuestion().getTitle(), "", "", cateds,
 98                             PlotOrientation.HORIZONTAL, true, true, true);
 99                     break ;
100                 case CHARTTYPE_BAR_2D_V:// 平面条形图
101                     chart = ChartFactory.createBarChart(qsm.getQuestion().getTitle(), "", "", cateds,
102                             PlotOrientation.VERTICAL, true, true, true);
103                 case CHARTTYPE_BAR_3D_H:// 平面条形图
104                     chart = ChartFactory.createBarChart3D(qsm.getQuestion().getTitle(), "", "", cateds,
105                             PlotOrientation.HORIZONTAL, true, true, true);
106                 case CHARTTYPE_BAR_3D_V:// 平面条形图
107                     chart = ChartFactory.createBarChart3D(qsm.getQuestion().getTitle(), "", "", cateds,
108                             PlotOrientation.VERTICAL, true, true, true);
109                     break ;
110                 //
111                 case CHARTTYPE_LINE_2D:// 平面条形图
112                     chart = ChartFactory.createLineChart(qsm.getQuestion().getTitle(), "", "", cateds,
113                             PlotOrientation.VERTICAL, true, true, true);
114                     break ;
115                 case CHARTTYPE_LINE_3D:// 平面条形图
116                     chart = ChartFactory.createLineChart3D(qsm.getQuestion().getTitle(), "", "", cateds,
117                             PlotOrientation.HORIZONTAL, true, true, true);
118                     break ;
119             }
120             //设置标题和提示条中文
121             chart.getTitle().setFont(font);
122             chart.getLegend().setItemFont(font);
123             //chart.setBackgroundImageAlpha(0.2f);
124         
125             //设置饼图特效
126             if(chart.getPlot() instanceof PiePlot){
127                 PiePlot pieplot = (PiePlot) chart.getPlot();
128                 pieplot.setLabelFont(font);
129                 pieplot.setExplodePercent(0, 0.1);
130                 pieplot.setStartAngle(-15);
131                 pieplot.setDirection(Rotation.CLOCKWISE);
132                 pieplot.setNoDataMessage("No data to display");
133                 //pieplot.setForegroundAlpha(0.5f);
134                 //pieplot.setBackgroundImageAlpha(0.3f);
135             }
136             //设置非饼图效果
137             else{
138                 chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
139                 chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
140                 chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
141                 chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
142             }
143         } catch (Exception e) {
144             e.printStackTrace();
145         }
146         return chart ;
147     }
148 
149     public int getChartType() {
150         return chartType;
151     }
152 
153     public void setChartType(int chartType) {
154         this.chartType = chartType;
155     }
156 
157     public Integer getQid() {
158         return qid;
159     }
160 
161     public void setQid(Integer qid) {
162         this.qid = qid;
163     }
164 }
bubuko.com,布布扣

 

jfreechart 的一般配置显示,布布扣,bubuko.com

jfreechart 的一般配置显示

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/kite/p/3726617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!