1 33 34 package com.icesoft.faces.component.outputchart; 35 36 import java.awt.Color ; 37 import java.awt.Paint ; 38 import java.awt.Shape ; 39 import java.io.OutputStream ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 45 import javax.faces.component.UIComponent; 46 import javax.faces.context.FacesContext; 47 48 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 import org.krysalis.jcharts.Chart; 51 import org.krysalis.jcharts.encoders.JPEGEncoder; 52 import org.krysalis.jcharts.imageMap.ImageMap; 53 import org.krysalis.jcharts.imageMap.ImageMapArea; 54 import org.krysalis.jcharts.properties.LegendProperties; 55 import org.krysalis.jcharts.properties.PointChartProperties; 56 import org.krysalis.jcharts.test.TestDataGenerator; 57 58 public abstract class AbstractChart { 59 private final Log log = LogFactory.getLog(AbstractChart.class); 60 protected OutputChart outputChart = null; 61 protected Chart chart = null; 62 private Chart userDefinedChart = null; 63 private static ColorMap colorMap = new ColorMap(); 64 private static ShapeMap shapeMap = new ShapeMap(); 65 private static LegendPlacementMap legendPlacementMap = new LegendPlacementMap(); 66 private ImageMapArea clickedImageMapArea; 67 String type = null; 68 69 public AbstractChart(UIComponent uiComponent) throws Throwable { 70 this.outputChart = (OutputChart) uiComponent; 71 this.type = outputChart.getType(); 72 } 73 74 public void encode() throws Throwable { 75 this.type = outputChart.getType(); 77 Chart currentChart = getChart(); 78 if (chart == currentChart) { 79 buildChart(); 80 } 81 if (getChart() != null) { 82 if (outputChart.isClientSideImageMap()) { 83 generateMap(getChart()); 84 } 85 OutputStream outputStream = outputChart.getNewOutputStream(); 86 JPEGEncoder.encode(getChart(), 1.0f, outputStream); 87 outputStream.flush(); 88 outputStream.close(); 89 } else { 90 log.equals("The jchart is not defined for the "+ 91 outputChart.getClientId(FacesContext.getCurrentInstance())+ 92 ", please check if the proper [type] has been defined"); 93 } 94 } 95 96 private Map generatedImageMapArea = new HashMap (); 97 98 private void generateMap(Chart chart) throws Throwable { 99 chart.renderWithImageMap(); 100 generatedImageMapArea.clear(); 101 ImageMap imageMap = chart.getImageMap(); 102 Iterator iterator = imageMap.getIterator(); 103 while (iterator.hasNext()) { 104 ImageMapArea mapArea = (ImageMapArea) iterator.next(); 105 generatedImageMapArea.put(mapArea.hashCode() + "", mapArea); 106 } 107 } 108 109 protected abstract void buildChart() throws Throwable ; 110 111 static AbstractChart createChart(UIComponent uiComponent) throws Throwable { 112 String type = ((OutputChart) uiComponent).getType(); 113 if (OutputChart.PIE2D_CHART_TYPE.equalsIgnoreCase(type) || 114 OutputChart.PIE3D_CHART_TYPE.equalsIgnoreCase(type)) { 115 return new PieChart(uiComponent); 116 } else { 117 return new AxisChart(uiComponent); 118 } 119 } 120 121 public Chart getChart() { 122 if (userDefinedChart != null) { 123 return userDefinedChart; 124 } 125 return chart; 126 } 127 128 public void setChart(Chart userDefinedChart) { 129 this.userDefinedChart = userDefinedChart; 130 } 131 132 public String [] getAsStringArray(Object obj) { 133 if (obj instanceof String []) { 134 return (String []) obj; 135 } else if (obj instanceof String ) { 136 return ((String ) obj).split(","); 137 } else if (obj instanceof List ) { 138 return (String []) ((List ) obj).toArray(new String [0]); 139 }else { 140 return null; 141 } 142 } 143 144 145 public double[][] getAs2dDoubleArray(Object obj) { 146 double[][] dbl2DArray = null; 147 if (obj instanceof double[][]) { 148 dbl2DArray = (double[][]) obj; 149 } else if (obj instanceof String ) { 150 String [] temp = ((String ) obj).split(":"); 151 dbl2DArray = new double[temp.length][]; 152 for (int i = 0; i < temp.length; i++) { 153 dbl2DArray[i] = getAsDoubleArray(temp[i]); 154 } 155 } else if (obj instanceof List ) { 156 List list = (List ) obj; 157 double[] outer = (double[]) list.get(0); 158 dbl2DArray = new double[outer.length][list.size()]; 159 for (int j = 0; j < list.size(); j++) { 160 for (int i = 0; i < outer.length; i++) { 161 outer = (double[]) list.get(j); 162 dbl2DArray[i][j] = outer[i]; 163 } 164 } 165 } 166 return dbl2DArray; 167 } 168 169 170 public double[] getAsDoubleArray(Object obj) { 171 double[] dblArray = null; 172 if (obj instanceof String ) { 173 String [] temp = ((String ) obj).split(","); 174 dblArray = new double[temp.length]; 175 for (int i = 0; i < temp.length; i++) { 176 dblArray[i] = Double.parseDouble(temp[i]); 177 } 178 } else if (obj instanceof List ) { 179 List objList = (List ) obj; 180 dblArray = new double[objList.size()]; 181 for (int i = 0; i < objList.size(); i++) { 182 dblArray[i] = ((Double ) objList.get(i)).doubleValue(); 183 } 184 } else if (obj instanceof double[]) { 185 dblArray = (double[]) obj; 186 } 187 return dblArray; 188 } 189 190 Paint [] paintArray = null; 191 192 public Paint [] getAsPaintArray(Object obj) { 193 if (obj instanceof String ) { 194 if (paintArray != null) { 195 return paintArray; 196 } 197 String [] temp = ((String ) obj).split(","); 198 paintArray = new Paint [temp.length]; 199 for (int i = 0; i < temp.length; i++) { 200 paintArray[i] = colorMap.getColor(temp[i].trim()); 201 } 202 } else if (obj instanceof List ) { 203 List objList = (List ) obj; 204 paintArray = new Paint [objList.size()]; 205 for (int i = 0; i < objList.size(); i++) { 206 paintArray[i] = (Paint ) objList.get(i); 207 } 208 } 209 return paintArray; 210 } 211 212 213 public Shape [] getAsShapeArray(Object obj) { 214 Shape [] shapeArray = null; 215 if (obj instanceof String ) { 216 String [] temp = ((String ) obj).split(","); 217 shapeArray = new Shape [temp.length]; 218 for (int i = 0; i < temp.length; i++) { 219 shapeArray[i] = shapeMap.getShape(temp[i].trim()); 220 } 221 } else if (obj instanceof List ) { 222 List objList = (List ) obj; 223 shapeArray = new Shape [objList.size()]; 224 for (int i = 0; i < objList.size(); i++) { 225 shapeArray[i] = (Shape ) objList.get(i); 226 } 227 } 228 return shapeArray; 229 } 230 231 Shape [] getGeneratedShapes(int count) { 232 Shape [] tempShapeArray = new Shape [count]; 233 Iterator it = shapeMap.values().iterator(); 234 for (int i = 0; i < count; i++) { 235 if (it.hasNext()) { 236 tempShapeArray[i] = (Shape ) it.next(); 237 } else { 238 it = shapeMap.values().iterator(); 239 tempShapeArray[i] = (Shape ) it.next(); 240 } 241 } 242 return tempShapeArray; 243 } 244 245 String [] getGeneratedLabels(String label, int count) { 246 String [] tempStringArray = new String [count]; 247 for (int i = 0; i < count; i++) { 248 tempStringArray[i] = label + " " + i; 249 } 250 return tempStringArray; 251 } 252 253 public Paint [] getPaints(Object obj, int count) { 254 if (obj == null && paintArray == null) { 255 return paintArray = TestDataGenerator.getRandomPaints(count); 256 } else if (obj == null && paintArray != null) { 257 return paintArray; 258 } else { 259 return getAsPaintArray(obj); 260 } 261 } 262 263 public Map getGeneratedImageMapArea() { 264 return generatedImageMapArea; 265 } 266 267 public ImageMapArea getClickedImageMapArea() { 268 return clickedImageMapArea; 269 } 270 271 public void setClickedImageMapArea(ImageMapArea clickedImageMapArea) { 272 this.clickedImageMapArea = clickedImageMapArea; 273 } 274 275 public static Color getColor(String color) { 276 return colorMap.getColor(color); 277 } 278 279 public LegendProperties getLegendProperties() { 280 LegendProperties legendProperties = new LegendProperties(); 281 legendProperties.setPlacement(legendPlacementMap.getLegendPlacement( 282 String.valueOf(outputChart.getLegendPlacement()))); 283 Object legendColumns = outputChart.getLegendColumns(); 284 if (legendColumns instanceof Integer ) { 285 legendProperties.setNumColumns(((Integer )outputChart.getLegendColumns()).intValue()); 286 }else if (legendColumns instanceof String ) { 287 legendProperties.setNumColumns(Integer.parseInt(outputChart.getLegendColumns().toString())); 288 } 289 return legendProperties; 290 } 291 } 292 293 class ColorMap extends HashMap { 294 private static final long serialVersionUID = 1L; 295 296 public ColorMap() { 297 this.put("black", Color.BLACK); 298 this.put("blue", Color.BLUE); 299 this.put("cyan", Color.CYAN); 300 this.put("darkGray", Color.DARK_GRAY); 301 this.put("gray", Color.GRAY); 302 this.put("green", Color.GREEN); 303 this.put("lightGray", Color.LIGHT_GRAY); 304 this.put("magenta", Color.MAGENTA); 305 this.put("orange", Color.ORANGE); 306 this.put("pink", Color.PINK); 307 this.put("red", Color.RED); 308 this.put("white", Color.WHITE); 309 this.put("yellow", Color.YELLOW); 310 } 311 312 public Color getColor(String key) { 313 return (Color ) super.get(key); 314 } 315 } 316 317 class ShapeMap extends HashMap { 318 319 private static final long serialVersionUID = 1L; 320 321 public ShapeMap() { 322 this.put("circle", PointChartProperties.SHAPE_CIRCLE); 323 this.put("diamond", PointChartProperties.SHAPE_DIAMOND); 324 this.put("square", PointChartProperties.SHAPE_SQUARE); 325 this.put("triangle", PointChartProperties.SHAPE_TRIANGLE); 326 } 327 328 public Shape getShape(String key) { 329 return (Shape ) super.get(key); 330 } 331 } 332 333 class LegendPlacementMap extends HashMap { 334 public LegendPlacementMap() { 335 this.put("top", new Integer (LegendProperties.TOP)); 336 this.put("bottom", new Integer (LegendProperties.BOTTOM)); 337 this.put("left", new Integer (LegendProperties.LEFT)); 338 this.put("right", new Integer (LegendProperties.RIGHT)); 339 } 340 341 public int getLegendPlacement(String key) { 342 if (!super.containsKey(key)) { 343 return 0; 344 } 345 return Integer.parseInt(super.get(key).toString()); 346 } 347 } | Popular Tags |