1 25 package org.jrobin.graph; 26 27 import java.util.Locale ; 28 import java.text.NumberFormat ; 29 import java.text.DecimalFormat ; 30 31 39 class ValueFormatter 40 { 41 protected static final int NO_SCALE = -1; 45 protected static final double DEFAULT_BASE = 1000.0; 46 47 private double base = DEFAULT_BASE; 48 private double[] scaleValues = new double[] { 49 1e18, 1e15, 1e12, 1e9, 1e6, 1e3, 1e0, 1e-3, 1e-6, 1e-9, 1e-12, 1e-15 50 }; 51 protected static String [] PREFIXES = new String [] { 52 "E", "P", "T", "G", "M", "k", " ", "m", "µ", "n", "p", "f" 53 }; 54 55 private double value; 56 private DecimalFormat decFormat; 57 private int formattedStrLen; 58 59 private double scaledValue; 60 private int scaleIndex = NO_SCALE; private int fixedIndex = NO_SCALE; 62 private String prefix; 63 64 private boolean scale = false; 65 66 67 ValueFormatter() { 71 } 72 73 78 ValueFormatter( double base, int scaleIndex ) 79 { 80 setBase( base ); 81 this.fixedIndex = scaleIndex; 82 } 83 84 85 96 void setFormat( double value, int numDec, int strLen ) 97 { 98 this.value = value; 99 this.decFormat = getDecimalFormat( numDec ); 100 this.formattedStrLen = strLen; 101 } 102 103 110 void setScaling( boolean normalScale, boolean uniformScale ) 111 { 112 if ( !uniformScale ) 113 scaleIndex = NO_SCALE; 114 115 if ( fixedIndex >= 0 ) { 116 scale = true; 117 scaleIndex = fixedIndex; 118 } 119 else { 120 scale = (normalScale || uniformScale); 121 } 122 } 123 124 128 String getFormattedValue() 129 { 130 String valueStr = "" + value; 131 132 if ( !Double.isNaN(value) ) { 133 if ( scale ) { 134 scaleValue( scaleIndex ); 135 valueStr = decFormat.format(scaledValue); 136 } 137 else 138 valueStr = decFormat.format(value); 139 } 140 141 int diff = formattedStrLen - valueStr.length(); 143 144 StringBuffer preSpace = new StringBuffer (""); 145 for (int i = 0; i < diff; i++) 146 preSpace.append(' '); 147 148 valueStr = preSpace.append(valueStr).toString(); 149 150 return valueStr; 151 } 152 153 157 String getScaledValue() 158 { 159 int tsv = scaleIndex; 160 scaleValue( tsv ); 161 long intVal = new Double ( scaledValue ).longValue(); 162 163 scaleIndex = tsv; 164 165 if ( intVal == scaledValue ) 166 return "" + intVal; 167 else 168 return "" + scaledValue; 169 } 170 171 175 void setBase( double baseValue ) 176 { 177 if ( baseValue == this.base ) 178 return; 179 180 this.base = baseValue; 181 double tmp = 1; 182 for (int i = 1; i < 7; i++) { 183 tmp *= baseValue; 184 scaleValues[6 - i] = tmp; 185 } 186 tmp = 1; 187 for (int i = 7; i < scaleValues.length; i++) { 188 tmp *= baseValue; 189 scaleValues[i] = ( 1 / tmp ); 190 } 191 } 192 193 double getBase() { 194 return base; 195 } 196 197 String getPrefix() { 198 return prefix; 199 } 200 201 202 209 private void scaleValue( int scaleIndex ) 210 { 211 double absValue = Math.abs(value); 212 if (scaleIndex == NO_SCALE) 213 { 214 this.prefix = " "; 215 this.scaledValue = value; 216 217 for (int i = 0; i < scaleValues.length; i++) 218 { 219 if (absValue >= scaleValues[i] && absValue < scaleValues[i] * base) 220 { 221 if ( scaleValues[i] != 1e-3 ) { 223 this.prefix = PREFIXES[i]; 224 this.scaledValue = value / scaleValues[i]; 225 this.scaleIndex = i; 226 return; 227 } 228 } 229 } 230 } 231 else { 232 this.prefix = PREFIXES[scaleIndex]; 233 this.scaledValue = value / scaleValues[scaleIndex]; 234 this.scaleIndex = scaleIndex; 235 } 236 } 237 238 244 private DecimalFormat getDecimalFormat( int numDec ) 245 { 246 StringBuffer formatStr = new StringBuffer ("###0"); for(int i = 0; i < numDec; i++) { 248 if(i == 0) { 249 formatStr.append('.'); 250 } 251 formatStr.append('0'); 252 } 253 254 NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH); 255 DecimalFormat df = (DecimalFormat ) nf; 256 df.applyPattern( formatStr.toString() ); 257 258 return df; 259 } 260 } 261 | Popular Tags |