1 25 package org.jrobin.graph; 26 27 import org.jrobin.core.RrdException; 28 29 34 class ValueGrid 35 { 36 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 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 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 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 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 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 double shifted = Math.abs(upper - lower); 145 if ( shifted == 0 ) shifted = upper; 147 148 double mod = 1.0; 150 int scaleIndex = scaleValues.length - 1; 151 while ( scaleIndex >= 0 && scaleValues[scaleIndex] < shifted ) 152 scaleIndex--; 153 154 if ( scaleValues[++scaleIndex] != 0 ) shifted = shifted / scaleValues[scaleIndex]; 157 158 if ( Double.isInfinite(shifted) ) 160 throw new RrdException( "ValueGrid failure: u=" + upper + " l=" + lower + " sv=" + scaleValues[scaleIndex] ); 161 162 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 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 |