KickJava   Java API By Example, From Geeks To Geeks.

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


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.chartData.AxisChartDataSet;
37 import org.krysalis.jcharts.chartData.DataSeries;
38 import org.krysalis.jcharts.properties.AreaChartProperties;
39 import org.krysalis.jcharts.properties.AxisProperties;
40 import org.krysalis.jcharts.properties.BarChartProperties;
41 import org.krysalis.jcharts.properties.ChartProperties;
42 import org.krysalis.jcharts.properties.ChartTypeProperties;
43 import org.krysalis.jcharts.properties.ClusteredBarChartProperties;
44 import org.krysalis.jcharts.properties.LegendProperties;
45 import org.krysalis.jcharts.properties.LineChartProperties;
46 import org.krysalis.jcharts.properties.PointChartProperties;
47 import org.krysalis.jcharts.properties.StackedAreaChartProperties;
48 import org.krysalis.jcharts.properties.StackedBarChartProperties;
49 import org.krysalis.jcharts.test.TestDataGenerator;
50 import org.krysalis.jcharts.types.ChartType;
51
52 import javax.faces.component.UIComponent;
53 import java.awt.*;
54
55 public class AxisChart extends AbstractChart {
56
57     public AxisChart(UIComponent uiComponent) throws Throwable JavaDoc {
58         super(uiComponent);
59     }
60
61     protected void buildChart() throws Throwable JavaDoc {
62         if (data == null) {
63             getData(outputChart.getData());
64         }
65         if (type.equalsIgnoreCase(OutputChart.AREA_CHART_TYPE)) {
66             buildAreaChart();
67         } else if (type.equalsIgnoreCase(OutputChart.AREA_STACKED_CHART_TYPE)) {
68             buildAreaStackedChart();
69         } else if (type.equalsIgnoreCase(OutputChart.BAR_CHART_TYPE)) {
70             buildBarChart();
71         } else if (type.equalsIgnoreCase(OutputChart.BAR_STACKED_CHART_TYPE)) {
72             buildBarStackedChart();
73         } else
74         if (type.equalsIgnoreCase(OutputChart.BAR_CLUSTERED_CHART_TYPE)) {
75             buildBarClusteredChart();
76         } else if (type.equalsIgnoreCase(OutputChart.LINE_CHART_TYPE)) {
77             buildLineChart();
78         } else if (type.equalsIgnoreCase(OutputChart.POINT_CHART_TYPE)) {
79             buildPointChart();
80         }
81     }
82
83     private void buildAreaChart() throws Throwable JavaDoc {
84         AreaChartProperties areaChartProperties = new AreaChartProperties();
85         buildAxisChart(ChartType.AREA, areaChartProperties);
86     }
87
88     private void buildAreaStackedChart() throws Throwable JavaDoc {
89         StackedAreaChartProperties areaChartProperties =
90                 new StackedAreaChartProperties();
91         buildAxisChart(ChartType.AREA_STACKED, areaChartProperties);
92     }
93
94     private void buildBarChart() throws Throwable JavaDoc {
95         BarChartProperties barChartProperties = new BarChartProperties();
96         buildAxisChart(ChartType.BAR, barChartProperties);
97     }
98
99     private void buildBarStackedChart() throws Throwable JavaDoc {
100         StackedBarChartProperties barChartProperties =
101                 new StackedBarChartProperties();
102         buildAxisChart(ChartType.BAR_STACKED, barChartProperties);
103     }
104
105     private void buildBarClusteredChart() throws Throwable JavaDoc {
106         ClusteredBarChartProperties barChartProperties =
107                 new ClusteredBarChartProperties();
108         buildAxisChart(ChartType.BAR_CLUSTERED, barChartProperties);
109     }
110
111     private void buildLineChart() throws Throwable JavaDoc {
112         Stroke[] strokes = new Stroke[data.length];
113         for (int i = 0; i < data.length; i++) {
114             strokes[i] = LineChartProperties.DEFAULT_LINE_STROKE;
115         }
116         LineChartProperties lineChartProperties = new LineChartProperties(
117                 strokes, getShapes(outputChart.getShapes()));
118         buildAxisChart(ChartType.LINE, lineChartProperties);
119     }
120
121     private void buildPointChart() throws Throwable JavaDoc {
122         Paint[] outlinePaints = TestDataGenerator.getRandomPaints(data.length);
123         boolean[] fillPointFlags = new boolean[data.length];
124         for (int i = 0; i < data.length; i++) {
125             fillPointFlags[i] = true;
126         }
127         PointChartProperties pointChartProperties = new PointChartProperties(
128                 getShapes(outputChart.getShapes()), fillPointFlags,
129                 outlinePaints);
130         buildAxisChart(ChartType.POINT, pointChartProperties);
131     }
132
133     void buildAxisChart(ChartType chartType,
134                         ChartTypeProperties chartTypeProperties)
135             throws Throwable JavaDoc {
136         DataSeries dataSeries = new DataSeries(
137                 getAsXaxisLabelsArray(outputChart.getXaxisLabels()),
138                 outputChart.getXaxisTitle(),
139                 outputChart.getYaxisTitle(),
140                 outputChart.getChartTitle());
141
142         AxisChartDataSet axisChartDataSet = new AxisChartDataSet(
143                 getAs2dDoubleArray(outputChart.getData()),
144                 getAsLabelsArray(outputChart.getLabels()),
145                 getPaints(outputChart.getColors()),
146                 chartType,
147                 chartTypeProperties);
148
149         AxisProperties axisProperties = ((chartType.equals(ChartType.BAR) ||
150                 chartType.equals(ChartType.BAR_CLUSTERED)) &&
151                 outputChart.isHorizontal())?
152                 new AxisProperties(true): new AxisProperties();
153
154         dataSeries.addIAxisPlotDataSet(axisChartDataSet);
155         chart = new org.krysalis.jcharts.axisChart.AxisChart(dataSeries,
156                                                              new ChartProperties(),
157                                                              axisProperties,
158                                                              getLegendProperties(),
159                                                              new Integer JavaDoc(
160                                                                      outputChart.getWidth()).intValue(),
161                                                              new Integer JavaDoc(
162                                                                      outputChart.getHeight()).intValue());
163     }
164
165     private Shape[] shapes;
166
167     private Shape[] getShapes(Object JavaDoc obj) {
168         if (obj == null && shapes == null) {
169             return shapes = getGeneratedShapes(data.length);
170         } else if (obj == null && shapes != null) {
171             return shapes;
172         } else {
173             return shapes = getAsShapeArray(obj);
174         }
175     }
176
177     String JavaDoc[] xaxisLabels = null;
178
179     public String JavaDoc[] getAsXaxisLabelsArray(Object JavaDoc obj) {
180         if (obj == null && xaxisLabels == null) {
181             return xaxisLabels = getGeneratedLabels("Xlabel", data[0].length);
182         } else if (obj == null && xaxisLabels != null) {
183             return xaxisLabels;
184         } else {
185             return getAsStringArray(obj);
186         }
187     }
188
189     String JavaDoc[] labels = null;
190
191     public String JavaDoc[] getAsLabelsArray(Object JavaDoc obj) {
192         if (obj == null && labels == null) {
193             return labels = getGeneratedLabels("Label", data.length);
194         } else if (obj == null && labels != null) {
195             return labels;
196         } else {
197             return getAsStringArray(obj);
198         }
199     }
200
201     private double[][] data = null;
202
203     public double[][] getData(Object JavaDoc obj) {
204         if (obj instanceof String JavaDoc && data != null) {
205             return data;
206         } else {
207             return data = getAs2dDoubleArray(obj);
208         }
209     }
210
211     public Paint[] getPaints(Object JavaDoc obj) {
212         return getPaints(obj, data.length);
213     }
214 }
215
Popular Tags