1 56 package org.opencrx.kernel.gui.generic.servlet.attribute; 57 58 import java.io.BufferedReader ; 59 import java.io.ByteArrayInputStream ; 60 import java.io.ByteArrayOutputStream ; 61 import java.io.IOException ; 62 import java.io.InputStreamReader ; 63 import java.io.Serializable ; 64 import java.util.HashMap ; 65 import java.util.Map ; 66 67 import javax.servlet.http.HttpServletRequest ; 68 69 import org.openmdx.application.gui.generic.servlet.Action; 70 import org.openmdx.application.gui.generic.servlet.ApplicationContext; 71 import org.openmdx.application.gui.generic.servlet.attribute.Attribute; 72 import org.openmdx.application.gui.generic.servlet.attribute.BinaryValue; 73 import org.openmdx.application.gui.generic.servlet.attribute.FieldDef; 74 import org.openmdx.application.gui.generic.servlet.view.View; 75 import org.openmdx.application.log.AppLog; 76 import org.openmdx.base.exception.ServiceException; 77 import org.openmdx.base.text.conversion.HtmlEncoder; 78 79 public class ChartValue 80 extends BinaryValue 81 implements Serializable { 82 83 public ChartValue( 85 Object object, 86 FieldDef fieldDef, 87 ApplicationContext application 88 ) { 89 super( 90 object, 91 fieldDef, 92 application 93 ); 94 } 95 96 public String toHtml( 98 HttpServletRequest request, 99 View view, 100 Attribute attribute, 101 Map modifiers, 102 Map popupImages, 103 Map renderingContext 104 ) { 105 int currentChartId = renderingContext.get("currentChartId") != null 106 ? ((Integer )renderingContext.get("currentChartId")).intValue() 107 : 0; 108 String result = ""; 109 try { 110 Action binaryValueAction = (Action)this.getValue(); 111 if(binaryValueAction != null) { 112 ByteArrayOutputStream os = new ByteArrayOutputStream (); 113 try { 114 this.getBinaryValue(os); 115 } 116 catch(ServiceException e) { 117 AppLog.warning(e.getMessage(), e.getCause(), 1); 118 } 119 BufferedReader reader = new BufferedReader ( 120 new InputStreamReader (new ByteArrayInputStream (os.toByteArray())) 121 ); 122 Map data = new HashMap (); 123 String line = null; 124 while((line = reader.readLine()) != null) { 125 if(line.indexOf(":") >= 0) { 126 data.put( 127 line.substring(0, line.indexOf(":")), 128 line.substring(line.indexOf(":") + 1) 129 ); 130 } 131 } 132 133 result += modifiers.get("gapModifier"); 135 result += "<td class=\"label\"><span class=\"nw\">" + attribute.getLabel() + "</span></td>"; 136 result += "<td " + modifiers.get("rowSpanModifier") + " class=\"valueL\" " + modifiers.get("widthModifier") + ">"; 137 138 int nCharts = new Integer (data.get("COUNT").toString().trim()).intValue(); 140 for(int chartIndex = 0; chartIndex < nCharts; chartIndex++) { 141 String chartName = "CHART[" + chartIndex + "]"; 142 String chartType = data.get(chartName + ".TYPE") + ""; 143 144 if("HORIZBAR".equals(chartType.trim())) { 146 String chartLabel = data.get(chartName + ".LABEL") + ""; 147 String xMinValue = data.get(chartName + ".MINVALUE") + ""; 148 String xMaxValue = data.get(chartName + ".MAXVALUE") + ""; 149 String scaleXTitle = data.get(chartName + ".SCALEXTITLE") + ""; 150 String scaleYTitle = data.get(chartName + ".SCALEYTITLE") + ""; 151 String yLabels = ""; 152 String xValues = ""; 153 String xValues2 = ""; 154 String borderColors = ""; 155 String fillColors = ""; 156 String fillColors2 = ""; 157 int itemCount = data.get(chartName + ".COUNT") == null 158 ? 0 159 : new Integer (data.get(chartName + ".COUNT").toString().trim()).intValue(); 160 String styleModifier = attribute.getSpanRow() > 1 162 ? "style=\"height: " + (1.2 + (attribute.getSpanRow()-1) * 1.5) + "em\"" 163 : "style=\"height: " + (6.0 + itemCount * 1.2) + "em\""; 164 for( 165 int count = 0; 166 count < itemCount; 167 count++ 168 ) { 169 yLabels += yLabels.length() == 0 ? "" : ", "; 170 yLabels += "\"" + data.get(chartName + ".LABEL[" + count + "]") + "\""; 171 xValues += xValues.length() == 0 ? "" : ", "; 172 xValues += "\"" + data.get(chartName + ".VAL[" + count + "]") + "\""; 173 borderColors += borderColors.length() == 0 ? "" : ", "; 174 borderColors += "\"" + data.get(chartName + ".BORDER[" + count + "]") + "\""; 175 fillColors += fillColors.length() == 0 ? "" : ", "; 176 fillColors += "\"" + data.get(chartName + ".FILL[" + count + "]") + "\""; 177 if(data.get(chartName + ".VAL2[" + count + "]") != null) { 179 xValues2 += xValues2.length() == 0 ? "" : ", "; 180 xValues2 += "\"" + data.get(chartName + ".VAL2[" + count + "]") + "\""; 181 } 182 if(data.get(chartName + ".FILL2[" + count + "]") != null) { 184 fillColors2 += fillColors2.length() == 0 ? "" : ", "; 185 fillColors2 += "\"" + data.get(chartName + ".FILL2[" + count + "]") + "\""; 186 } 187 } 188 result += "<div><iframe id=\"Chart" + currentChartId + "\" SRC=\"blank.html\" name=\"Chart" + currentChartId + "\" frameborder=0 scrolling=\"no\" width=\"100%\"" + styleModifier + "></iframe></div>"; 189 result += "<script language=\"javascript\" type=\"text/javascript\">"; 190 result += " function displayChart" + currentChartId + "() {"; 191 if((xValues.length() > 0) && (fillColors2.length() > 0)) { 192 result += " calcChartTypeHorizontalBarsOverlay('Chart" + currentChartId + "', '" + HtmlEncoder.encode(chartLabel, false) + "', '" + scaleXTitle + "', '" + scaleYTitle + "', Array(" + xValues + "), " + xMinValue + ", " + xMaxValue + ", Array(" + yLabels + "), Array(" + borderColors + "), Array(" + fillColors + "), Array(" + xValues2 +"), Array(" + fillColors2 + "));"; 193 } 194 else { 195 result += " calcChartTypeHorizontalBars('Chart" + currentChartId + "', '" + HtmlEncoder.encode(chartLabel, false) + "', '" + scaleXTitle + "', '" + scaleYTitle + "', Array(" + xValues + "), " + xMinValue + ", " + xMaxValue + ", Array(" + yLabels + "), Array(" + borderColors + "), Array(" + fillColors + "));"; 196 } 197 result += " }"; 198 result += "</script>"; 199 } 200 201 else if("VERTBAR".equals(chartType.trim())) { 203 String chartLabel = data.get(chartName + ".LABEL") + ""; 204 String yMinValue = data.get(chartName + ".MINVALUE") + ""; 205 String yMaxValue = data.get(chartName + ".MAXVALUE") + ""; 206 String scaleXTitle = data.get(chartName + ".SCALEXTITLE") + ""; 207 String scaleYTitle = data.get(chartName + ".SCALEYTITLE") + ""; 208 String xLabels = ""; 209 String yValues = ""; 210 String borderColors = ""; 211 String fillColors = ""; 212 int itemCount = data.get(chartName + ".COUNT") == null 213 ? 0 214 : new Integer (data.get(chartName + ".COUNT").toString().trim()).intValue(); 215 String styleModifier = attribute.getSpanRow() > 1 216 ? "style=\"height: " + (1.2 + (attribute.getSpanRow()-1) * 1.5) + "em\"" 217 : "15em"; 218 for(int count = 0; count < itemCount; count++) { 219 xLabels += xLabels.length() == 0 ? "" : ", "; 220 xLabels += "\"" + data.get(chartName + ".LABEL[" + count + "]") + "\""; 221 yValues += yValues.length() == 0 ? "" : ", "; 222 yValues += data.get(chartName + ".VAL[" + count + "]"); 223 borderColors += borderColors.length() == 0 ? "" : ", "; 224 borderColors += "\"" + data.get(chartName + ".BORDER[" + count + "]") + "\""; 225 fillColors += fillColors.length() == 0 ? "" : ", "; 226 fillColors += "\"" + data.get(chartName + ".FILL[" + count + "]") + "\""; 227 } 228 result += "<div><iframe id=\"Chart" + currentChartId + "\" SRC=\"blank.html\" name=\"Chart" + currentChartId + "\" frameborder=0 scrolling=\"no\" width=\"100%\" " + styleModifier + "></iframe></div>"; 229 result += "<script language=\"javascript\" type=\"text/javascript\">"; 230 result += " function displayChart" + currentChartId + "() {"; 231 result += " calcChartTypeVerticalBars('Chart" + currentChartId + "', '" + HtmlEncoder.encode(chartLabel, false) + "', '" + scaleXTitle + "', '" + scaleYTitle + "', Array(" + yValues + "), " + yMinValue + ", " + yMaxValue + ", Array(" + xLabels + "), Array(" + borderColors + "), Array(" + fillColors + "));"; 232 result += " }"; 233 result += "</script>"; 234 } 235 } 236 result += "</td>"; 237 } 238 } 239 catch(IOException e) {} 240 renderingContext.put( 241 "currentChartId", 242 new Integer (currentChartId+1) 243 ); 244 return result; 245 } 246 252 private static final long serialVersionUID = 3258689922910336053L; 253 254 } 255 256 | Popular Tags |