KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > outputchart > AbstractChart


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.outputchart;
35
36 import java.awt.Color JavaDoc;
37 import java.awt.Paint JavaDoc;
38 import java.awt.Shape JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
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 JavaDoc type = null;
68
69     public AbstractChart(UIComponent uiComponent) throws Throwable JavaDoc {
70         this.outputChart = (OutputChart) uiComponent;
71         this.type = outputChart.getType();
72     }
73
74     public void encode() throws Throwable JavaDoc {
75         //if type is dynamic here we should update it
76
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 JavaDoc 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 JavaDoc generatedImageMapArea = new HashMap JavaDoc();
97
98     private void generateMap(Chart chart) throws Throwable JavaDoc {
99         chart.renderWithImageMap();
100         generatedImageMapArea.clear();
101         ImageMap imageMap = chart.getImageMap();
102         Iterator JavaDoc 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 JavaDoc;
110
111     static AbstractChart createChart(UIComponent uiComponent) throws Throwable JavaDoc {
112         String JavaDoc 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 JavaDoc[] getAsStringArray(Object JavaDoc obj) {
133         if (obj instanceof String JavaDoc[]) {
134             return (String JavaDoc[]) obj;
135         } else if (obj instanceof String JavaDoc) {
136             return ((String JavaDoc) obj).split(",");
137         } else if (obj instanceof List JavaDoc) {
138             return (String JavaDoc[]) ((List JavaDoc) obj).toArray(new String JavaDoc[0]);
139         }else {
140             return null;
141         }
142     }
143
144
145     public double[][] getAs2dDoubleArray(Object JavaDoc obj) {
146         double[][] dbl2DArray = null;
147         if (obj instanceof double[][]) {
148             dbl2DArray = (double[][]) obj;
149         } else if (obj instanceof String JavaDoc) {
150             String JavaDoc[] temp = ((String JavaDoc) 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 JavaDoc) {
156             List JavaDoc list = (List JavaDoc) 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 JavaDoc obj) {
171         double[] dblArray = null;
172         if (obj instanceof String JavaDoc) {
173             String JavaDoc[] temp = ((String JavaDoc) 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 JavaDoc) {
179             List JavaDoc objList = (List JavaDoc) obj;
180             dblArray = new double[objList.size()];
181             for (int i = 0; i < objList.size(); i++) {
182                 dblArray[i] = ((Double JavaDoc) objList.get(i)).doubleValue();
183             }
184         } else if (obj instanceof double[]) {
185             dblArray = (double[]) obj;
186         }
187         return dblArray;
188     }
189
190     Paint JavaDoc[] paintArray = null;
191
192     public Paint JavaDoc[] getAsPaintArray(Object JavaDoc obj) {
193         if (obj instanceof String JavaDoc) {
194             if (paintArray != null) {
195                 return paintArray;
196             }
197             String JavaDoc[] temp = ((String JavaDoc) obj).split(",");
198             paintArray = new Paint JavaDoc[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 JavaDoc) {
203             List JavaDoc objList = (List JavaDoc) obj;
204             paintArray = new Paint JavaDoc[objList.size()];
205             for (int i = 0; i < objList.size(); i++) {
206                 paintArray[i] = (Paint JavaDoc) objList.get(i);
207             }
208         }
209         return paintArray;
210     }
211
212
213     public Shape JavaDoc[] getAsShapeArray(Object JavaDoc obj) {
214         Shape JavaDoc[] shapeArray = null;
215         if (obj instanceof String JavaDoc) {
216             String JavaDoc[] temp = ((String JavaDoc) obj).split(",");
217             shapeArray = new Shape JavaDoc[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 JavaDoc) {
222             List JavaDoc objList = (List JavaDoc) obj;
223             shapeArray = new Shape JavaDoc[objList.size()];
224             for (int i = 0; i < objList.size(); i++) {
225                 shapeArray[i] = (Shape JavaDoc) objList.get(i);
226             }
227         }
228         return shapeArray;
229     }
230
231     Shape JavaDoc[] getGeneratedShapes(int count) {
232         Shape JavaDoc[] tempShapeArray = new Shape JavaDoc[count];
233         Iterator JavaDoc it = shapeMap.values().iterator();
234         for (int i = 0; i < count; i++) {
235             if (it.hasNext()) {
236                 tempShapeArray[i] = (Shape JavaDoc) it.next();
237             } else {
238                 it = shapeMap.values().iterator();
239                 tempShapeArray[i] = (Shape JavaDoc) it.next();
240             }
241         }
242         return tempShapeArray;
243     }
244
245     String JavaDoc[] getGeneratedLabels(String JavaDoc label, int count) {
246         String JavaDoc[] tempStringArray = new String JavaDoc[count];
247         for (int i = 0; i < count; i++) {
248             tempStringArray[i] = label + " " + i;
249         }
250         return tempStringArray;
251     }
252
253     public Paint JavaDoc[] getPaints(Object JavaDoc 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 JavaDoc 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 JavaDoc getColor(String JavaDoc 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 JavaDoc legendColumns = outputChart.getLegendColumns();
284         if (legendColumns instanceof Integer JavaDoc) {
285             legendProperties.setNumColumns(((Integer JavaDoc)outputChart.getLegendColumns()).intValue());
286         }else if (legendColumns instanceof String JavaDoc) {
287             legendProperties.setNumColumns(Integer.parseInt(outputChart.getLegendColumns().toString()));
288         }
289         return legendProperties;
290     }
291 }
292
293 class ColorMap extends HashMap JavaDoc {
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 JavaDoc getColor(String JavaDoc key) {
313         return (Color JavaDoc) super.get(key);
314     }
315 }
316
317 class ShapeMap extends HashMap JavaDoc {
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 JavaDoc getShape(String JavaDoc key) {
329         return (Shape JavaDoc) super.get(key);
330     }
331 }
332
333 class LegendPlacementMap extends HashMap JavaDoc {
334     public LegendPlacementMap() {
335         this.put("top", new Integer JavaDoc(LegendProperties.TOP));
336         this.put("bottom", new Integer JavaDoc(LegendProperties.BOTTOM));
337         this.put("left", new Integer JavaDoc(LegendProperties.LEFT));
338         this.put("right", new Integer JavaDoc(LegendProperties.RIGHT));
339     }
340     
341     public int getLegendPlacement(String JavaDoc key) {
342         if (!super.containsKey(key)) {
343             return 0;
344         }
345         return Integer.parseInt(super.get(key).toString());
346     }
347 }
Popular Tags