KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > charts > PieChartBean


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.icefaces.samples.showcase.components.charts;
35
36 import com.icesoft.faces.component.outputchart.OutputChart;
37 import com.icesoft.faces.context.effects.Effect;
38 import com.icesoft.faces.context.effects.Highlight;
39
40 import java.awt.*;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * The PieChartBean is responsible for holding all the backing information and
48  * data for the pie chart
49  *
50  * @since 1.5
51  */

52 public class PieChartBean extends Chart {
53
54     //list of labels for the chart
55
protected static List JavaDoc labels = new ArrayList JavaDoc();
56
57     //list of the data used by the chart
58
protected static List JavaDoc data = new ArrayList JavaDoc();
59
60     //list of the colors used in the pie chart
61
private List JavaDoc paints;
62
63     //all sales
64
private static List JavaDoc allSales = new ArrayList JavaDoc();
65
66     //list of the saled data from the sales class
67
private static final List JavaDoc sales = buildSales();
68
69     //a map of the sales data
70
private static Map JavaDoc salesMap;
71
72     //
73
private static String JavaDoc clickedAreaValue = ChartMediator.DEFAULT_STRING;
74
75     //a temporary string for the current label
76
private String JavaDoc label;
77
78     private float value;
79
80     //flag to determine if the chart is a 3D pie
81
public static boolean is3D = false;
82
83     //flag to determine if the graph needs rendering
84
private boolean pieNeedsRendering = false;
85
86     //the temporary value for the selected color
87
private Color selectedColor;
88
89     //the highlight effect for when the value selected is changed
90
private static Effect effectOutputText = new Highlight("#ffff99");
91
92     //index to delete from
93
int deletInex = 0;
94
95     //list of items to delete
96
private List JavaDoc deleteList = new ArrayList JavaDoc();
97
98
99
100
101     public PieChartBean() {
102         super();
103         paints = new ArrayList JavaDoc();
104         paints.add(new Color(26, 86, 138)); //#1A568A
105
paints.add(new Color(76, 126, 167)); //#4C7EA7
106
paints.add(new Color(148, 179, 203)); //#94B3CB
107
paints.add(new Color(193, 211, 223)); //#C1D3DF
108
}
109
110
111
112
113     /**
114      * Method to build the sales list and create the chart using the data from
115      * the sales class
116      *
117      * @return list of sales items for charting.
118      */

119     public static List JavaDoc buildSales() {
120         ArrayList JavaDoc salesTemp = new ArrayList JavaDoc();
121
122         salesMap = Sales.getSales();
123
124         Iterator JavaDoc it = salesMap.values().iterator();
125         double price;
126         String JavaDoc label;
127         while (it.hasNext()) {
128
129             Sales[] yearSale = (Sales[]) it.next();
130             price = 0;
131             label = "";
132             for (int i = 0; i < yearSale.length; i++) {
133                 price += (yearSale[i]).getPrice();
134                 label = (yearSale[i]).getYear();
135                 salesTemp.add(yearSale[i]);
136                 allSales.add(yearSale[i]);
137
138             }
139             labels.add(label);
140             data.add(new Double JavaDoc(price));
141
142         }
143         return salesTemp;
144     }
145
146
147     /**
148      * Method to call the rendering of the chart based on the pieNeedsRendering
149      * flag
150      *
151      * @param component chart component which will be rendered.
152      * @return boolean true if OutputChart should be re-rendered; otherwise,
153      * false.
154      */

155     public boolean renderOnSubmit(OutputChart component) {
156         if (pieNeedsRendering) {
157             pieNeedsRendering = false;
158             return true;
159         } else {
160             return false;
161         }
162     }
163
164     public static void setIs3D(boolean i3D) {
165         is3D = i3D;
166     }
167
168     /**
169      * Method to return whether chart is 3D or not
170      *
171      * @return boolean
172      */

173     public static boolean is3D() {
174         return is3D;
175     }
176
177
178     public String JavaDoc getLabel() {
179         return label;
180     }
181
182     public void setLabel(String JavaDoc label) {
183         if (null == label || label.length() < 1) {
184             label = " ";
185         }
186         this.label = label;
187     }
188
189     public float getValue() {
190         return value;
191     }
192
193     public void setValue(float value) {
194         this.value = value;
195     }
196
197
198     public String JavaDoc getClickedAreaValue() {
199         return clickedAreaValue;
200     }
201
202     public void setClickedAreaValue(String JavaDoc clickedAreaValue) {
203         PieChartBean.clickedAreaValue = clickedAreaValue;
204     }
205
206
207     public Effect getEffectOutputText() {
208         return effectOutputText;
209     }
210
211     public void setEffectOutputText(Effect effectOutputText) {
212         PieChartBean.effectOutputText = effectOutputText;
213     }
214
215
216     /**
217      * Method to set the displayed table data to that corresponding with the
218      * year clicked
219      *
220      * @param year clicked
221      */

222     public static void setSalesForYear(String JavaDoc year) {
223         sales.clear();
224         Sales[] yearSales = (Sales[]) salesMap.get(year);
225         for (int i = 0; i < yearSales.length; i++) {
226             sales.add(yearSales[i]);
227         }
228     }
229
230     public List JavaDoc getSales() {
231         return type.equalsIgnoreCase("pie3d") ? allSales : sales;
232     }
233
234
235     public List JavaDoc getData() {
236         return data;
237     }
238
239     public List JavaDoc getLabels() {
240         return labels;
241     }
242
243     public List JavaDoc getPaints() {
244         return paints;
245     }
246
247     public void setPaints(List JavaDoc paints) {
248         this.paints = paints;
249     }
250 }
251
Popular Tags