KickJava   Java API By Example, From Geeks To Geeks.

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


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.interfaces.IAxisChartDataSet;
40 import org.krysalis.jcharts.imageMap.RectMapArea;
41 import org.krysalis.jcharts.properties.*;
42 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.AxisValueRenderEvent;
43
44 import java.awt.*;
45 import java.awt.geom.Rectangle2D JavaDoc;
46
47
48 /*************************************************************************************
49  *
50  * @author Nathaniel Auvil
51  * @version $Id: ClusteredBarChart.java,v 1.2 2003/12/07 14:04:27 nathaniel_auvil Exp $
52  ************************************************************************************/

53 abstract class ClusteredBarChart
54 {
55
56     /********************************************************************************************
57      * Draws the chart
58      * uses Rectangle2D......keep having rounding problems.
59      *
60      * @param axisChart
61      * @param iAxisChartDataSet
62      *********************************************************************************************/

63     static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet )
64     {
65         Graphics2D g2d = axisChart.getGraphics2D();
66         ClusteredBarChartProperties clusteredBarChartProperties = ( ClusteredBarChartProperties ) iAxisChartDataSet.getChartTypeProperties();
67
68
69         float barGroupWidth;
70         float barWidth;
71
72         //---y axis position on screen to start drawing.
73
float startingY;
74         float startingX;
75         float width;
76         float height;
77
78         DataAxisProperties dataAxisProperties;
79         if( axisChart.getAxisProperties().isPlotHorizontal() )
80         {
81             dataAxisProperties = ( DataAxisProperties ) axisChart.getAxisProperties().getXAxisProperties();
82
83             barGroupWidth = axisChart.getYAxis().getScalePixelWidth() * clusteredBarChartProperties.getPercentage();
84             barWidth = barGroupWidth / iAxisChartDataSet.getNumberOfDataSets();
85
86             //---where the group of bars starts
87
startingX = axisChart.getXAxis().getZeroLineCoordinate();
88             startingY = axisChart.getYAxis().getLastTickY() - ( barGroupWidth / 2 );
89             width = 0;
90             height = barWidth;
91             Rectangle2D.Float JavaDoc rectangle = new Rectangle2D.Float JavaDoc( startingX, startingY, width, height );
92
93             horizontalPlot( axisChart,
94                                  iAxisChartDataSet,
95                                  clusteredBarChartProperties,
96                                  dataAxisProperties,
97                                  g2d,
98                                  rectangle,
99                                  startingX,
100                                  startingY,
101                                  barWidth );
102         }
103         else
104         {
105             dataAxisProperties = ( DataAxisProperties ) axisChart.getAxisProperties().getYAxisProperties();
106
107             barGroupWidth = axisChart.getXAxis().getScalePixelWidth() * clusteredBarChartProperties.getPercentage();
108             barWidth = barGroupWidth / iAxisChartDataSet.getNumberOfDataSets();
109
110             //---where the group of bars starts
111
startingX = axisChart.getXAxis().getTickStart() - ( barGroupWidth / 2 );
112             startingY = axisChart.getYAxis().getZeroLineCoordinate();
113             width = barWidth;
114             height = 0;
115             Rectangle2D.Float JavaDoc rectangle = new Rectangle2D.Float JavaDoc( startingX, startingY, width, height );
116
117             verticalPlot( axisChart,
118                               iAxisChartDataSet,
119                               clusteredBarChartProperties,
120                               dataAxisProperties,
121                               g2d,
122                               rectangle,
123                               startingX,
124                               startingY,
125                               barWidth );
126         }
127     }
128
129
130     /*******************************************************************************************
131      *
132      *
133      * @param axisChart
134      * @param iAxisChartDataSet
135      * @param clusteredBarChartProperties
136      * @param dataAxisProperties
137      * @param g2d
138      * @param rectangle
139      * @param startingX
140      * @param startingY
141      * @param barWidth
142      ********************************************************************************************/

143     private static void horizontalPlot( AxisChart axisChart,
144                                                     IAxisChartDataSet iAxisChartDataSet,
145                                                     ClusteredBarChartProperties clusteredBarChartProperties,
146                                                     DataAxisProperties dataAxisProperties,
147                                                     Graphics2D g2d,
148                                                     Rectangle2D.Float JavaDoc rectangle,
149                                                     float startingX,
150                                                     float startingY,
151                                                     float barWidth )
152     {
153       int imageMapLabelIndex = axisChart.getYAxis().getNumberOfScaleItems() - 1;
154
155         //---setup the total area rectangle
156
Rectangle2D.Float JavaDoc totalItemArea= new Rectangle2D.Float JavaDoc();
157       totalItemArea.y = axisChart.getYAxis().getOrigin() - axisChart.getYAxis().getPixelLength() + 1;
158         totalItemArea.height= axisChart.getYAxis().getScalePixelWidth() - 1;
159         totalItemArea.x= axisChart.getXAxis().getOrigin() + 1;
160         totalItemArea.width= axisChart.getXAxis().getPixelLength() - 1;
161
162
163       //---reuse the same Object for pre and post render events.
164
AxisValueRenderEvent axisValueRenderEvent= new AxisValueRenderEvent( axisChart,
165                                                                                                     iAxisChartDataSet,
166                                                                                                     g2d,
167                                                                                                     totalItemArea,
168                                                                                                     axisChart.getXAxis().getZeroLineCoordinate() );
169
170
171         //LOOP
172
for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ )
173         {
174             for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ )
175             {
176                 g2d.setPaint( iAxisChartDataSet.getPaint( j ) );
177
178                 //---there is only ever one data set for a regular bar chart
179
axisValueRenderEvent.setDataSetIndex( j );
180
181                 //---set values for the preRender event
182
axisValueRenderEvent.setValueX( axisChart.getXAxis().getZeroLineCoordinate() );
183                 axisValueRenderEvent.setValueY( (float) rectangle.getCenterY() );
184                 axisValueRenderEvent.setValueIndex( i );
185
186                 //---we want to do this regardless if we render an item
187
clusteredBarChartProperties.firePreRender( axisValueRenderEvent );
188
189
190                 //---if value == 0 do not plot anything.
191
if( iAxisChartDataSet.getValue( j, i ) != 0.0d )
192                 {
193
194                     if( iAxisChartDataSet.getValue( j, i ) < 0 )
195                     {
196                         rectangle.x = axisChart.getXAxis().computeAxisCoordinate( axisChart.getXAxis().getOrigin(),
197                                                                                                      iAxisChartDataSet.getValue( j, i ),
198                                                                                                      axisChart.getXAxis().getScaleCalculator().getMinValue() );
199                         rectangle.width = startingX - rectangle.x;
200
201                         //---set values for the postRender event
202
axisValueRenderEvent.setValueX( rectangle.x );
203                     }
204                     else
205                     {
206                         rectangle.x = startingX;
207                         rectangle.width = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ),
208                                                                                                 axisChart.getXAxis().getOneUnitPixelSize() );
209
210                         //---set values for the postRender event
211
axisValueRenderEvent.setValueX( rectangle.x + rectangle.width );
212                     }
213
214
215
216                     //---with a user defined scale, we could have non-zero data points with a height of zero.
217
if( rectangle.width != 0 )
218                     {
219                         g2d.fill( rectangle );
220
221                         if( clusteredBarChartProperties.getShowOutlinesFlag() )
222                         {
223                             clusteredBarChartProperties.getBarOutlineStroke().draw( g2d, rectangle );
224                         }
225
226
227                         //---if we are generating an ImageMap, store the image coordinates
228
if( axisChart.getGenerateImageMapFlag() )
229                         {
230                             String JavaDoc label;
231                             if( axisChart.getYAxis().getAxisLabelsGroup() != null )
232                             {
233                                 label= axisChart.getYAxis().getAxisLabelsGroup().getTextTag( imageMapLabelIndex ).getText();
234                             }
235                             else
236                             {
237                                 label= null;
238                             }
239
240                             axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle,
241                                                                                                          iAxisChartDataSet.getValue( j, i ),
242                                                                                                          label,
243                                                                                                          iAxisChartDataSet.getLegendLabel( j ) ) );
244                         }
245                     }
246                 }
247
248                 //---notify everyone we just rendered
249
clusteredBarChartProperties.firePostRender( axisValueRenderEvent );
250
251                 rectangle.y += barWidth;
252             }
253
254             imageMapLabelIndex--;
255
256             startingY += axisChart.getYAxis().getScalePixelWidth();
257             rectangle.y = startingY;
258
259             totalItemArea.y+= axisChart.getYAxis().getScalePixelWidth();
260         }
261     }
262
263
264     /**************************************************************************************
265      *
266      * @param axisChart
267      * @param iAxisChartDataSet
268      * @param barChartProperties
269      * @param dataAxisProperties
270      * @param g2d
271      * @param rectangle
272      * @param startingY
273      **************************************************************************************/

274     private static void verticalPlot( AxisChart axisChart,
275                                                  IAxisChartDataSet iAxisChartDataSet,
276                                                  BarChartProperties barChartProperties,
277                                                  DataAxisProperties dataAxisProperties,
278                                                  Graphics2D g2d,
279                                                  Rectangle2D.Float JavaDoc rectangle,
280                                                  float barGroupStartingX,
281                                                  float startingY,
282                                                  float barWidth )
283     {
284
285         //---setup the total area rectangle
286
Rectangle2D.Float JavaDoc totalItemArea= new Rectangle2D.Float JavaDoc();
287         totalItemArea.x= axisChart.getXAxis().getOrigin() + 1;
288         totalItemArea.y = axisChart.getYAxis().getOrigin() - axisChart.getYAxis().getPixelLength() + 1;
289         totalItemArea.width= axisChart.getXAxis().getScalePixelWidth() - 1;
290         totalItemArea.height= axisChart.getYAxis().getPixelLength() - 1;
291
292
293         //---reuse the same Object for pre and post render events.
294
AxisValueRenderEvent axisValueRenderEvent= new AxisValueRenderEvent( axisChart,
295                                                                                                     iAxisChartDataSet,
296                                                                                                     g2d,
297                                                                                                     totalItemArea,
298                                                                                                     axisChart.getYAxis().getZeroLineCoordinate() );
299
300         //LOOP
301
for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ )
302         {
303             for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ )
304             {
305                 g2d.setPaint( iAxisChartDataSet.getPaint( j ) );
306
307
308                 //---there is only ever one data set for a regular bar chart
309
axisValueRenderEvent.setDataSetIndex( j );
310
311
312                 //---if value == 0 do not plot anything.
313
if( iAxisChartDataSet.getValue( j, i ) != 0.0d )
314                 {
315                     //---set values for the preRender event
316
axisValueRenderEvent.setValueX( (float) rectangle.getCenterX() );
317                     axisValueRenderEvent.setValueY( axisChart.getYAxis().getZeroLineCoordinate() );
318                     axisValueRenderEvent.setValueIndex( i );
319
320                     //---we want to do this regardless if we render an item
321
barChartProperties.firePreRender( axisValueRenderEvent );
322
323
324                     if( iAxisChartDataSet.getValue( j, i ) < 0 )
325                     {
326                         rectangle.y = startingY;
327                         rectangle.height = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getYAxis().getOneUnitPixelSize() );
328
329                         //---set values for the postRender event
330
axisValueRenderEvent.setValueY( rectangle.y + rectangle.height );
331                     }
332                     else
333                     {
334                         rectangle.y = axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
335                                                                                                      iAxisChartDataSet.getValue( j, i ),
336                                                                                                      axisChart.getYAxis().getScaleCalculator().getMinValue() );
337                         rectangle.height = startingY - rectangle.y;
338
339                         //---set values for the postRender event
340
axisValueRenderEvent.setValueY( rectangle.y );
341                     }
342
343                     //---with a user defined scale, we could have non-zero data points with a height of zero.
344
if( rectangle.height != 0 )
345                     {
346                         g2d.fill( rectangle );
347
348                         if( barChartProperties.getShowOutlinesFlag() )
349                         {
350                             barChartProperties.getBarOutlineStroke().draw( g2d, rectangle );
351                         }
352
353                         //---if we are generating an ImageMap, store the image coordinates
354
if( axisChart.getGenerateImageMapFlag() )
355                         {
356                             String JavaDoc label;
357                             if( axisChart.getXAxis().getAxisLabelsGroup() != null )
358                             {
359                                 label= axisChart.getXAxis().getAxisLabelsGroup().getTextTag( i ).getText();
360                             }
361                             else
362                             {
363                                 label= null;
364                             }
365
366                             axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle,
367                                                                                                          iAxisChartDataSet.getValue( j, i ),
368                                                                                                          label,
369                                                                                                          iAxisChartDataSet.getLegendLabel( j ) ) );
370                         }
371                     }
372
373                     //---notify everyone we just rendered
374
barChartProperties.firePostRender( axisValueRenderEvent );
375                 }
376
377                 rectangle.x += barWidth;
378             }
379
380             totalItemArea.x+= axisChart.getXAxis().getScalePixelWidth();
381
382             barGroupStartingX += axisChart.getXAxis().getScalePixelWidth();
383             rectangle.x = barGroupStartingX;
384         }
385     }
386
387 }
388
Popular Tags