1 19 package jcckit.plot; 20 21 import java.util.Properties ; 22 23 import jcckit.graphic.BasicGraphicAttributes; 24 import jcckit.graphic.GraphPoint; 25 import jcckit.graphic.LineAttributes; 26 import jcckit.graphic.ShapeAttributes; 27 import jcckit.graphic.TextAttributes; 28 import jcckit.util.ConfigData; 29 import jcckit.util.ConfigParameters; 30 import jcckit.util.ConfigParametersBasedConfigData; 31 import jcckit.util.Factory; 32 import jcckit.util.Format; 33 import jcckit.util.PropertiesBasedConfigData; 34 import jcckit.util.TicLabelFormat; 35 import jcckit.util.Util; 36 37 52 public class AxisParameters { 53 54 public static final String LOG_SCALE_KEY = "logScale", 55 MINIMUM_KEY = "minimum", 56 MAXIMUM_KEY = "maximum", 57 AXIS_LENGTH_KEY = "axisLength", 58 AXIS_ATTRIBUTES_KEY = "axisAttributes", 59 AXIS_LABEL_KEY = "axisLabel", 60 AXIS_LABEL_POSITION_KEY = "axisLabelPosition", 61 AXIS_LABEL_ATTRIBUTES_KEY = "axisLabelAttributes", 62 AUTOMATIC_TIC_CALCULATION_KEY 63 = "automaticTicCalculation", 64 MINIMUM_TIC_KEY = "minimumTic", 65 MAXIMUM_TIC_KEY = "maximumTic", 66 NUMBER_OF_TICS_KEY = "numberOfTics", 67 TIC_LENGTH_KEY = "ticLength", 68 TIC_ATTRIBUTES_KEY = "ticAttributes", 69 TIC_LABEL_FORMAT_KEY = "ticLabelFormat", 70 TIC_LABEL_POSITION_KEY = "ticLabelPosition", 71 TIC_LABEL_ATTRIBUTES_KEY = "ticLabelAttributes", 72 GRID_KEY = "grid", 73 GRID_ATTRIBUTES_KEY = "gridAttributes"; 74 75 private static final double LN10 = Math.log(10); 76 77 78 boolean logScale; 79 80 double minimum; 81 82 double maximum; 83 84 double axisLength; 85 89 LineAttributes axisAttributes; 90 91 boolean automaticTicCalculation; 92 double minimumTic; 93 double maximumTic; 94 int numberOfTics; 95 99 double ticLength; 100 104 LineAttributes ticAttributes; 105 106 TicLabelFormat ticLabelFormat; 107 108 GraphPoint ticLabelPosition; 109 110 TextAttributes ticLabelAttributes; 111 112 113 boolean grid; 114 118 LineAttributes gridAttributes; 119 120 121 String axisLabel; 122 123 GraphPoint axisLabelPosition; 124 125 TextAttributes axisLabelAttributes; 126 127 132 double[] calculateTics() { 133 if (automaticTicCalculation) { 134 calculateTicsParameters(); 135 } 136 double[] result = new double[numberOfTics]; 137 if (numberOfTics > 0) { 138 double b = Util.log(minimumTic, logScale); 139 double a = Util.log(maximumTic, logScale); 140 a = numberOfTics > 1 ? (a - b) / (numberOfTics - 1) : 0; 141 for (int i = 0; i < result.length; i++) { 142 result[i] = Util.exp(a * i + b, logScale); 143 } 144 result[0] = adjust(minimum, result[0]); 145 result[numberOfTics - 1] = adjust(maximum, result[numberOfTics - 1]); 146 } 147 return result; 148 } 149 150 private void calculateTicsParameters() { 151 double min = Math.min(minimum, maximum); 152 double max = Math.max(minimum, maximum); 153 if (logScale) { 154 int minExponent = (int) (199.9999 + Math.log(min) / LN10) - 199; 155 int maxExponent = (int) (200.0001 + Math.log(max) / LN10) - 200; 156 minimumTic = Math.exp(LN10 * minExponent); 157 maximumTic = Math.exp(LN10 * maxExponent); 158 numberOfTics = maxExponent - minExponent + 1; 159 } else { 160 int baseExponent = (int) (199.69 + Math.log(max - min) / LN10) - 200; 161 double base = 0.2 * Math.exp(LN10 * baseExponent); 162 do 163 { 164 base *= 5; 165 int minInt = (int) (999999.999999 + min / base) - 999999; 166 int maxInt = (int) (1000000.000001 + max / base) - 1000000; 167 minimumTic = minInt * base; 168 maximumTic = maxInt * base; 169 numberOfTics = maxInt - minInt + 1; 170 } while (numberOfTics > 11); 171 } 172 } 173 174 178 private static double adjust(double adjustingValue, double value) { 179 return value != 0 && Math.abs(adjustingValue / value - 1) < 1e-11 180 ? adjustingValue : value; 181 } 182 183 187 private static Properties createDefaultAxisProperties() { 188 Properties p = new Properties (); 189 p.put(LOG_SCALE_KEY, "false"); 190 p.put(MINIMUM_KEY, "0"); 191 p.put(MAXIMUM_KEY, "1"); 192 p.put(AXIS_LENGTH_KEY, "0.8"); 193 p.put(AXIS_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 194 ShapeAttributes.class.getName()); 195 p.put(AXIS_LABEL_KEY, "x"); 196 p.put(AXIS_LABEL_POSITION_KEY, "0 -0.05"); 197 p.put(AXIS_LABEL_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 198 BasicGraphicAttributes.class.getName()); 199 p.put(AXIS_LABEL_ATTRIBUTES_KEY + '/' 200 + BasicGraphicAttributes.HORIZONTAL_ANCHOR_KEY, "center"); 201 p.put(AUTOMATIC_TIC_CALCULATION_KEY, "true"); 202 p.put(TIC_LENGTH_KEY, "0.01"); 203 p.put(TIC_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 204 ShapeAttributes.class.getName()); 205 p.put(TIC_LABEL_POSITION_KEY, "0 -0.01"); 206 p.put(TIC_LABEL_FORMAT_KEY, "%1.1f"); 207 p.put(TIC_LABEL_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 208 BasicGraphicAttributes.class.getName()); 209 p.put(TIC_LABEL_ATTRIBUTES_KEY + '/' 210 + BasicGraphicAttributes.HORIZONTAL_ANCHOR_KEY, "center"); 211 p.put(TIC_LABEL_ATTRIBUTES_KEY + '/' 212 + BasicGraphicAttributes.VERTICAL_ANCHOR_KEY, "top"); 213 p.put(GRID_KEY, "false"); 214 p.put(GRID_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 215 ShapeAttributes.class.getName()); 216 return p; 217 } 218 219 223 private static Properties createDefaultXAxisProperties() { 224 Properties p = createDefaultAxisProperties(); 225 p.put(AXIS_LABEL_KEY, "x"); 226 p.put(AXIS_LABEL_POSITION_KEY, "0 -0.05"); 227 p.put(AXIS_LABEL_ATTRIBUTES_KEY + '/' 228 + BasicGraphicAttributes.VERTICAL_ANCHOR_KEY, "top"); 229 p.put(TIC_LABEL_POSITION_KEY, "0 -0.01"); 230 p.put(TIC_LABEL_ATTRIBUTES_KEY + '/' 231 + BasicGraphicAttributes.HORIZONTAL_ANCHOR_KEY, "center"); 232 p.put(TIC_LABEL_ATTRIBUTES_KEY + '/' 233 + BasicGraphicAttributes.VERTICAL_ANCHOR_KEY, "top"); 234 return p; 235 } 236 237 327 public static AxisParameters createXAxis(ConfigParameters config) { 328 return createAxis(config, createDefaultXAxisProperties()); 329 } 330 331 335 private static Properties createDefaultYAxisProperties() { 336 Properties p = createDefaultAxisProperties(); 337 p.put(AXIS_LENGTH_KEY, "0.45"); 338 p.put(AXIS_LABEL_KEY, "y"); 339 p.put(AXIS_LABEL_POSITION_KEY, "-0.1 0"); 340 p.put(AXIS_LABEL_ATTRIBUTES_KEY + '/' 341 + BasicGraphicAttributes.VERTICAL_ANCHOR_KEY, "bottom"); 342 p.put(AXIS_LABEL_ATTRIBUTES_KEY + '/' 343 + BasicGraphicAttributes.ORIENTATION_ANGLE_KEY, "90"); 344 p.put(TIC_LABEL_POSITION_KEY, "-0.01 0"); 345 p.put(TIC_LABEL_ATTRIBUTES_KEY + '/' 346 + BasicGraphicAttributes.HORIZONTAL_ANCHOR_KEY, "right"); 347 p.put(TIC_LABEL_ATTRIBUTES_KEY + '/' 348 + BasicGraphicAttributes.VERTICAL_ANCHOR_KEY, "center"); 349 return p; 350 } 351 352 442 public static AxisParameters createYAxis(ConfigParameters config) { 443 return createAxis(config, createDefaultYAxisProperties()); 444 } 445 446 private static AxisParameters createAxis(ConfigParameters config, 447 Properties p) { 448 ConfigData cd = new PropertiesBasedConfigData(p); 449 ConfigParameters c = new ConfigParameters(cd); 450 cd = new ConfigParametersBasedConfigData(config, c); 451 c = new ConfigParameters(cd); 452 453 AxisParameters a = new AxisParameters(); 454 a.logScale = c.getBoolean(LOG_SCALE_KEY); 455 a.minimum = c.getDouble(MINIMUM_KEY); 456 a.maximum = c.getDouble(MAXIMUM_KEY); 457 a.axisLength = c.getDouble(AXIS_LENGTH_KEY); 458 a.axisAttributes 459 = (LineAttributes) Factory.create(c.getNode(AXIS_ATTRIBUTES_KEY)); 460 a.axisLabel = c.get(AXIS_LABEL_KEY); 461 a.axisLabelPosition 462 = new GraphPoint(c.getDoubleArray(AXIS_LABEL_POSITION_KEY)); 463 a.axisLabelAttributes = (TextAttributes) Factory.create( 464 c.getNode(AXIS_LABEL_ATTRIBUTES_KEY)); 465 a.ticLength = c.getDouble(TIC_LENGTH_KEY); 466 a.automaticTicCalculation 467 = c.getBoolean(AUTOMATIC_TIC_CALCULATION_KEY); 468 if (!a.automaticTicCalculation) { 469 a.calculateTicsParameters(); a.minimumTic = c.getDouble(MINIMUM_TIC_KEY, a.minimumTic); 471 a.maximumTic = c.getDouble(MAXIMUM_TIC_KEY, a.maximumTic); 472 a.numberOfTics = c.getInt(NUMBER_OF_TICS_KEY, a.numberOfTics); 473 } 474 a.ticAttributes 475 = (LineAttributes) Factory.create(c.getNode(TIC_ATTRIBUTES_KEY)); 476 a.ticLabelFormat = createTicLabelFormat(c); 477 a.ticLabelPosition 478 = new GraphPoint(c.getDoubleArray(TIC_LABEL_POSITION_KEY)); 479 a.ticLabelAttributes = (TextAttributes) Factory.create( 480 c.getNode(TIC_LABEL_ATTRIBUTES_KEY)); 481 a.grid = c.getBoolean(GRID_KEY); 482 a.gridAttributes 483 = (LineAttributes) Factory.create(c.getNode(GRID_ATTRIBUTES_KEY)); 484 return a; 485 } 486 487 private static TicLabelFormat createTicLabelFormat(ConfigParameters c) 488 { 489 TicLabelFormat result = Format.create(c, TIC_LABEL_FORMAT_KEY); 490 ConfigParameters node = c.getNode(TIC_LABEL_FORMAT_KEY); 491 if (node.get(Factory.CLASS_NAME_KEY, null) != null) { 492 result = (TicLabelFormat) Factory.create(node); 493 } 494 return result; 495 } 496 } 497 | Popular Tags |