KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > axisChart > axis > YAxis


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.axis;
37
38
39 import org.krysalis.jcharts.axisChart.AxisChart;
40 import org.krysalis.jcharts.properties.*;
41 import org.krysalis.jcharts.properties.util.ChartFont;
42 import org.krysalis.jcharts.test.HTMLGenerator;
43 import org.krysalis.jcharts.test.HTMLTestable;
44
45 import java.awt.*;
46 import java.awt.geom.Line2D JavaDoc;
47 import java.lang.reflect.Field JavaDoc;
48
49
50 /*************************************************************************************
51  *
52  * @author Nathaniel Auvil, John Thomson
53  * @version $Id: YAxis.java,v 1.5 2004/05/31 16:29:47 nathaniel_auvil Exp $
54  ************************************************************************************/

55 public class YAxis extends Axis implements HTMLTestable
56 {
57
58    private float lastTickY;
59
60
61     /**************************************************************************************************
62      * Constructor
63      *
64      * @param axisChart
65      ***************************************************************************************************/

66     public YAxis( AxisChart axisChart, int numberOfScaleItems )
67     {
68         super( axisChart, numberOfScaleItems );
69     }
70
71
72     /************************************************************************************
73      * Need this value as horizontal plots start from the top of the axis and go down
74      *
75      * @return float
76      ************************************************************************************/

77     public float getLastTickY()
78     {
79         return lastTickY;
80     }
81
82
83     /*************************************************************************************************
84      * Computes the minimum pixel width required for the Y-Axis.
85      * Includes space, if needed, for: axis title + padding, axis values + tick padding, and tick marks.
86      *
87      **************************************************************************************************/

88     public void computeMinimumWidthNeeded( String JavaDoc yAxisTitle )
89     {
90         AxisTypeProperties axisTypeProperties= super.getAxisChart().getAxisProperties().getYAxisProperties();
91
92
93         float widthNeeded = 0;
94
95
96         //---if we are displaying axis scale labels, add width of widest label
97
if( axisTypeProperties.showAxisLabels() )
98         {
99                         // Dual Y axis changes integrated CMC 25Aug03
100
//widthNeeded+= super.getAxisLabelsGroup().getWidestLabel();
101
if ( axisTypeProperties.getShowRightAxis() )
102                         {
103                                 if ( super.getAxisLabelsGroupRight().getWidestLabel()>super.getAxisLabelsGroup().getWidestLabel())
104                                 {
105                                         widthNeeded+= super.getAxisLabelsGroupRight().getWidestLabel();
106                                 }
107                                 else
108                                 {
109                                         widthNeeded+= super.getAxisLabelsGroup().getWidestLabel();
110                                 }
111                             }
112                         else
113                         {
114                                 widthNeeded+= super.getAxisLabelsGroup().getWidestLabel();
115                         }
116                 }
117
118
119         if( axisTypeProperties.getShowTicks() != AxisTypeProperties.TICKS_NONE )
120         {
121             //---add the padding between scale labels and tick marks
122
widthNeeded += axisTypeProperties.getPaddingBetweenLabelsAndTicks();
123
124             //---add width of tick marks
125
widthNeeded += axisTypeProperties.getAxisTickMarkPixelLength();
126         }
127         else
128         {
129             //---else, if we are not showing any ticks, add padding between labels and axis, if we are displaying labels
130
if( axisTypeProperties.showAxisLabels() )
131             {
132                 widthNeeded += axisTypeProperties.getPaddingBetweenAxisAndLabels();
133             }
134         }
135
136
137         //---include axis title height if needed. Remember it is vertical for y-axis
138
//if( super.getAxisChart().getAxisProperties().getShowAxisTitle( AxisProperties.Y_AXIS ) )
139
if( yAxisTitle != null )
140         {
141             super.computeAxisTitleDimensions( yAxisTitle, axisTypeProperties.getTitleChartFont() );
142             widthNeeded += super.getTitleHeight();
143             widthNeeded += axisTypeProperties.getPaddingBetweenAxisTitleAndLabels();
144         }
145
146         super.setMinimumWidthNeeded( widthNeeded );
147     }
148
149
150     /****************************************************************************************************
151      *
152      * @param axisTitle
153      * @param graphics2D
154      * @param axisTypeProperties
155      ***************************************************************************************************/

156     private void renderAxisTitle( String JavaDoc axisTitle, Graphics2D graphics2D, AxisTypeProperties axisTypeProperties )
157     {
158         if( axisTitle != null )
159         {
160             float titleY; // = super.getAxisChart().getChartProperties().getEdgePadding();
161
float titleX = super.getAxisChart().getXAxis().getOrigin() - super.getMinimumWidthNeeded() + super.getTitleHeight();
162
163             //---if YAxis title is larger than the YAxis itself, center it on image.
164
if( super.getTitleWidth() > super.getPixelLength() )
165             {
166                 //titleY= super.getTitleWidth();
167
titleY= super.getAxisChart().getImageHeight() - ( ( super.getAxisChart().getImageHeight() - super.getTitleWidth() ) / 2 );
168             }
169             //---else, center on YAxis.
170
else
171             {
172                 titleY = this.getOrigin() - ( ( super.getPixelLength() - super.getTitleWidth() ) / 2 );
173             }
174
175 //TODO this should use a TextTag
176
graphics2D.setFont( axisTypeProperties.getAxisTitleChartFont().getFont().deriveFont( ChartFont.VERTICAL_ROTATION ) );
177             graphics2D.setPaint( axisTypeProperties.getAxisTitleChartFont().getPaint() );
178             graphics2D.drawString( axisTitle, titleX, titleY );
179         }
180     }
181
182
183     /*********************************************************************************************
184      * Renders the YAxis on the passes Graphics2D object
185      *
186      * @param graphics2D
187      * @param axisProperties
188      * @param yAxisTitle
189      **********************************************************************************************/

190     public void render( Graphics2D graphics2D, AxisProperties axisProperties, String JavaDoc yAxisTitle )
191     {
192         AxisTypeProperties axisTypeProperties = axisProperties.getYAxisProperties();
193
194
195         //---AXIS TITLE
196
this.renderAxisTitle( yAxisTitle, graphics2D, axisProperties.getYAxisProperties() );
197
198
199         Line2D.Float JavaDoc line2D;
200         float stringY= 0;
201
202
203         if( axisTypeProperties instanceof DataAxisProperties )
204         {
205             //---start at the axis
206
line2D = new Line2D.Float JavaDoc( 0.0f, super.getOrigin(), 0.0f, super.getOrigin() );
207
208             if( axisTypeProperties.showAxisLabels() )
209             {
210                 stringY = super.getOrigin() + ( super.getAxisLabelsGroup().getTallestLabel() / 4 );
211             }
212         }
213         else
214         {
215             //--start at half a axis item width
216
float y = super.getOrigin() - ( super.getScalePixelWidth() / 2 );
217             line2D = new Line2D.Float JavaDoc( 0.0f, y, 0.0f, y );
218
219             if( axisTypeProperties.showAxisLabels() )
220             {
221                 stringY = y + ( super.getAxisLabelsGroup().getTallestLabel() / 4 );
222
223                 //---horizontal plots start at top of axis and go down
224
super.getAxisLabelsGroup().reverse();
225             }
226         }
227
228
229         float tickX1 = super.getAxisChart().getXAxis().getOrigin() - axisTypeProperties.getAxisTickMarkPixelLength();
230         float tickX2 = super.getAxisChart().getXAxis().getOrigin();
231         float gridLineX1 = super.getAxisChart().getXAxis().getOrigin() + 1;
232         float gridLineX2 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength();
233
234
235         float stringX = super.getAxisChart().getXAxis().getOrigin() - axisTypeProperties.getAxisTickMarkPixelLength();
236
237         if( axisTypeProperties.showAxisLabels() )
238         {
239             stringX-= axisTypeProperties.getPaddingBetweenLabelsAndTicks();
240         }
241
242
243         for( int i = 0; i < super.getNumberOfScaleItems(); i++ )
244         {
245             //---GRID LINES
246
if( axisTypeProperties.getShowGridLines() != AxisTypeProperties.GRID_LINES_NONE )
247             {
248                 //---we do not want to draw a grid line over the axis as data axis first value is on the axis
249
if( i > 0 || ( i == 0 && !( axisTypeProperties instanceof DataAxisProperties ) ) )
250                 {
251                     line2D.x1 = gridLineX1;
252                     line2D.x2 = gridLineX2;
253
254                     //TODO what is this doing???? How could i ever equal the size?
255

256                     if( i < super.getAxisLabelsGroup().size()
257                         || ( i == super.getAxisLabelsGroup().size() && !axisTypeProperties.getShowEndBorder() ) )
258
259 // if( i == super.getNumberOfScaleItems() - 1 && !axisTypeProperties.getShowEndBorder() )
260
{
261                         axisTypeProperties.getGridLineChartStroke().draw( graphics2D, line2D );
262                     }
263                     else
264                     {
265                         //---draw top border with the same ChartStroke as the X-Axis
266
axisProperties.getXAxisProperties().getAxisStroke().draw( graphics2D, line2D );
267                     }
268                 }
269             }
270
271             //---TICK MARKS
272
if( axisTypeProperties.getShowTicks() != AxisTypeProperties.TICKS_NONE )
273             {
274                 line2D.x1 = tickX1;
275                 line2D.x2 = tickX2;
276                 axisTypeProperties.getTickChartStroke().draw( graphics2D, line2D );
277             }
278
279             //---need this value as horizontal plots start from the top of the axis and go down
280
//---must set this no matter if no ticks are present as horizontal plots start their rendering based on this screen coordinate.
281
this.lastTickY= line2D.y1;
282
283             line2D.y1 -= super.getScalePixelWidth();
284             line2D.y2 = line2D.y1;
285
286
287             //---AXIS LABEL
288
if( axisTypeProperties.showAxisLabels() )
289             {
290                 super.getAxisLabelsGroup().render( i, graphics2D, stringX - super.getAxisLabelsGroup().getTextTag( i ).getWidth(), stringY );
291                 //graphics2D.setPaint( axisProperties.getScaleFontColor() );
292
//graphics2D.drawString( this.formattedLabels[ i ], stringX - this.labelWidths[ i ], stringY );
293
}
294
295             stringY -= super.getScalePixelWidth();
296         }
297
298
299         //---AXIS----------------------------------------------------------------------
300
line2D.x1 = super.getAxisChart().getXAxis().getOrigin();
301         line2D.x2 = line2D.x1;
302         line2D.y1 = super.getOrigin() - super.getPixelLength();
303         line2D.y2 = super.getOrigin();
304         axisTypeProperties.getAxisStroke().draw( graphics2D, line2D );
305
306
307         //---TOP BORDER----------------------------------------------------------------
308
if( axisTypeProperties.getShowEndBorder() )
309         {
310             line2D.x1 = super.getAxisChart().getXAxis().getOrigin();
311             line2D.x2 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength();
312             line2D.y1 = super.getOrigin() - super.getPixelLength();
313             line2D.y2 = line2D.y1;
314             axisProperties.getXAxisProperties().getAxisStroke().draw( graphics2D, line2D );
315         }
316
317
318         //---ZERO LINE-----------------------------------------------------------------
319
if( axisTypeProperties instanceof DataAxisProperties )
320         {
321             DataAxisProperties dataAxisProperties = ( DataAxisProperties ) axisTypeProperties;
322
323             if( dataAxisProperties.showZeroLine()
324                 && super.getScaleCalculator().getMinValue() < 0.0d
325                 && super.getScaleCalculator().getMaxValue() > 0.0d )
326             {
327                 line2D.y1 = super.getZeroLineCoordinate();
328                 line2D.y2 = line2D.y1;
329                 line2D.x1 = super.getAxisChart().getXAxis().getOrigin();
330                 line2D.x2 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength();
331                 dataAxisProperties.getZeroLineChartStroke().draw( graphics2D, line2D );
332             }
333         }
334
335                 // Dual Y axis changes integrated CMC 25Aug03
336
//---if AXIS at the right----------------------------------------------------------------------
337

338                 if (axisTypeProperties.getShowRightAxis())
339                 {
340
341                         line2D.x1 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength();
342                         line2D.x2 = line2D.x1;
343                         line2D.y1 = super.getOrigin() - super.getPixelLength() - 10 ;
344                         line2D.y2 = super.getOrigin();
345                         axisTypeProperties.getAxisStroke().draw( graphics2D, line2D );
346
347                         float tickRightX1 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength();
348                         float tickRightX2 = tickRightX1 + axisTypeProperties.getAxisTickMarkPixelLength();
349                         line2D.y1 = super.getOrigin();
350                         line2D.y2 = line2D.y1;
351
352                         stringY = super.getOrigin() + ( super.getAxisLabelsGroupRight().getTallestLabel() / 4 );
353                         stringX = super.getAxisChart().getXAxis().getOrigin() + axisTypeProperties.getAxisTickMarkPixelLength()
354                                     + super.getAxisChart().getXAxis().getPixelLength();
355
356                         stringX+= axisTypeProperties.getPaddingBetweenLabelsAndTicks();
357
358                         for( int i = 0; i < super.getNumberOfScaleItems(); i++ )
359                         {
360                                 //---TICK MARKS at the right
361
if( axisTypeProperties.getShowTicks() != AxisTypeProperties.TICKS_NONE )
362                                 {
363                                         line2D.x1 = tickRightX1;
364                                         line2D.x2 = tickRightX2;
365                                         axisTypeProperties.getTickChartStroke().draw( graphics2D, line2D );
366                                 }
367
368                                 line2D.y1 -= super.getScalePixelWidth();
369                                 line2D.y2 = line2D.y1;
370
371
372                                 //---AXIS LABEL at the right
373
if( axisTypeProperties.showAxisLabels() )
374                                 {
375
376                                         // Font and Paint set in TextTagGroup object in 1.0 it seems CMC 25Aug03
377
//graphics2D.setFont( axisTypeProperties.getScaleChartFontRight().getFont());
378
//graphics2D.setPaint( axisTypeProperties.getScaleChartFontRight().getPaint() );
379
super.getAxisLabelsGroupRight().render( i, graphics2D, stringX , stringY );
380                                 }
381                                 stringY -= super.getScalePixelWidth();
382                         }
383                 }
384     }
385
386
387     /*************************************************************************************************
388      * Takes a value and determines the screen coordinate it should be drawn at. THe only difference
389      * between this and the x-axis is we subtract to the origin versus subtract from it.
390
391      *
392      * @param origin
393      * @param value
394      * @param axisMinValue the minimum value on the axis
395      * @return float the screen pixel coordinate
396      **************************************************************************************************/

397     public float computeAxisCoordinate( float origin, double value, double axisMinValue )
398     {
399         double returnValue = origin - ( value - axisMinValue ) * this.getOneUnitPixelSize();
400         return ( float ) returnValue;
401     }
402
403
404     /*********************************************************************************************
405      * Enables the testing routines to display the contents of this Object.
406      *
407      * @param htmlGenerator
408      **********************************************************************************************/

409 //todo is this method needed?
410
public void toHTML( HTMLGenerator htmlGenerator )
411     {
412         htmlGenerator.propertiesTableStart( this.getClass().getName() );
413
414         super.toHTML( htmlGenerator );
415
416         Field JavaDoc[] fields = this.getClass().getDeclaredFields();
417         for( int i = 0; i < fields.length; i++ )
418         {
419             try
420             {
421                 htmlGenerator.addField( fields[ i ].getName(), fields[ i ].get( this ) );
422             }
423             catch( IllegalAccessException JavaDoc illegalAccessException )
424             {
425                 illegalAccessException.printStackTrace();
426             }
427         }
428
429         htmlGenerator.propertiesTableEnd();
430     }
431
432 }
433
Popular Tags