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 }