1 46 47 package org.jfree.data.contour; 48 49 import java.util.Arrays ; 50 import java.util.Date ; 51 import java.util.Vector ; 52 53 import org.jfree.data.Range; 54 import org.jfree.data.xy.AbstractXYZDataset; 55 import org.jfree.data.xy.XYDataset; 56 57 63 public class DefaultContourDataset extends AbstractXYZDataset 64 implements ContourDataset { 65 66 67 protected Comparable seriesKey = null; 68 69 70 protected Number [] xValues = null; 71 72 73 protected Number [] yValues = null; 74 75 76 protected Number [] zValues = null; 77 78 79 protected int[] xIndex = null; 80 81 82 boolean[] dateAxis = new boolean[3]; 83 84 87 public DefaultContourDataset() { 88 super(); 89 } 90 91 99 public DefaultContourDataset(Comparable seriesKey, 100 Object [] xData, 101 Object [] yData, 102 Object [] zData) { 103 104 this.seriesKey = seriesKey; 105 initialize(xData, yData, zData); 106 } 107 108 115 public void initialize(Object [] xData, 116 Object [] yData, 117 Object [] zData) { 118 119 this.xValues = new Double [xData.length]; 120 this.yValues = new Double [yData.length]; 121 this.zValues = new Double [zData.length]; 122 123 131 Vector tmpVector = new Vector (); double x = 1.123452e31; for (int k = 0; k < this.xValues.length; k++) { 134 if (xData[k] != null) { 135 Number xNumber; 136 if (xData[k] instanceof Number ) { 137 xNumber = (Number ) xData[k]; 138 } 139 else if (xData[k] instanceof Date ) { 140 this.dateAxis[0] = true; 141 Date xDate = (Date ) xData[k]; 142 xNumber = new Long (xDate.getTime()); } 144 else { 145 xNumber = new Integer (0); 146 } 147 this.xValues[k] = new Double (xNumber.doubleValue()); 148 150 if (x != this.xValues[k].doubleValue()) { 152 tmpVector.add(new Integer (k)); x = this.xValues[k].doubleValue(); 155 } 157 } 158 } 159 160 Object [] inttmp = tmpVector.toArray(); 161 this.xIndex = new int[inttmp.length]; 164 for (int i = 0; i < inttmp.length; i++) { 165 this.xIndex[i] = ((Integer ) inttmp[i]).intValue(); 166 } 167 for (int k = 0; k < this.yValues.length; k++) { this.yValues[k] = (Double ) yData[k]; 170 if (zData[k] != null) { 171 this.zValues[k] = (Double ) zData[k]; 172 } 173 } 174 } 175 176 183 public static Object [][] formObjectArray(double[][] data) { 184 Object [][] object = new Double [data.length][data[0].length]; 185 186 for (int i = 0; i < object.length; i++) { 187 for (int j = 0; j < object[i].length; j++) { 188 object[i][j] = new Double (data[i][j]); 189 } 190 } 191 return object; 192 } 193 194 201 public static Object [] formObjectArray(double[] data) { 202 Object [] object = new Double [data.length]; 203 for (int i = 0; i < object.length; i++) { 204 object[i] = new Double (data[i]); 205 } 206 return object; 207 } 208 209 217 public int getItemCount(int series) { 218 if (series > 0) { 219 throw new IllegalArgumentException ("Only one series for contour"); 220 } 221 return this.zValues.length; 222 } 223 224 229 public double getMaxZValue() { 230 double zMax = -1.e20; 231 for (int k = 0; k < this.zValues.length; k++) { 232 if (this.zValues[k] != null) { 233 zMax = Math.max(zMax, this.zValues[k].doubleValue()); 234 } 235 } 236 return zMax; 237 } 238 239 244 public double getMinZValue() { 245 double zMin = 1.e20; 246 for (int k = 0; k < this.zValues.length; k++) { 247 if (this.zValues[k] != null) { 248 zMin = Math.min(zMin, this.zValues[k].doubleValue()); 249 } 250 } 251 return zMin; 252 } 253 254 262 public Range getZValueRange(Range x, Range y) { 263 264 double minX = x.getLowerBound(); 265 double minY = y.getLowerBound(); 266 double maxX = x.getUpperBound(); 267 double maxY = y.getUpperBound(); 268 269 double zMin = 1.e20; 270 double zMax = -1.e20; 271 for (int k = 0; k < this.zValues.length; k++) { 272 if (this.xValues[k].doubleValue() >= minX 273 && this.xValues[k].doubleValue() <= maxX 274 && this.yValues[k].doubleValue() >= minY 275 && this.yValues[k].doubleValue() <= maxY) { 276 if (this.zValues[k] != null) { 277 zMin = Math.min(zMin, this.zValues[k].doubleValue()); 278 zMax = Math.max(zMax, this.zValues[k].doubleValue()); 279 } 280 } 281 } 282 283 return new Range(zMin, zMax); 284 } 285 286 296 public double getMinZValue(double minX, 297 double minY, 298 double maxX, 299 double maxY) { 300 301 double zMin = 1.e20; 302 for (int k = 0; k < this.zValues.length; k++) { 303 if (this.zValues[k] != null) { 304 zMin = Math.min(zMin, this.zValues[k].doubleValue()); 305 } 306 } 307 return zMin; 308 309 } 310 311 318 public int getSeriesCount() { 319 return 1; 320 } 321 322 331 public Comparable getSeriesKey(int series) { 332 if (series > 0) { 333 throw new IllegalArgumentException ("Only one series for contour"); 334 } 335 return this.seriesKey; 336 } 337 338 343 public int[] getXIndices() { 344 return this.xIndex; 345 } 346 347 352 public Number [] getXValues() { 353 return this.xValues; 354 } 355 356 365 public Number getX(int series, int item) { 366 if (series > 0) { 367 throw new IllegalArgumentException ("Only one series for contour"); 368 } 369 return this.xValues[item]; 370 } 371 372 379 public Number getXValue(int item) { 380 return this.xValues[item]; 381 } 382 383 388 public Number [] getYValues() { 389 return this.yValues; 390 } 391 392 401 public Number getY(int series, int item) { 402 if (series > 0) { 403 throw new IllegalArgumentException ("Only one series for contour"); 404 } 405 return this.yValues[item]; 406 } 407 408 413 public Number [] getZValues() { 414 return this.zValues; 415 } 416 417 426 public Number getZ(int series, int item) { 427 if (series > 0) { 428 throw new IllegalArgumentException ("Only one series for contour"); 429 } 430 return this.zValues[item]; 431 } 432 433 438 public int[] indexX() { 439 int[] index = new int[this.xValues.length]; 440 for (int k = 0; k < index.length; k++) { 441 index[k] = indexX(k); 442 } 443 return index; 444 } 445 446 453 public int indexX(int k) { 454 int i = Arrays.binarySearch(this.xIndex, k); 455 if (i >= 0) { 456 return i; 457 } 458 else { 459 return -1 * i - 2; 460 } 461 } 462 463 464 471 public int indexY(int k) { return (k / this.xValues.length); 473 } 474 475 483 public int indexZ(int i, int j) { 484 return this.xValues.length * j + i; 485 } 486 487 494 public boolean isDateAxis(int axisNumber) { 495 if (axisNumber < 0 || axisNumber > 2) { 496 return false; } 498 return this.dateAxis[axisNumber]; 499 } 500 501 506 public void setSeriesKeys(Comparable [] seriesKeys) { 507 if (seriesKeys.length > 1) { 508 throw new IllegalArgumentException ( 509 "Contours only support one series" 510 ); 511 } 512 this.seriesKey = seriesKeys[0]; 513 fireDatasetChanged(); 514 } 515 516 } 517 | Popular Tags |