root / sqs-reader / src / main / java / net / sqs2 / omr / result / export / chart / ChartHTMLWriter.java @ 1854
History | View | Annotate | Download (9.8 KB)
| 1 | /**
|
|---|---|
| 2 | * ChartHTMLWriter.java |
| 3 | |
| 4 | Copyright 2009 KUBO Hiroya (hiroya@cuc.ac.jp). |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations under the License. |
| 17 | |
| 18 | Author hiroya |
| 19 | */ |
| 20 | |
| 21 | package net.sqs2.omr.result.export.chart;
|
| 22 | |
| 23 | import java.io.BufferedOutputStream; |
| 24 | import java.io.File; |
| 25 | import java.io.FileOutputStream; |
| 26 | import java.io.IOException; |
| 27 | import java.io.OutputStream; |
| 28 | import java.io.PrintWriter; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.List; |
| 32 | |
| 33 | import net.sqs2.omr.AppConstants; |
| 34 | import net.sqs2.omr.app.Messages; |
| 35 | import net.sqs2.omr.master.FormArea; |
| 36 | import net.sqs2.omr.master.FormMaster; |
| 37 | import net.sqs2.omr.model.SourceDirectory; |
| 38 | import net.sqs2.omr.model.SpreadSheet; |
| 39 | import net.sqs2.omr.result.chart.ChartConstants; |
| 40 | import net.sqs2.omr.result.export.HTMLWriter; |
| 41 | import net.sqs2.omr.result.export.ResultDirectoryUtil; |
| 42 | import net.sqs2.omr.result.export.SessionResultExportModule; |
| 43 | import net.sqs2.omr.result.export.html.HTMLReportExportModule; |
| 44 | |
| 45 | import org.jfree.data.category.DefaultCategoryDataset; |
| 46 | import org.jfree.data.general.DefaultPieDataset; |
| 47 | |
| 48 | import freemarker.template.Template; |
| 49 | import freemarker.template.TemplateException; |
| 50 | |
| 51 | class ChartHTMLWriter extends HTMLWriter { |
| 52 | private final SessionResultExportModule sessionResultEventExporter; |
| 53 | File chartDirectoryFile;
|
| 54 | ChartImageWriter chartImageFactory; |
| 55 | |
| 56 | ChartHTMLWriter(SessionResultExportModule reportExporter, File chartDirectoryFile, String skin) throws IOException { |
| 57 | super(skin);
|
| 58 | this.sessionResultEventExporter = reportExporter;
|
| 59 | this.chartDirectoryFile = chartDirectoryFile;
|
| 60 | this.chartImageFactory = new ChartImageWriter(640, 200); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void drawBarChartImage(List<FormArea> formAreaList, SpreadSheet spreadSheet) throws IOException, TemplateException { |
| 65 | SourceDirectory sourceDirectory = spreadSheet.getSourceDirectory(); |
| 66 | FormArea defaultFormArea = formAreaList.get(0);
|
| 67 | int columnIndex = defaultFormArea.getQuestionIndex();
|
| 68 | File chartImageFile = ResultDirectoryUtil.createTargetFile(sourceDirectory, defaultFormArea, "CHART", "bar"); |
| 69 | if (chartImageFile.exists()
|
| 70 | && this.sessionResultEventExporter.getTargetLastModifiedArray()[columnIndex] < chartImageFile.lastModified()) {
|
| 71 | return;
|
| 72 | } |
| 73 | |
| 74 | int[] numAnswers = this.sessionResultEventExporter.getValueTotalMatrix()[columnIndex]; |
| 75 | DefaultCategoryDataset barDataset = new DefaultCategoryDataset();
|
| 76 | |
| 77 | for (int itemIndex = 0; itemIndex < formAreaList.size(); itemIndex++) { |
| 78 | FormArea formArea = formAreaList.get(itemIndex); |
| 79 | int value = numAnswers[itemIndex];
|
| 80 | String label = formArea.getItemLabel();
|
| 81 | // barDataset.setValue(value, "", createLabel(itemIndex, label,
|
| 82 | // value));
|
| 83 | barDataset.setValue(value, "", createLabel(itemIndex, label, value));
|
| 84 | } |
| 85 | |
| 86 | OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(chartImageFile)); |
| 87 | this.chartImageFactory.saveBarChart(outputStream, ChartConstants.ITEM_LABEL,
|
| 88 | ChartConstants.UNIT_LABEL, barDataset); |
| 89 | outputStream.close(); |
| 90 | } |
| 91 | |
| 92 | void drawPieChartImage(List<FormArea> formAreaList, SpreadSheet spreadSheet) throws IOException, TemplateException { |
| 93 | SourceDirectory sourceDirectory = spreadSheet.getSourceDirectory(); |
| 94 | // FormMaster master = (FormMaster) sourceDirectory.getPageMaster();
|
| 95 | FormArea defaultFormArea = formAreaList.get(0);
|
| 96 | |
| 97 | int columnIndex = defaultFormArea.getQuestionIndex();
|
| 98 | |
| 99 | File chartImageFile = ResultDirectoryUtil.createTargetFile(sourceDirectory, defaultFormArea, "CHART", "pie"); |
| 100 | |
| 101 | if (chartImageFile.exists()
|
| 102 | && this.sessionResultEventExporter.getTargetLastModifiedArray()[columnIndex] < chartImageFile.lastModified()) {
|
| 103 | return;
|
| 104 | } |
| 105 | |
| 106 | int[] numAnswers = this.sessionResultEventExporter.getValueTotalMatrix()[columnIndex]; |
| 107 | int numNoAnswers = this.sessionResultEventExporter.getNumNoValues()[columnIndex]; |
| 108 | int numMultipleAnswers = this.sessionResultEventExporter.getNumMultipleValues()[columnIndex]; |
| 109 | |
| 110 | int total = spreadSheet.getNumRows() - numMultipleAnswers;
|
| 111 | |
| 112 | if (0 == total) { |
| 113 | return;
|
| 114 | } |
| 115 | |
| 116 | DefaultPieDataset pieDataset = new DefaultPieDataset();
|
| 117 | |
| 118 | for (int itemIndex = 0; itemIndex < formAreaList.size(); itemIndex++) { |
| 119 | FormArea formArea = formAreaList.get(itemIndex); |
| 120 | int value = numAnswers[itemIndex];
|
| 121 | String label = formArea.getItemLabel();
|
| 122 | pieDataset.setValue(createLabel(itemIndex, label, value, total), value); |
| 123 | } |
| 124 | |
| 125 | pieDataset.setValue(createLabel(ChartConstants.NO_ANSWER, numNoAnswers, total), numNoAnswers); |
| 126 | |
| 127 | OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(chartImageFile)); |
| 128 | this.chartImageFactory.savePieChart(outputStream, pieDataset);
|
| 129 | outputStream.close(); |
| 130 | } |
| 131 | |
| 132 | void writeChartIndexHTMLFile(File chartDirectoryFile, SpreadSheet spreadSheet, boolean isExportChartImageMode) throws IOException, TemplateException { |
| 133 | SourceDirectory sourceDirectory = spreadSheet.getSourceDirectory(); |
| 134 | FormMaster master = (FormMaster) sourceDirectory.getPageMaster(); |
| 135 | List<SelectChartData> charts = new ArrayList<SelectChartData>(); |
| 136 | HashMap<String, Object> map = new HashMap<String, Object>(); |
| 137 | |
| 138 | for (String qid : master.getQIDSet()) { |
| 139 | List<FormArea> formAreaList = master.getFormAreaList(qid);
|
| 140 | List<ChartItem> chartItemList = new ArrayList<ChartItem>(formAreaList.size()); |
| 141 | FormArea defaultFormArea = formAreaList.get(0);
|
| 142 | int columnIndex = defaultFormArea.getQuestionIndex();
|
| 143 | int[] numAnswers = this.sessionResultEventExporter.getValueTotalMatrix()[columnIndex]; |
| 144 | |
| 145 | if (defaultFormArea.isSelect1() || defaultFormArea.isSelect()) {
|
| 146 | for (int itemIndex = 0; itemIndex < formAreaList.size(); itemIndex++) { |
| 147 | FormArea formArea = formAreaList.get(itemIndex); |
| 148 | int value = numAnswers[itemIndex];
|
| 149 | chartItemList.add(new ChartItem(formArea, value));
|
| 150 | } |
| 151 | |
| 152 | if (defaultFormArea.isSelect1()) {
|
| 153 | int numNoAnswers = this.sessionResultEventExporter.getNumNoValues()[columnIndex]; |
| 154 | int numMultipleAnswers = this.sessionResultEventExporter.getNumMultipleValues()[columnIndex]; |
| 155 | SelectChartData chartData = new Select1ChartData(defaultFormArea, chartItemList,
|
| 156 | numNoAnswers, numMultipleAnswers); |
| 157 | charts.add(chartData); |
| 158 | } else if (defaultFormArea.isSelect()) { |
| 159 | SelectChartData chartData = new SelectChartData(defaultFormArea, chartItemList);
|
| 160 | charts.add(chartData); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | int numPages = sourceDirectory.getPageMaster().getNumPages();
|
| 166 | int numRows = sourceDirectory.getNumPageIDsTotal() / numPages;
|
| 167 | |
| 168 | HTMLReportExportModule.registTitle(master, map); |
| 169 | map.put("exportChartImageMode", isExportChartImageMode);
|
| 170 | map.put("path", sourceDirectory.getRelativePath());
|
| 171 | map.put("master", master);
|
| 172 | map.put("numRows", numRows);
|
| 173 | map.put("charts", charts);
|
| 174 | map.put("skin", AppConstants.GROUP_ID);
|
| 175 | registChartIndexParameters(map); |
| 176 | File chartIndexFile = new File(chartDirectoryFile, "index.html"); |
| 177 | PrintWriter chartDirectoryIndexWriter = ResultDirectoryUtil.createPrintWriter(chartIndexFile);
|
| 178 | Template chartIndexTemplate = this.loader.getTemplate("chartIndex.ftl", "UTF-8"); |
| 179 | chartIndexTemplate.process(map, chartDirectoryIndexWriter); |
| 180 | chartDirectoryIndexWriter.close(); |
| 181 | } |
| 182 | |
| 183 | private void registChartIndexParameters(HashMap<String, Object> map){ |
| 184 | registFTLParameters(map); |
| 185 | for(String key: new String[]{ |
| 186 | "noAnswerLabel",
|
| 187 | "errorLabel",
|
| 188 | "totalLabel",
|
| 189 | "unitLabel",
|
| 190 | "selectSingleLabel",
|
| 191 | "selectMultipleLabel",
|
| 192 | "exceptErrorDescriptionLabel"}){
|
| 193 | map.put(key, Messages.getString("result.chartIndex."+key));
|
| 194 | } |
| 195 | } |
| 196 | |
| 197 | String createLabel(int itemIndex, String label, int value, int total) { |
| 198 | int percent = 100 * value / total; |
| 199 | // return Integer.toString(itemIndex + 1) + ':' + label + " = " +
|
| 200 | // Integer.toString(value) + ChartConstants.UNIT_LABEL +
|
| 201 | // '('+percent+"%)";
|
| 202 | return label + " = " + Integer.toString(value) + ChartConstants.UNIT_LABEL + '(' + percent + "%)"; |
| 203 | } |
| 204 | |
| 205 | String createLabel(String label, int value, int total) { |
| 206 | if (0 < total) { |
| 207 | int percent = 100 * value / total; |
| 208 | return label + " = " + Integer.toString(value) + ChartConstants.UNIT_LABEL + '(' + percent + "%)"; |
| 209 | } else {
|
| 210 | return label + " = " + Integer.toString(value) + ChartConstants.UNIT_LABEL; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | String createLabel(int itemIndex, String label, int value) { |
| 215 | return Integer.toString(itemIndex + 1) + ':' + label + " = " + Integer.toString(value) |
| 216 | + ChartConstants.UNIT_LABEL; |
| 217 | } |
| 218 | |
| 219 | public class ChartItem { |
| 220 | FormArea formArea; |
| 221 | int value;
|
| 222 | |
| 223 | ChartItem(FormArea formArea, int value) {
|
| 224 | this.formArea = formArea;
|
| 225 | this.value = value;
|
| 226 | } |
| 227 | |
| 228 | public FormArea getFormArea() {
|
| 229 | return this.formArea; |
| 230 | } |
| 231 | |
| 232 | public int getValue() { |
| 233 | return this.value; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | public class SelectChartData { |
| 238 | private FormArea defaultFormArea;
|
| 239 | private List<ChartItem> chartItemList; |
| 240 | |
| 241 | SelectChartData(FormArea defaultFormArea, List<ChartItem> chartitemList) {
|
| 242 | this.defaultFormArea = defaultFormArea;
|
| 243 | this.chartItemList = chartitemList;
|
| 244 | } |
| 245 | |
| 246 | public FormArea getDefaultFormArea() {
|
| 247 | return this.defaultFormArea; |
| 248 | } |
| 249 | |
| 250 | public List<ChartItem> getChartItemList() { |
| 251 | return this.chartItemList; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | public class Select1ChartData extends SelectChartData { |
| 256 | private int numNoAnswers, numMultipleAnswers; |
| 257 | |
| 258 | Select1ChartData(FormArea defaultFormArea, List<ChartItem> chartItemList, int numNoAnswers, |
| 259 | int numMultipleAnswers) {
|
| 260 | super(defaultFormArea, chartItemList);
|
| 261 | this.numNoAnswers = numNoAnswers;
|
| 262 | this.numMultipleAnswers = numMultipleAnswers;
|
| 263 | } |
| 264 | |
| 265 | public int getNumNoAnswers() { |
| 266 | return this.numNoAnswers; |
| 267 | } |
| 268 | |
| 269 | public int getNumMultipleAnswers() { |
| 270 | return this.numMultipleAnswers; |
| 271 | } |
| 272 | } |
| 273 | } |

SourceEditor2.0(2010/08/18)
