KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > axisChart > StackedBarChart


1 /***********************************************************************************************
2  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
3  *
4  * Redistribution and use of this software and associated documentation ("Software"), with or
5  * without modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain copyright statements and notices.
8  * Redistributions must also contain a copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
15  * products derived from this Software without prior written permission of Nathaniel G.
16  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
17  *
18  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
19  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
20  * registered trademark of Nathaniel G. Auvil.
21  *
22  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
23  *
24  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
25  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
33  ************************************************************************************************/

34
35
36 package org.krysalis.jcharts.axisChart;
37
38
39 import org.krysalis.jcharts.chartData.ChartDataException;
40 import org.krysalis.jcharts.chartData.interfaces.IAxisChartDataSet;
41 import org.krysalis.jcharts.imageMap.RectMapArea;
42 import org.krysalis.jcharts.properties.DataAxisProperties;
43 import org.krysalis.jcharts.properties.StackedBarChartProperties;
44
45 import java.awt.*;
46 import java.awt.geom.Rectangle2D JavaDoc;
47
48
49 /*************************************************************************************
50  *
51  * @author Nathaniel Auvil
52  * @version $Id: StackedBarChart.java,v 1.1 2003/05/17 16:56:57 nathaniel_auvil Exp $
53  ************************************************************************************/

54 abstract class StackedBarChart
55 {
56
57     /********************************************************************************************
58      * Draws the chart
59      *
60      * @param axisChart
61      *********************************************************************************************/

62     static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet ) throws ChartDataException
63     {
64         Graphics2D g2d = axisChart.getGraphics2D();
65         StackedBarChartProperties stackedBarChartProperties = (StackedBarChartProperties) iAxisChartDataSet.getChartTypeProperties();
66
67
68         float barWidth;
69
70         //---y axis position on screen to start drawing.
71
float startingX;
72         float startingY;
73         float width;
74         float height;
75
76
77         DataAxisProperties dataAxisProperties;
78
79
80         if( axisChart.getAxisProperties().isPlotHorizontal() )
81         {
82             dataAxisProperties = (DataAxisProperties) axisChart.getAxisProperties().getXAxisProperties();
83             barWidth = axisChart.getYAxis().getScalePixelWidth() * stackedBarChartProperties.getPercentage();
84
85             startingX = axisChart.getXAxis().getZeroLineCoordinate();
86             startingY = axisChart.getYAxis().getLastTickY() - (barWidth / 2);
87             width = 0;
88             height = barWidth;
89             Rectangle2D.Float JavaDoc rectangle = new Rectangle2D.Float JavaDoc( startingX, startingY, width, height );
90
91             StackedBarChart.horizontalPlot( axisChart, iAxisChartDataSet, stackedBarChartProperties, dataAxisProperties, g2d, rectangle, startingX );
92         }
93         else
94         {
95             dataAxisProperties = (DataAxisProperties) axisChart.getAxisProperties().getYAxisProperties();
96             barWidth = axisChart.getXAxis().getScalePixelWidth() * stackedBarChartProperties.getPercentage();
97
98             startingX = axisChart.getXAxis().getTickStart() - (barWidth / 2);
99             startingY = axisChart.getYAxis().getZeroLineCoordinate();
100             width = barWidth;
101             height = 0;
102             Rectangle2D.Float JavaDoc rectangle = new Rectangle2D.Float JavaDoc( startingX, startingY, width, height );
103
104             StackedBarChart.verticalPlot( axisChart, iAxisChartDataSet, stackedBarChartProperties, dataAxisProperties, g2d, rectangle, startingY );
105         }
106     }
107
108
109     /**************************************************************************************
110      *
111      * @param axisChart
112      * @param iAxisChartDataSet
113      * @param stackedBarChartProperties
114      * @param dataAxisProperties
115      * @param g2d
116      * @param rectangle
117      * @param startingX
118      **************************************************************************************/

119     private static void horizontalPlot( AxisChart axisChart,
120                                                     IAxisChartDataSet iAxisChartDataSet,
121                                                     StackedBarChartProperties stackedBarChartProperties,
122                                                     DataAxisProperties dataAxisProperties,
123                                                     Graphics2D g2d,
124                                                     Rectangle2D.Float JavaDoc rectangle,
125                                                     float startingX ) throws ChartDataException
126     {
127         int imageMapLabelIndex = axisChart.getYAxis().getNumberOfScaleItems() - 1;
128
129         //LOOP
130
//---initial postion of each line.
131
for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ )
132         {
133             //---draw each bar in stack
134
for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ )
135             {
136                 //---if segment has a zero value, draw nothing.
137
if( iAxisChartDataSet.getValue( j, i ) == 0 )
138                 {
139                     continue;
140                 }
141                 else if( iAxisChartDataSet.getValue( j, i ) < 0 )
142                 {
143 //todo i think we could support this, but it can wait
144
throw new ChartDataException( "Negative values in Stacked Bar charts are not supported yet... Coming soon..." );
145 /*
146                     rectangle.x = axisChart.getXAxis().computeAxisCoordinate( axisChart.getXAxis().getOrigin(),
147                                                                                                  iAxisChartDataSet.getValue( 0, i ),
148                                                                                                  dataAxisProperties.getScaleCalculator().getMinValue() );
149                     rectangle.width = startingX - rectangle.x;
150 */

151                 }
152                 else
153                 {
154
155                     rectangle.width = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getXAxis().getOneUnitPixelSize() );
156
157
158                     //rectangle.x = startingX;
159
//rectangle.width = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getXAxis().getOneUnitPixelSize() );
160
}
161
162
163                 g2d.setPaint( iAxisChartDataSet.getPaint( j ) );
164                 g2d.fill( rectangle );
165
166                 if( stackedBarChartProperties.getShowOutlinesFlag() )
167                 {
168                     stackedBarChartProperties.getBarOutlineStroke().draw( g2d, rectangle );
169                 }
170
171
172                 //---if we are generating an ImageMap, store the image coordinates
173
if( axisChart.getGenerateImageMapFlag() )
174                 {
175                     String JavaDoc label = null;
176                     if( axisChart.getYAxis().getAxisLabelsGroup() != null )
177                     {
178                         label = axisChart.getYAxis().getAxisLabelsGroup().getTextTag( imageMapLabelIndex ).getText();
179                     }
180                     axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle,
181                                                                                                  iAxisChartDataSet.getValue( j, i ),
182                                                                                                  label,
183                                                                                                  iAxisChartDataSet.getLegendLabel( j ) ) );
184                 }
185
186                 rectangle.x += rectangle.width;
187             }
188
189             imageMapLabelIndex--;
190
191             rectangle.y += axisChart.getYAxis().getScalePixelWidth();
192             rectangle.x = startingX;
193         }
194     }
195
196
197     /**************************************************************************************
198      *
199      * @param axisChart
200      * @param iAxisChartDataSet
201      * @param stackedBarChartProperties
202      * @param dataAxisProperties
203      * @param g2d
204      * @param rectangle
205      * @param startingY
206      **************************************************************************************/

207     private static void verticalPlot( AxisChart axisChart,
208                                                  IAxisChartDataSet iAxisChartDataSet,
209                                                  StackedBarChartProperties stackedBarChartProperties,
210                                                  DataAxisProperties dataAxisProperties,
211                                                  Graphics2D g2d,
212                                                  Rectangle2D.Float JavaDoc rectangle,
213                                                  float startingY )
214     {
215         //IDataSeries iDataSeries= (IDataSeries) axisChart.getIAxisDataSeries();
216

217         //LOOP
218
//---initial postion of each line.
219
for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ )
220         {
221             //---draw each bar in stack
222
for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ )
223             {
224                 //---if segment has a zero value, draw nothing.
225
if( iAxisChartDataSet.getValue( j, i ) == 0 )
226                 {
227                     continue;
228                 }
229
230                 rectangle.height = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getYAxis().getOneUnitPixelSize() );
231                 rectangle.y -= rectangle.height;
232
233
234                 g2d.setPaint( iAxisChartDataSet.getPaint( j ) );
235                 g2d.fill( rectangle );
236
237                 if( stackedBarChartProperties.getShowOutlinesFlag() )
238                 {
239                     stackedBarChartProperties.getBarOutlineStroke().draw( g2d, rectangle );
240                 }
241
242
243                 //---if we are generating an ImageMap, store the image coordinates
244
if( axisChart.getGenerateImageMapFlag() )
245                 {
246                     String JavaDoc label = null;
247                     if( axisChart.getXAxis().getAxisLabelsGroup() != null )
248                     {
249                         label = axisChart.getXAxis().getAxisLabelsGroup().getTextTag( i ).getText();
250                     }
251                     axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle,
252                                                                                                  iAxisChartDataSet.getValue( j, i ),
253                                                                                                  label,
254                                                                                                  iAxisChartDataSet.getLegendLabel( j ) ) );
255                 }
256
257             }
258
259             rectangle.x += axisChart.getXAxis().getScalePixelWidth();
260             rectangle.y = startingY;
261         }
262     }
263
264 }
265
Popular Tags