KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > chartText > NumericTagGroup


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 package org.krysalis.jcharts.chartText;
36
37
38 import org.krysalis.jcharts.axisChart.axis.scale.ScaleCalculator;
39 import org.krysalis.jcharts.properties.util.ChartFont;
40
41 import java.awt.font.FontRenderContext JavaDoc;
42 import java.text.NumberFormat JavaDoc;
43
44
45 /*************************************************************************************
46  *
47  * @author Nathaniel Auvil, John Thomsen
48  * @version $Id: NumericTagGroup.java,v 1.1 2003/05/17 16:58:32 nathaniel_auvil Exp $
49  ************************************************************************************/

50 public class NumericTagGroup extends TextTagGroup
51 {
52     private NumberFormat JavaDoc numberFormat;
53
54
55     /******************************************************************************************
56      *
57      * @param chartFont
58      * @param fontRenderContext
59      * @param isCurrency
60      * @param isPercent
61      * @param showCommas
62      * @param roundingPowerOfTen the exponent of ten to round to: 1=10, -1=.1, 3=1000, etc...
63      *******************************************************************************************/

64     public NumericTagGroup( ChartFont chartFont,
65                                     FontRenderContext JavaDoc fontRenderContext,
66                                     boolean isCurrency,
67                                     boolean isPercent,
68                                     boolean showCommas,
69                                     int roundingPowerOfTen )
70     {
71         super( chartFont, fontRenderContext );
72
73         this.numberFormat = getNumberFormatInstance( isCurrency, isPercent, showCommas, roundingPowerOfTen );
74     }
75
76
77     /*********************************************************************************************
78      * Allow use of this logic to not only this class but to the chart label values.
79      *
80      * @param isCurrency
81      * @param isPercent
82      * @param showCommas
83      * @param roundingPowerOfTen
84      * @return NumberFormat
85      ********************************************************************************************/

86     public static final NumberFormat JavaDoc getNumberFormatInstance( boolean isCurrency,
87                                                                                  boolean isPercent,
88                                                                                  boolean showCommas,
89                                                                                  int roundingPowerOfTen )
90     {
91         NumberFormat JavaDoc numberFormat;
92
93         //---CURRENCY
94
if( isCurrency )
95         {
96             numberFormat = NumberFormat.getCurrencyInstance();
97         }
98         else if( isPercent )
99         {
100             numberFormat = NumberFormat.getPercentInstance();
101         }
102         else
103         {
104             numberFormat = NumberFormat.getInstance();
105         }
106
107         //---COMMAS
108
if( showCommas )
109         {
110             numberFormat.setGroupingUsed( true );
111         }
112         else
113         {
114             numberFormat.setGroupingUsed( false );
115         }
116
117         //---TRIM OFF DECIMAL PLACES IF ROUND TO WHOLE NUMBER
118
if( roundingPowerOfTen >= 0 )
119         {
120             numberFormat.setMaximumFractionDigits( 0 );
121             numberFormat.setMinimumFractionDigits( 0 );
122         }
123         else
124         {
125             numberFormat.setMaximumFractionDigits( -roundingPowerOfTen );
126             numberFormat.setMinimumFractionDigits( -roundingPowerOfTen );
127         }
128
129         return numberFormat;
130     }
131
132
133     /******************************************************************************************
134      *
135      * @param value
136      *****************************************************************************************/

137     public void addLabel( double value )
138     {
139         super.addLabel( this.numberFormat.format( value ) );
140     }
141
142
143     /*******************************************************************************************
144      * Creates the axis labels for the passed ScaleCalculator instance.
145      *
146      * @param scaleCalculator
147      *******************************************************************************************/

148     public void createAxisScaleLabels( ScaleCalculator scaleCalculator )
149     {
150         double value = scaleCalculator.getMinValue();
151         this.addLabel( value );
152
153         for( int i = 1; i < scaleCalculator.getNumberOfScaleItems(); i++ )
154         {
155             value += scaleCalculator.getIncrement();
156             this.addLabel( value );
157         }
158     }
159
160 }
161
Popular Tags