KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.krysalis.jcharts.Chart;
37 import org.krysalis.jcharts.chartData.PieChartDataSet;
38 import org.krysalis.jcharts.nonAxisChart.PieChart2D;
39 import org.krysalis.jcharts.nonAxisChart.PieChart3D;
40 import org.krysalis.jcharts.properties.ChartProperties;
41 import org.krysalis.jcharts.properties.LegendProperties;
42 import org.krysalis.jcharts.properties.PieChart2DProperties;
43 import org.krysalis.jcharts.properties.PieChart3DProperties;
44 import org.krysalis.jcharts.properties.util.ChartStroke;
45 import org.krysalis.jcharts.types.PieLabelType;
46
47 import javax.faces.component.UIComponent;
48 import java.awt.*;
49
50 public class PieChart extends AbstractChart {
51
52     public PieChart(UIComponent outputChart) throws Throwable JavaDoc {
53         super(outputChart);
54     }
55
56     protected void buildChart() {
57         try {
58             if (type.equalsIgnoreCase("pie2d")) {
59                 chart = getPie2dChart();
60             } else if (type.equalsIgnoreCase("pie3d")) {
61                 chart = getPie3dChart();
62             }
63         } catch (Throwable JavaDoc e) {
64             e.printStackTrace();
65         }
66     }
67
68     double data[] = null;
69
70     public double[] getData(Object JavaDoc obj) {
71         if (data != null && obj instanceof String JavaDoc) {
72             return data;
73         }
74         return data = super.getAsDoubleArray(obj);
75     }
76
77     String JavaDoc[] labels = null;
78
79     public String JavaDoc[] getLabels(Object JavaDoc obj) {
80         if (obj == null && labels == null) {
81             return labels = getGeneratedLabels("label", data.length);
82         } else if ((obj == null || obj instanceof String JavaDoc) && labels != null) {
83             return labels;
84         } else {
85             return labels = getAsStringArray(obj);
86         }
87     }
88
89     public Paint[] getPaints(Object JavaDoc obj) {
90         return getPaints(obj, data.length);
91     }
92
93     private Chart getPie2dChart() throws Throwable JavaDoc {
94         PieChart2DProperties pieChart2DProperties = new PieChart2DProperties();
95         PieChartDataSet pieChartDataSet;
96         pieChartDataSet = new PieChartDataSet(outputChart.getChartTitle(),
97                                               getData(outputChart.getData()),
98                                               getLabels(
99                                                       outputChart.getLabels()),
100                                               getPaints(
101                                                       outputChart.getColors()),
102                                               pieChart2DProperties);
103
104         return new PieChart2D(pieChartDataSet,
105                               getLegendProperties(),
106                               new ChartProperties(),
107                               new Integer JavaDoc(outputChart.getWidth()).intValue(),
108                               new Integer JavaDoc(outputChart.getHeight()).intValue());
109
110     }
111
112     private Chart getPie3dChart() throws Throwable JavaDoc {
113         PieChart3DProperties pieChart3DProperties = new PieChart3DProperties();
114         pieChart3DProperties.setDepth(30);
115         pieChart3DProperties.setBorderChartStroke(
116                 new ChartStroke(new BasicStroke(1f), Color.black));
117         pieChart3DProperties.setPieLabelType(PieLabelType.LEGEND_LABELS);
118         PieChartDataSet pieChartDataSet =
119                 new PieChartDataSet(outputChart.getChartTitle(),
120                                     getData(outputChart.getData()),
121                                     getLabels(outputChart.getLabels()),
122                                     getPaints(outputChart.getColors()),
123                                     pieChart3DProperties);
124         return new PieChart3D(pieChartDataSet, null, new ChartProperties(),
125                               new Integer JavaDoc(outputChart.getWidth()).intValue(),
126                               new Integer JavaDoc(outputChart.getHeight()).intValue());
127     }
128 }
129
Popular Tags