001: /***********************************************************************************************
002: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
003: *
004: * Redistribution and use of this software and associated documentation ("Software"), with or
005: * without modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain copyright statements and notices.
008: * Redistributions must also contain a copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other materials
012: * provided with the distribution.
013: *
014: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
015: * products derived from this Software without prior written permission of Nathaniel G.
016: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
017: *
018: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
019: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
020: * registered trademark of Nathaniel G. Auvil.
021: *
022: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
023: *
024: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
025: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
026: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
032: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
033: ************************************************************************************************/package org.krysalis.jcharts.axisChart;
034:
035: import org.krysalis.jcharts.chartData.ChartDataException;
036: import org.krysalis.jcharts.chartData.interfaces.IAxisChartDataSet;
037: import org.krysalis.jcharts.imageMap.RectMapArea;
038: import org.krysalis.jcharts.properties.DataAxisProperties;
039: import org.krysalis.jcharts.properties.StackedBarChartProperties;
040:
041: import java.awt.*;
042: import java.awt.geom.Rectangle2D;
043:
044: /*************************************************************************************
045: *
046: * @author Nathaniel Auvil
047: * @version $Id: StackedBarChart.java,v 1.1 2003/05/17 16:56:57 nathaniel_auvil Exp $
048: ************************************************************************************/
049: abstract class StackedBarChart {
050:
051: /********************************************************************************************
052: * Draws the chart
053: *
054: * @param axisChart
055: *********************************************************************************************/
056: static void render(AxisChart axisChart,
057: IAxisChartDataSet iAxisChartDataSet)
058: throws ChartDataException {
059: Graphics2D g2d = axisChart.getGraphics2D();
060: StackedBarChartProperties stackedBarChartProperties = (StackedBarChartProperties) iAxisChartDataSet
061: .getChartTypeProperties();
062:
063: float barWidth;
064:
065: //---y axis position on screen to start drawing.
066: float startingX;
067: float startingY;
068: float width;
069: float height;
070:
071: DataAxisProperties dataAxisProperties;
072:
073: if (axisChart.getAxisProperties().isPlotHorizontal()) {
074: dataAxisProperties = (DataAxisProperties) axisChart
075: .getAxisProperties().getXAxisProperties();
076: barWidth = axisChart.getYAxis().getScalePixelWidth()
077: * stackedBarChartProperties.getPercentage();
078:
079: startingX = axisChart.getXAxis().getZeroLineCoordinate();
080: startingY = axisChart.getYAxis().getLastTickY()
081: - (barWidth / 2);
082: width = 0;
083: height = barWidth;
084: Rectangle2D.Float rectangle = new Rectangle2D.Float(
085: startingX, startingY, width, height);
086:
087: StackedBarChart.horizontalPlot(axisChart,
088: iAxisChartDataSet, stackedBarChartProperties,
089: dataAxisProperties, g2d, rectangle, startingX);
090: } else {
091: dataAxisProperties = (DataAxisProperties) axisChart
092: .getAxisProperties().getYAxisProperties();
093: barWidth = axisChart.getXAxis().getScalePixelWidth()
094: * stackedBarChartProperties.getPercentage();
095:
096: startingX = axisChart.getXAxis().getTickStart()
097: - (barWidth / 2);
098: startingY = axisChart.getYAxis().getZeroLineCoordinate();
099: width = barWidth;
100: height = 0;
101: Rectangle2D.Float rectangle = new Rectangle2D.Float(
102: startingX, startingY, width, height);
103:
104: StackedBarChart.verticalPlot(axisChart, iAxisChartDataSet,
105: stackedBarChartProperties, dataAxisProperties, g2d,
106: rectangle, startingY);
107: }
108: }
109:
110: /**************************************************************************************
111: *
112: * @param axisChart
113: * @param iAxisChartDataSet
114: * @param stackedBarChartProperties
115: * @param dataAxisProperties
116: * @param g2d
117: * @param rectangle
118: * @param startingX
119: **************************************************************************************/
120: private static void horizontalPlot(AxisChart axisChart,
121: IAxisChartDataSet iAxisChartDataSet,
122: StackedBarChartProperties stackedBarChartProperties,
123: DataAxisProperties dataAxisProperties, Graphics2D g2d,
124: Rectangle2D.Float rectangle, float startingX)
125: throws ChartDataException {
126: int imageMapLabelIndex = axisChart.getYAxis()
127: .getNumberOfScaleItems() - 1;
128:
129: //LOOP
130: //---initial postion of each line.
131: for (int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++) {
132: //---draw each bar in stack
133: for (int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++) {
134: //---if segment has a zero value, draw nothing.
135: if (iAxisChartDataSet.getValue(j, i) == 0) {
136: continue;
137: } else if (iAxisChartDataSet.getValue(j, i) < 0) {
138: //todo i think we could support this, but it can wait
139: throw new ChartDataException(
140: "Negative values in Stacked Bar charts are not supported yet... Coming soon...");
141: /*
142: rectangle.x = axisChart.getXAxis().computeAxisCoordinate( axisChart.getXAxis().getOrigin(),
143: iAxisChartDataSet.getValue( 0, i ),
144: dataAxisProperties.getScaleCalculator().getMinValue() );
145: rectangle.width = startingX - rectangle.x;
146: */
147: } else {
148:
149: rectangle.width = BarChart
150: .computeScaleHeightOfValue(
151: iAxisChartDataSet.getValue(j, i),
152: axisChart.getXAxis()
153: .getOneUnitPixelSize());
154:
155: //rectangle.x = startingX;
156: //rectangle.width = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getXAxis().getOneUnitPixelSize() );
157: }
158:
159: g2d.setPaint(iAxisChartDataSet.getPaint(j));
160: g2d.fill(rectangle);
161:
162: if (stackedBarChartProperties.getShowOutlinesFlag()) {
163: stackedBarChartProperties.getBarOutlineStroke()
164: .draw(g2d, rectangle);
165: }
166:
167: //---if we are generating an ImageMap, store the image coordinates
168: if (axisChart.getGenerateImageMapFlag()) {
169: String label = null;
170: if (axisChart.getYAxis().getAxisLabelsGroup() != null) {
171: label = axisChart.getYAxis()
172: .getAxisLabelsGroup().getTextTag(
173: imageMapLabelIndex).getText();
174: }
175: axisChart.getImageMap().addImageMapArea(
176: new RectMapArea(rectangle,
177: iAxisChartDataSet.getValue(j, i),
178: label, iAxisChartDataSet
179: .getLegendLabel(j)));
180: }
181:
182: rectangle.x += rectangle.width;
183: }
184:
185: imageMapLabelIndex--;
186:
187: rectangle.y += axisChart.getYAxis().getScalePixelWidth();
188: rectangle.x = startingX;
189: }
190: }
191:
192: /**************************************************************************************
193: *
194: * @param axisChart
195: * @param iAxisChartDataSet
196: * @param stackedBarChartProperties
197: * @param dataAxisProperties
198: * @param g2d
199: * @param rectangle
200: * @param startingY
201: **************************************************************************************/
202: private static void verticalPlot(AxisChart axisChart,
203: IAxisChartDataSet iAxisChartDataSet,
204: StackedBarChartProperties stackedBarChartProperties,
205: DataAxisProperties dataAxisProperties, Graphics2D g2d,
206: Rectangle2D.Float rectangle, float startingY) {
207: //IDataSeries iDataSeries= (IDataSeries) axisChart.getIAxisDataSeries();
208:
209: //LOOP
210: //---initial postion of each line.
211: for (int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++) {
212: //---draw each bar in stack
213: for (int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++) {
214: //---if segment has a zero value, draw nothing.
215: if (iAxisChartDataSet.getValue(j, i) == 0) {
216: continue;
217: }
218:
219: rectangle.height = BarChart.computeScaleHeightOfValue(
220: iAxisChartDataSet.getValue(j, i), axisChart
221: .getYAxis().getOneUnitPixelSize());
222: rectangle.y -= rectangle.height;
223:
224: g2d.setPaint(iAxisChartDataSet.getPaint(j));
225: g2d.fill(rectangle);
226:
227: if (stackedBarChartProperties.getShowOutlinesFlag()) {
228: stackedBarChartProperties.getBarOutlineStroke()
229: .draw(g2d, rectangle);
230: }
231:
232: //---if we are generating an ImageMap, store the image coordinates
233: if (axisChart.getGenerateImageMapFlag()) {
234: String label = null;
235: if (axisChart.getXAxis().getAxisLabelsGroup() != null) {
236: label = axisChart.getXAxis()
237: .getAxisLabelsGroup().getTextTag(i)
238: .getText();
239: }
240: axisChart.getImageMap().addImageMapArea(
241: new RectMapArea(rectangle,
242: iAxisChartDataSet.getValue(j, i),
243: label, iAxisChartDataSet
244: .getLegendLabel(j)));
245: }
246:
247: }
248:
249: rectangle.x += axisChart.getXAxis().getScalePixelWidth();
250: rectangle.y = startingY;
251: }
252: }
253:
254: }
|