KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractChart;
37 import com.icesoft.faces.component.outputchart.OutputChart;
38
39 import javax.faces.event.ActionEvent;
40 import javax.faces.event.ValueChangeEvent;
41 import javax.faces.model.SelectItem;
42 import java.awt.*;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46
47 /**
48  * The DynamicPieChartBean is responsible for holding all the backing
49  * information and data for the dynamic pie chart
50  *
51  * @since 1.5
52  */

53 public class DynamicPieChart {
54
55     //list of labels for the chart
56
public List JavaDoc labels = new ArrayList JavaDoc();
57
58     //list of the data used by the chart
59
public List JavaDoc data = new ArrayList JavaDoc();
60
61     //list of the colors used in the pie chart
62
private List JavaDoc paints = new ArrayList JavaDoc();
63
64     //a temporary string for the current label
65
private String JavaDoc label;
66
67     private float value;
68
69     //flag to determine if the chart is a 3D pie
70
public boolean is3D = false;
71
72     //flag to determine if the graph needs rendering
73
private boolean pieNeedsRendering = false;
74
75     //the temporary value for the selected color
76
private Color selectedColor;
77
78     //index to delete from
79
int deletInex = 0;
80
81     //list of items to delete
82
private List JavaDoc deleteList = new ArrayList JavaDoc();
83
84     //array of the available paints used in the chart
85
public static final SelectItem[] availablePaints = new SelectItem[]{
86
87             new SelectItem("E6EDF2", "Blue 1"),
88             new SelectItem("CAE1EF", "Blue 2"),
89             new SelectItem("C1D3DF", "Blue 3"),
90             new SelectItem("B4C7D4", "Blue 4"),
91             new SelectItem("94B3CB", "Blue 5"),
92             new SelectItem("4C7EA7", "Blue 6"),
93             new SelectItem("4FAADC", "Blue 7"),
94             new SelectItem("4397C5", "Blue 8"),
95             new SelectItem("1A568A", "Blue 9"),
96             new SelectItem("0D4274", "Blue 10"),
97             new SelectItem("CCCCCC", "Grey 1"),
98             new SelectItem("ACACAC", "Grey 2"),
99             new SelectItem("F78208", "Orange"),
100             new SelectItem("000000", "Black")};
101
102
103     /**
104      * Method to build the sales list and create the chart using the data from
105      * the sales class
106      */

107     public DynamicPieChart() {
108         Iterator JavaDoc it = Sales.getSales().values().iterator();
109         double price;
110         String JavaDoc label;
111         int r = 3;
112         while (it.hasNext()) {
113
114             Sales[] yearSale = (Sales[]) it.next();
115             price = 0;
116             label = "";
117             for (int i = 0; i < yearSale.length; i++) {
118                 price += (yearSale[i]).getPrice();
119                 label = (yearSale[i]).getYear();
120
121             }
122             labels.add(label);
123             data.add(new Double JavaDoc(price));
124             //adds paint from availablePaints list
125
paints.add(new Color(Integer.parseInt(
126                     (String JavaDoc) availablePaints[r].getValue(), 16)));
127             r++;
128         }
129
130     }
131
132
133     /**
134      * Method to call the rendering of the chart based on the pieNeedsRendering
135      * flag
136      *
137      * @param component chart component which will be rendered.
138      * @return boolean true if OutputChart should be re-rendered; otherwise,
139      * false.
140      */

141     public boolean renderOnSubmit(OutputChart component) {
142         if (pieNeedsRendering) {
143             pieNeedsRendering = false;
144             return true;
145         } else {
146             return false;
147         }
148     }
149
150     public void setIs3D(boolean i3D) {
151         this.is3D = i3D;
152     }
153
154     /**
155      * Method to return whether chart is 3D or not
156      *
157      * @return boolean
158      */

159     public boolean is3D() {
160         return is3D;
161     }
162
163
164     public SelectItem[] getAvailablePaints() {
165         return availablePaints;
166     }
167
168
169     /**
170      * Method to listen for the change in color in the graph
171      *
172      * @param event JSF value changed event
173      */

174     public void paintChangeListener(ValueChangeEvent event) {
175
176         if (event.getNewValue() != null) {
177             selectedColor =
178                     new Color(
179                             Integer.parseInt(
180                                     event.getNewValue().toString(), 16));
181             System.out.println("Hex Color: " +
182                         Integer.parseInt(
183                                     event.getNewValue().toString(), 16));
184         }
185     }
186
187
188     public String JavaDoc getLabel() {
189         return label;
190     }
191
192     public void setLabel(String JavaDoc label) {
193         if (null == label || label.length() < 1) {
194             label = " ";
195         }
196         this.label = label;
197     }
198
199     public float getValue() {
200         return value;
201     }
202
203     public void setValue(float value) {
204         this.value = value;
205     }
206
207     /**
208      * Method to add a value and a color to the chart
209      *
210      * @param event JSF action event.
211      */

212     public void addToChart(ActionEvent event) {
213
214
215         paints.add(selectedColor);
216
217         labels.add(label);
218
219         data.add(new Double JavaDoc(value));
220
221         pieNeedsRendering = true;
222
223     }
224
225
226     public List JavaDoc getDeleteList() {
227         deleteList.clear();
228         deleteList.add(new SelectItem("-1", "Select..."));
229         for (int i = 0; i < labels.size(); i++) {
230             deleteList.add(new SelectItem("" + i, "" + labels.get(i)));
231         }
232         return deleteList;
233     }
234
235     public void setDeleteList(List JavaDoc deleteList) {
236         this.deleteList = deleteList;
237     }
238
239     public boolean isDeleteAllowed() {
240         return labels.size() > 2;
241     }
242
243
244     /**
245      * Method to listen for an action to delete from the chart
246      *
247      * @param event JSF value changed event
248      */

249     public void deleteListValueChangeListener(ValueChangeEvent event) {
250         if (event.getNewValue() != null) {
251             deletInex = Integer.parseInt(event.getNewValue().toString());
252         }
253     }
254
255     /**
256      * Method to delete an item from the chart
257      *
258      * @param event JSF action event
259      */

260     public void deleteChart(ActionEvent event) {
261         if (deletInex >= 0 && labels.size() > 2) {
262             labels.remove(deletInex);
263             data.remove(deletInex);
264             paints.remove(deletInex);
265             pieNeedsRendering = true;
266         }
267     }
268
269
270     public List JavaDoc getData() {
271         return data;
272     }
273
274     public List JavaDoc getLabels() {
275
276         return labels;
277     }
278
279     public List JavaDoc getPaints() {
280
281         return paints;
282     }
283
284     public void setPaints(List JavaDoc paints) {
285
286
287         this.paints = paints;
288
289     }
290 }
291
292
Popular Tags