001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.outputchart;
035:
036: import java.awt.Color;
037: import java.awt.Paint;
038: import java.awt.Shape;
039: import java.io.File;
040: import java.io.OutputStream;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.List;
044: import java.util.Map;
045:
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048:
049: import org.apache.commons.logging.Log;
050: import org.apache.commons.logging.LogFactory;
051: import org.krysalis.jcharts.Chart;
052: import org.krysalis.jcharts.encoders.JPEGEncoder;
053: import org.krysalis.jcharts.imageMap.ImageMap;
054: import org.krysalis.jcharts.imageMap.ImageMapArea;
055: import org.krysalis.jcharts.properties.LegendProperties;
056: import org.krysalis.jcharts.properties.PointChartProperties;
057: import org.krysalis.jcharts.test.TestDataGenerator;
058:
059: public abstract class AbstractChart {
060: private final Log log = LogFactory.getLog(AbstractChart.class);
061: protected OutputChart outputChart = null;
062: protected Chart chart = null;
063: private Chart userDefinedChart = null;
064: private static ColorMap colorMap = new ColorMap();
065: private static ShapeMap shapeMap = new ShapeMap();
066: private static LegendPlacementMap legendPlacementMap = new LegendPlacementMap();
067: private ImageMapArea clickedImageMapArea;
068: String type = null;
069: private File imageFile;
070:
071: public AbstractChart(UIComponent uiComponent) throws Throwable {
072: this .outputChart = (OutputChart) uiComponent;
073: this .type = outputChart.getType();
074: }
075:
076: public void encode() throws Throwable {
077: //if type is dynamic here we should update it
078: this .type = outputChart.getType();
079: Chart currentChart = getChart();
080: if (chart == currentChart) {
081: buildChart();
082: }
083: if (getChart() != null) {
084: if (outputChart.isClientSideImageMap()) {
085: generateMap(getChart());
086: }
087: OutputStream outputStream = outputChart
088: .getNewOutputStream();
089: JPEGEncoder.encode(getChart(), 1.0f, outputStream);
090: outputStream.flush();
091: outputStream.close();
092: } else {
093: log
094: .equals("The jchart is not defined for the "
095: + outputChart.getClientId(FacesContext
096: .getCurrentInstance())
097: + ", please check if the proper [type] has been defined");
098: }
099: }
100:
101: private Map generatedImageMapArea = new HashMap();
102:
103: private void generateMap(Chart chart) throws Throwable {
104: chart.renderWithImageMap();
105: generatedImageMapArea.clear();
106: ImageMap imageMap = chart.getImageMap();
107: Iterator iterator = imageMap.getIterator();
108: while (iterator.hasNext()) {
109: ImageMapArea mapArea = (ImageMapArea) iterator.next();
110: generatedImageMapArea.put(mapArea.hashCode() + "", mapArea);
111: }
112: }
113:
114: protected abstract void buildChart() throws Throwable;
115:
116: static AbstractChart createChart(UIComponent uiComponent)
117: throws Throwable {
118: String type = ((OutputChart) uiComponent).getType();
119: if (OutputChart.PIE2D_CHART_TYPE.equalsIgnoreCase(type)
120: || OutputChart.PIE3D_CHART_TYPE.equalsIgnoreCase(type)) {
121: return new PieChart(uiComponent);
122: } else {
123: return new AxisChart(uiComponent);
124: }
125: }
126:
127: public Chart getChart() {
128: if (userDefinedChart != null) {
129: return userDefinedChart;
130: }
131: return chart;
132: }
133:
134: public void setChart(Chart userDefinedChart) {
135: this .userDefinedChart = userDefinedChart;
136: }
137:
138: public String[] getAsStringArray(Object obj) {
139: if (obj instanceof String[]) {
140: return (String[]) obj;
141: } else if (obj instanceof String) {
142: return ((String) obj).split(",");
143: } else if (obj instanceof List) {
144: return (String[]) ((List) obj).toArray(new String[0]);
145: } else {
146: return null;
147: }
148: }
149:
150: public double[][] getAs2dDoubleArray(Object obj) {
151: double[][] dbl2DArray = null;
152: if (obj instanceof double[][]) {
153: dbl2DArray = (double[][]) obj;
154: } else if (obj instanceof String) {
155: String[] temp = ((String) obj).split(":");
156: dbl2DArray = new double[temp.length][];
157: for (int i = 0; i < temp.length; i++) {
158: dbl2DArray[i] = getAsDoubleArray(temp[i]);
159: }
160: } else if (obj instanceof List) {
161: List list = (List) obj;
162: double[] outer = (double[]) list.get(0);
163: dbl2DArray = new double[outer.length][list.size()];
164: for (int j = 0; j < list.size(); j++) {
165: for (int i = 0; i < outer.length; i++) {
166: outer = (double[]) list.get(j);
167: dbl2DArray[i][j] = outer[i];
168: }
169: }
170: }
171: return dbl2DArray;
172: }
173:
174: public double[] getAsDoubleArray(Object obj) {
175: double[] dblArray = null;
176: if (obj instanceof String) {
177: String[] temp = ((String) obj).split(",");
178: dblArray = new double[temp.length];
179: for (int i = 0; i < temp.length; i++) {
180: dblArray[i] = Double.parseDouble(temp[i]);
181: }
182: } else if (obj instanceof List) {
183: List objList = (List) obj;
184: dblArray = new double[objList.size()];
185: for (int i = 0; i < objList.size(); i++) {
186: dblArray[i] = ((Double) objList.get(i)).doubleValue();
187: }
188: } else if (obj instanceof double[]) {
189: dblArray = (double[]) obj;
190: }
191: return dblArray;
192: }
193:
194: Paint[] paintArray = null;
195:
196: public Paint[] getAsPaintArray(Object obj) {
197: if (obj instanceof String) {
198: if (paintArray != null) {
199: return paintArray;
200: }
201: String[] temp = ((String) obj).split(",");
202: paintArray = new Paint[temp.length];
203: for (int i = 0; i < temp.length; i++) {
204: paintArray[i] = colorMap.getColor(temp[i].trim());
205: }
206: } else if (obj instanceof List) {
207: List objList = (List) obj;
208: paintArray = new Paint[objList.size()];
209: for (int i = 0; i < objList.size(); i++) {
210: paintArray[i] = (Paint) objList.get(i);
211: }
212: } else if (obj instanceof String[]) {
213: String[] colors = (String[]) obj;
214: paintArray = new Paint[colors.length];
215: for (int i = 0; i < colors.length; i++) {
216: paintArray[i] = colorMap.getColor(colors[i]);
217: }
218: }
219: return paintArray;
220: }
221:
222: public Shape[] getAsShapeArray(Object obj) {
223: Shape[] shapeArray = null;
224: if (obj instanceof String) {
225: String[] temp = ((String) obj).split(",");
226: shapeArray = new Shape[temp.length];
227: for (int i = 0; i < temp.length; i++) {
228: shapeArray[i] = shapeMap.getShape(temp[i].trim());
229: }
230: } else if (obj instanceof List) {
231: List objList = (List) obj;
232: shapeArray = new Shape[objList.size()];
233: for (int i = 0; i < objList.size(); i++) {
234: shapeArray[i] = (Shape) objList.get(i);
235: }
236: }
237: return shapeArray;
238: }
239:
240: Shape[] getGeneratedShapes(int count) {
241: Shape[] tempShapeArray = new Shape[count];
242: Iterator it = shapeMap.values().iterator();
243: for (int i = 0; i < count; i++) {
244: if (it.hasNext()) {
245: tempShapeArray[i] = (Shape) it.next();
246: } else {
247: it = shapeMap.values().iterator();
248: tempShapeArray[i] = (Shape) it.next();
249: }
250: }
251: return tempShapeArray;
252: }
253:
254: String[] getGeneratedLabels(String label, int count) {
255: String[] tempStringArray = new String[count];
256: for (int i = 0; i < count; i++) {
257: tempStringArray[i] = label + " " + i;
258: }
259: return tempStringArray;
260: }
261:
262: public Paint[] getPaints(Object obj, int count) {
263: if (obj == null && paintArray == null) {
264: return paintArray = TestDataGenerator
265: .getRandomPaints(count);
266: } else if (obj == null && paintArray != null) {
267: return paintArray;
268: } else {
269: return getAsPaintArray(obj);
270: }
271: }
272:
273: public Map getGeneratedImageMapArea() {
274: return generatedImageMapArea;
275: }
276:
277: public ImageMapArea getClickedImageMapArea() {
278: return clickedImageMapArea;
279: }
280:
281: public void setClickedImageMapArea(ImageMapArea clickedImageMapArea) {
282: this .clickedImageMapArea = clickedImageMapArea;
283: }
284:
285: public static Color getColor(String color) {
286: return colorMap.getColor(color);
287: }
288:
289: public LegendProperties getLegendProperties() {
290: String legendPlacement = (String) outputChart
291: .getLegendPlacement();
292: if (legendPlacement.equals("none")) {
293: return null;
294: }
295: LegendProperties legendProperties = new LegendProperties();
296: legendProperties.setPlacement(legendPlacementMap
297: .getLegendPlacement(legendPlacement));
298: Object legendColumns = outputChart.getLegendColumns();
299: if (legendColumns instanceof Integer) {
300: legendProperties.setNumColumns(((Integer) outputChart
301: .getLegendColumns()).intValue());
302: } else if (legendColumns instanceof String) {
303: legendProperties.setNumColumns(Integer.parseInt(outputChart
304: .getLegendColumns().toString()));
305: }
306: return legendProperties;
307: }
308:
309: public File getImageFile() {
310: return imageFile;
311: }
312:
313: public void setImageFile(File imageFile) {
314: this .imageFile = imageFile;
315: }
316: }
317:
318: class ColorMap extends HashMap {
319: private static final long serialVersionUID = 1L;
320:
321: public ColorMap() {
322: this .put("black", Color.BLACK);
323: this .put("blue", Color.BLUE);
324: this .put("cyan", Color.CYAN);
325: this .put("darkGray", Color.DARK_GRAY);
326: this .put("gray", Color.GRAY);
327: this .put("green", Color.GREEN);
328: this .put("lightGray", Color.LIGHT_GRAY);
329: this .put("magenta", Color.MAGENTA);
330: this .put("orange", Color.ORANGE);
331: this .put("pink", Color.PINK);
332: this .put("red", Color.RED);
333: this .put("white", Color.WHITE);
334: this .put("yellow", Color.YELLOW);
335: }
336:
337: public Color getColor(String key) {
338: return (Color) super .get(key);
339: }
340: }
341:
342: class ShapeMap extends HashMap {
343:
344: private static final long serialVersionUID = 1L;
345:
346: public ShapeMap() {
347: this .put("circle", PointChartProperties.SHAPE_CIRCLE);
348: this .put("diamond", PointChartProperties.SHAPE_DIAMOND);
349: this .put("square", PointChartProperties.SHAPE_SQUARE);
350: this .put("triangle", PointChartProperties.SHAPE_TRIANGLE);
351: }
352:
353: public Shape getShape(String key) {
354: return (Shape) super .get(key);
355: }
356: }
357:
358: class LegendPlacementMap extends HashMap {
359: public LegendPlacementMap() {
360: this .put("top", new Integer(LegendProperties.TOP));
361: this .put("bottom", new Integer(LegendProperties.BOTTOM));
362: this .put("left", new Integer(LegendProperties.LEFT));
363: this .put("right", new Integer(LegendProperties.RIGHT));
364: }
365:
366: public int getLegendPlacement(String key) {
367: if (!super .containsKey(key)) {
368: return 0;
369: }
370: return Integer.parseInt(super.get(key).toString());
371: }
372: }
|