001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.outputchart;
035:
036: import org.krysalis.jcharts.chartData.AxisChartDataSet;
037: import org.krysalis.jcharts.chartData.DataSeries;
038: import org.krysalis.jcharts.properties.AreaChartProperties;
039: import org.krysalis.jcharts.properties.AxisProperties;
040: import org.krysalis.jcharts.properties.BarChartProperties;
041: import org.krysalis.jcharts.properties.ChartProperties;
042: import org.krysalis.jcharts.properties.ChartTypeProperties;
043: import org.krysalis.jcharts.properties.ClusteredBarChartProperties;
044: import org.krysalis.jcharts.properties.LegendProperties;
045: import org.krysalis.jcharts.properties.LineChartProperties;
046: import org.krysalis.jcharts.properties.PointChartProperties;
047: import org.krysalis.jcharts.properties.StackedAreaChartProperties;
048: import org.krysalis.jcharts.properties.StackedBarChartProperties;
049: import org.krysalis.jcharts.test.TestDataGenerator;
050: import org.krysalis.jcharts.types.ChartType;
051:
052: import javax.faces.component.UIComponent;
053: import java.awt.*;
054:
055: public class AxisChart extends AbstractChart {
056:
057: public AxisChart(UIComponent uiComponent) throws Throwable {
058: super (uiComponent);
059: }
060:
061: protected void buildChart() throws Throwable {
062: if (data == null) {
063: getData(outputChart.getData());
064: }
065: if (type.equalsIgnoreCase(OutputChart.AREA_CHART_TYPE)) {
066: buildAreaChart();
067: } else if (type
068: .equalsIgnoreCase(OutputChart.AREA_STACKED_CHART_TYPE)) {
069: buildAreaStackedChart();
070: } else if (type.equalsIgnoreCase(OutputChart.BAR_CHART_TYPE)) {
071: buildBarChart();
072: } else if (type
073: .equalsIgnoreCase(OutputChart.BAR_STACKED_CHART_TYPE)) {
074: buildBarStackedChart();
075: } else if (type
076: .equalsIgnoreCase(OutputChart.BAR_CLUSTERED_CHART_TYPE)) {
077: buildBarClusteredChart();
078: } else if (type.equalsIgnoreCase(OutputChart.LINE_CHART_TYPE)) {
079: buildLineChart();
080: } else if (type.equalsIgnoreCase(OutputChart.POINT_CHART_TYPE)) {
081: buildPointChart();
082: }
083: }
084:
085: private void buildAreaChart() throws Throwable {
086: AreaChartProperties areaChartProperties = new AreaChartProperties();
087: buildAxisChart(ChartType.AREA, areaChartProperties);
088: }
089:
090: private void buildAreaStackedChart() throws Throwable {
091: StackedAreaChartProperties areaChartProperties = new StackedAreaChartProperties();
092: buildAxisChart(ChartType.AREA_STACKED, areaChartProperties);
093: }
094:
095: private void buildBarChart() throws Throwable {
096: BarChartProperties barChartProperties = new BarChartProperties();
097: buildAxisChart(ChartType.BAR, barChartProperties);
098: }
099:
100: private void buildBarStackedChart() throws Throwable {
101: StackedBarChartProperties barChartProperties = new StackedBarChartProperties();
102: buildAxisChart(ChartType.BAR_STACKED, barChartProperties);
103: }
104:
105: private void buildBarClusteredChart() throws Throwable {
106: ClusteredBarChartProperties barChartProperties = new ClusteredBarChartProperties();
107: buildAxisChart(ChartType.BAR_CLUSTERED, barChartProperties);
108: }
109:
110: private void buildLineChart() throws Throwable {
111: Stroke[] strokes = new Stroke[data.length];
112: for (int i = 0; i < data.length; i++) {
113: strokes[i] = LineChartProperties.DEFAULT_LINE_STROKE;
114: }
115: LineChartProperties lineChartProperties = new LineChartProperties(
116: strokes, getShapes(outputChart.getShapes()));
117: buildAxisChart(ChartType.LINE, lineChartProperties);
118: }
119:
120: private void buildPointChart() throws Throwable {
121: Paint[] outlinePaints = TestDataGenerator
122: .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) throws Throwable {
135: DataSeries dataSeries = new DataSeries(
136: getAsXaxisLabelsArray(outputChart.getXaxisLabels()),
137: outputChart.getXaxisTitle(), outputChart
138: .getYaxisTitle(), outputChart.getChartTitle());
139:
140: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(
141: getAs2dDoubleArray(outputChart.getData()),
142: getAsLabelsArray(outputChart.getLabels()),
143: getPaints(outputChart.getColors()), chartType,
144: chartTypeProperties);
145:
146: AxisProperties axisProperties = ((chartType
147: .equals(ChartType.BAR) || chartType
148: .equals(ChartType.BAR_CLUSTERED)) && outputChart
149: .isHorizontal()) ? new AxisProperties(true)
150: : new AxisProperties();
151:
152: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
153: chart = new org.krysalis.jcharts.axisChart.AxisChart(
154: dataSeries, new ChartProperties(), axisProperties,
155: getLegendProperties(), new Integer(outputChart
156: .getWidth()).intValue(), new Integer(
157: outputChart.getHeight()).intValue());
158: }
159:
160: private Shape[] shapes;
161:
162: private Shape[] getShapes(Object obj) {
163: if (obj == null && shapes == null) {
164: return shapes = getGeneratedShapes(data.length);
165: } else if (obj == null && shapes != null) {
166: return shapes;
167: } else {
168: return shapes = getAsShapeArray(obj);
169: }
170: }
171:
172: String[] xaxisLabels = null;
173:
174: public String[] getAsXaxisLabelsArray(Object obj) {
175: if (obj == null && xaxisLabels == null) {
176: return xaxisLabels = getGeneratedLabels("Xlabel",
177: data[0].length);
178: } else if (obj == null && xaxisLabels != null) {
179: return xaxisLabels;
180: } else {
181: return getAsStringArray(obj);
182: }
183: }
184:
185: String[] labels = null;
186:
187: public String[] getAsLabelsArray(Object obj) {
188: if (obj == null && labels == null) {
189: return labels = getGeneratedLabels("Label", data.length);
190: } else if (obj == null && labels != null) {
191: return labels;
192: } else {
193: return getAsStringArray(obj);
194: }
195: }
196:
197: private double[][] data = null;
198:
199: public double[][] getData(Object obj) {
200: if (obj instanceof String && data != null) {
201: return data;
202: } else {
203: return data = getAs2dDoubleArray(obj);
204: }
205: }
206:
207: public Paint[] getPaints(Object obj) {
208: return getPaints(obj, data.length);
209: }
210: }
|