1 55 56 package org.jfree.data.general; 57 58 import java.util.List ; 59 60 import org.jfree.data.xy.AbstractIntervalXYDataset; 61 import org.jfree.data.xy.OHLCDataset; 62 import org.jfree.data.xy.IntervalXYDataset; 63 import org.jfree.data.xy.XYDataset; 64 65 72 public class CombinedDataset extends AbstractIntervalXYDataset 73 implements XYDataset, 74 OHLCDataset, 75 IntervalXYDataset, 76 CombinationDataset { 77 78 79 private List datasetInfo = new java.util.ArrayList (); 80 81 84 public CombinedDataset() { 85 super(); 86 } 87 88 94 public CombinedDataset(SeriesDataset[] data) { 95 add(data); 96 } 97 98 104 public void add(SeriesDataset data) { 105 fastAdd(data); 106 DatasetChangeEvent event = new DatasetChangeEvent(this, this); 107 notifyListeners(event); 108 } 109 110 116 public void add(SeriesDataset[] data) { 117 118 for (int i = 0; i < data.length; i++) { 119 fastAdd(data[i]); 120 } 121 DatasetChangeEvent event = new DatasetChangeEvent(this, this); 122 notifyListeners(event); 123 124 } 125 126 133 public void add(SeriesDataset data, int series) { 134 add(new SubSeriesDataset(data, series)); 135 } 136 137 142 private void fastAdd(SeriesDataset data) { 143 for (int i = 0; i < data.getSeriesCount(); i++) { 144 this.datasetInfo.add(new DatasetInfo(data, i)); 145 } 146 } 147 148 152 157 public int getSeriesCount() { 158 return this.datasetInfo.size(); 159 } 160 161 168 public Comparable getSeriesKey(int series) { 169 DatasetInfo di = getDatasetInfo(series); 170 return di.data.getSeriesKey(di.series); 171 } 172 173 177 188 public Number getX(int series, int item) { 189 DatasetInfo di = getDatasetInfo(series); 190 return ((XYDataset) di.data).getX(di.series, item); 191 } 192 193 204 public Number getY(int series, int item) { 205 DatasetInfo di = getDatasetInfo(series); 206 return ((XYDataset) di.data).getY(di.series, item); 207 } 208 209 219 public int getItemCount(int series) { 220 DatasetInfo di = getDatasetInfo(series); 221 return ((XYDataset) di.data).getItemCount(di.series); 222 } 223 224 228 239 public Number getHigh(int series, int item) { 240 DatasetInfo di = getDatasetInfo(series); 241 return ((OHLCDataset) di.data).getHigh(di.series, item); 242 } 243 244 253 public double getHighValue(int series, int item) { 254 double result = Double.NaN; 255 Number high = getHigh(series, item); 256 if (high != null) { 257 result = high.doubleValue(); 258 } 259 return result; 260 } 261 262 273 public Number getLow(int series, int item) { 274 DatasetInfo di = getDatasetInfo(series); 275 return ((OHLCDataset) di.data).getLow(di.series, item); 276 } 277 278 287 public double getLowValue(int series, int item) { 288 double result = Double.NaN; 289 Number low = getLow(series, item); 290 if (low != null) { 291 result = low.doubleValue(); 292 } 293 return result; 294 } 295 296 307 public Number getOpen(int series, int item) { 308 DatasetInfo di = getDatasetInfo(series); 309 return ((OHLCDataset) di.data).getOpen(di.series, item); 310 } 311 312 321 public double getOpenValue(int series, int item) { 322 double result = Double.NaN; 323 Number open = getOpen(series, item); 324 if (open != null) { 325 result = open.doubleValue(); 326 } 327 return result; 328 } 329 330 341 public Number getClose(int series, int item) { 342 DatasetInfo di = getDatasetInfo(series); 343 return ((OHLCDataset) di.data).getClose(di.series, item); 344 } 345 346 355 public double getCloseValue(int series, int item) { 356 double result = Double.NaN; 357 Number close = getClose(series, item); 358 if (close != null) { 359 result = close.doubleValue(); 360 } 361 return result; 362 } 363 364 375 public Number getVolume(int series, int item) { 376 DatasetInfo di = getDatasetInfo(series); 377 return ((OHLCDataset) di.data).getVolume(di.series, item); 378 } 379 380 389 public double getVolumeValue(int series, int item) { 390 double result = Double.NaN; 391 Number volume = getVolume(series, item); 392 if (volume != null) { 393 result = volume.doubleValue(); 394 } 395 return result; 396 } 397 398 402 410 public Number getStartX(int series, int item) { 411 DatasetInfo di = getDatasetInfo(series); 412 if (di.data instanceof IntervalXYDataset) { 413 return ((IntervalXYDataset) di.data).getStartX(di.series, item); 414 } 415 else { 416 return getX(series, item); 417 } 418 } 419 420 428 public Number getEndX(int series, int item) { 429 DatasetInfo di = getDatasetInfo(series); 430 if (di.data instanceof IntervalXYDataset) { 431 return ((IntervalXYDataset) di.data).getEndX(di.series, item); 432 } 433 else { 434 return getX(series, item); 435 } 436 } 437 438 446 public Number getStartY(int series, int item) { 447 DatasetInfo di = getDatasetInfo(series); 448 if (di.data instanceof IntervalXYDataset) { 449 return ((IntervalXYDataset) di.data).getStartY(di.series, item); 450 } 451 else { 452 return getY(series, item); 453 } 454 } 455 456 464 public Number getEndY(int series, int item) { 465 DatasetInfo di = getDatasetInfo(series); 466 if (di.data instanceof IntervalXYDataset) { 467 return ((IntervalXYDataset) di.data).getEndY(di.series, item); 468 } 469 else { 470 return getY(series, item); 471 } 472 } 473 474 478 485 public SeriesDataset getParent() { 486 487 SeriesDataset parent = null; 488 for (int i = 0; i < this.datasetInfo.size(); i++) { 489 SeriesDataset child = getDatasetInfo(i).data; 490 if (child instanceof CombinationDataset) { 491 SeriesDataset childParent 492 = ((CombinationDataset) child).getParent(); 493 if (parent == null) { 494 parent = childParent; 495 } 496 else if (parent != childParent) { 497 return null; 498 } 499 } 500 else { 501 return null; 502 } 503 } 504 return parent; 505 506 } 507 508 518 public int[] getMap() { 519 520 int[] map = null; 521 for (int i = 0; i < this.datasetInfo.size(); i++) { 522 SeriesDataset child = getDatasetInfo(i).data; 523 if (child instanceof CombinationDataset) { 524 int[] childMap = ((CombinationDataset) child).getMap(); 525 if (childMap == null) { 526 return null; 527 } 528 map = joinMap(map, childMap); 529 } 530 else { 531 return null; 532 } 533 } 534 return map; 535 } 536 537 541 548 public int getChildPosition(Dataset child) { 549 550 int n = 0; 551 for (int i = 0; i < this.datasetInfo.size(); i++) { 552 SeriesDataset childDataset = getDatasetInfo(i).data; 553 if (childDataset instanceof CombinedDataset) { 554 int m = ((CombinedDataset) childDataset) 555 .getChildPosition(child); 556 if (m >= 0) { 557 return n + m; 558 } 559 n++; 560 } 561 else { 562 if (child == childDataset) { 563 return n; 564 } 565 n++; 566 } 567 } 568 return -1; 569 } 570 571 575 582 private DatasetInfo getDatasetInfo(int series) { 583 return (DatasetInfo) this.datasetInfo.get(series); 584 } 585 586 594 private int[] joinMap(int[] a, int[] b) { 595 if (a == null) { 596 return b; 597 } 598 if (b == null) { 599 return a; 600 } 601 int[] result = new int[a.length + b.length]; 602 System.arraycopy(a, 0, result, 0, a.length); 603 System.arraycopy(b, 0, result, a.length, b.length); 604 return result; 605 } 606 607 611 private class DatasetInfo { 612 613 614 private SeriesDataset data; 615 616 617 private int series; 618 619 625 DatasetInfo(SeriesDataset data, int series) { 626 this.data = data; 627 this.series = series; 628 } 629 } 630 631 } 632 | Popular Tags |