KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > ValueGrid


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org)
7  *
8  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.graph;
26
27 import org.jrobin.core.RrdException;
28
29 /**
30  * <p>Holds specific information about the Value axis grid of the chart.</p>
31  *
32  * @author Arne Vandamme (cobralord@jrobin.org)
33  */

34 class ValueGrid
35 {
36     // ================================================================
37
// -- Members
38
// ================================================================
39
private boolean rigid;
40     private double lower;
41     private double upper;
42     
43     private double baseValue = ValueFormatter.DEFAULT_BASE;
44     private double[] scaleValues = new double[] {
45                                             1e18, 1e15, 1e12, 1e9, 1e6, 1e3, 1e0, 1e-3, 1e-6, 1e-9, 1e-12, 1e-15
46                                         };
47     
48     private ValueAxisUnit vAxis;
49     
50     
51     // ================================================================
52
// -- Constructors
53
// ================================================================
54
/**
55      * Creates a value grid based on a value range and possibly a value axis
56      * unit specification. The grid can also be specified to be rigid, to prevent
57      * auto scaling of the displayed value range.
58      * @param gr Grid range object.
59      * @param low Lower value of the value range.
60      * @param up Upper value of the value range.
61      * @param vAxis ValueAxisUnit specified to determine the grid lines, if the given
62      * ValueAxisUnit is null, one will be automatically determined.
63      */

64     ValueGrid( GridRange gr, double low, double up, ValueAxisUnit vAxis, double base ) throws RrdException
65     {
66         double grLower = Double.MAX_VALUE;
67         double grUpper = Double.MIN_VALUE;
68
69         if ( gr != null )
70         {
71             this.rigid = gr.isRigid();
72             grLower = gr.getLowerValue();
73             grUpper = gr.getUpperValue();
74         }
75         
76         this.lower = low;
77         this.upper = up;
78         this.vAxis = vAxis;
79
80         // Fill in the scale values
81
if ( base != baseValue )
82         {
83             baseValue = base;
84
85             double tmp = 1;
86             for (int i = 1; i < 7; i++) {
87                 tmp *= baseValue;
88                 scaleValues[6 - i] = tmp;
89             }
90             tmp = 1;
91             for (int i = 7; i < scaleValues.length; i++) {
92                 tmp *= baseValue;
93                 scaleValues[i] = ( 1 / tmp );
94             }
95         }
96
97         // Set an appropriate value axis it not given yet
98
setValueAxis();
99
100         if ( !rigid ) {
101             this.lower = ( lower == grLower ? grLower : this.vAxis.getNiceLower( lower ) );
102             this.upper = ( upper == grUpper ? grUpper : this.vAxis.getNiceHigher( upper ) );
103         }
104     }
105     
106     
107     // ================================================================
108
// -- Protected methods
109
// ================================================================
110
double getLowerValue() {
111         return lower;
112     }
113
114     double getUpperValue() {
115         return upper;
116     }
117
118     ValueMarker[] getValueMarkers() {
119         return vAxis.getValueMarkers( lower, upper );
120     }
121     
122         
123     // ================================================================
124
// -- Private methods
125
// ================================================================
126
/**
127      * Determines a good ValueAxisUnit to use for grid calculation.
128      * A decent grid is selected based on the value range being used in the chart.
129      */

130     private void setValueAxis() throws RrdException
131     {
132         if ( vAxis != null )
133             return;
134
135         if ( Double.isNaN(upper) || upper == Double.MIN_VALUE || upper == Double.MAX_VALUE )
136             upper = 0.9;
137         if ( Double.isNaN(lower) || lower == Double.MAX_VALUE || lower == Double.MIN_VALUE )
138             lower = 0;
139
140         if ( !rigid && upper == 0 && upper == lower )
141             upper = 0.9;
142
143         // Determine nice axis grid
144
double shifted = Math.abs(upper - lower);
145         if ( shifted == 0 ) // Special case, no 'range' available
146
shifted = upper;
147
148         // Find the scaled unit for this range
149
double mod = 1.0;
150         int scaleIndex = scaleValues.length - 1;
151         while ( scaleIndex >= 0 && scaleValues[scaleIndex] < shifted )
152             scaleIndex--;
153
154         // Keep the rest of division
155
if ( scaleValues[++scaleIndex] != 0 ) // Don't divide by zero, it is silly
156
shifted = shifted / scaleValues[scaleIndex];
157
158         // Safety check to avoid infinite loop
159
if ( Double.isInfinite(shifted) )
160             throw new RrdException( "ValueGrid failure: u=" + upper + " l=" + lower + " sv=" + scaleValues[scaleIndex] );
161
162         // While rest > 10, divide by 10
163
while ( shifted > 10.0 ) {
164             shifted /= 10;
165             mod *= 10;
166         }
167
168         while ( shifted < 1.0 ) {
169             shifted *= 10;
170             mod /= 10;
171         }
172
173         // Create nice grid based on 'fixed' ranges
174
if ( shifted <= 2 )
175             vAxis = new ValueAxisUnit( 0.1 * mod * scaleValues[scaleIndex], 0.5 * mod * scaleValues[scaleIndex] );
176         else if ( shifted <= 4 )
177             vAxis = new ValueAxisUnit( 0.2 * mod * scaleValues[scaleIndex], 1.0 * mod * scaleValues[scaleIndex] );
178         else if ( shifted <= 6 )
179             vAxis = new ValueAxisUnit( 0.5 * mod * scaleValues[scaleIndex], 1.0 * mod * scaleValues[scaleIndex] );
180         else
181             vAxis = new ValueAxisUnit( 1.0 * mod * scaleValues[scaleIndex], 2.0 * mod * scaleValues[scaleIndex] );
182     }
183 }
184
Popular Tags