1 44 45 package org.jfree.data; 46 47 import java.util.List ; 48 49 55 public class CombinedDataset extends AbstractSeriesDataset 56 implements XYDataset, HighLowDataset, IntervalXYDataset, 57 CombinationDataset { 58 59 60 private List datasetInfo = new java.util.ArrayList (); 61 62 65 public CombinedDataset() { 66 } 67 68 73 public CombinedDataset(SeriesDataset[] data) { 74 add(data); 75 } 76 77 82 public void add(SeriesDataset data) { 83 84 fastAdd(data); 85 DatasetChangeEvent event = new DatasetChangeEvent(this, this); 86 notifyListeners(event); 87 88 } 89 90 96 public void add(SeriesDataset[] data) { 97 98 for (int i = 0; i < data.length; i++) { 99 fastAdd(data[i]); 100 } 101 DatasetChangeEvent event = new DatasetChangeEvent(this, this); 102 notifyListeners(event); 103 104 } 105 106 113 public void add(SeriesDataset data, int series) { 114 add(new SubSeriesDataset(data, series)); 115 } 116 117 122 private void fastAdd(SeriesDataset data) { 123 for (int i = 0; i < data.getSeriesCount(); i++) { 124 datasetInfo.add(new DatasetInfo(data, i)); 125 } 126 } 127 128 132 137 public int getSeriesCount() { 138 return datasetInfo.size(); 139 } 140 141 148 public String getSeriesName(int series) { 149 DatasetInfo di = getDatasetInfo(series); 150 return di.data.getSeriesName(di.series); 151 } 152 153 157 167 public Number getXValue(int series, int item) { 168 DatasetInfo di = getDatasetInfo(series); 169 return ((XYDataset) di.data).getXValue(di.series, item); 170 } 171 172 182 public Number getYValue(int series, int item) { 183 DatasetInfo di = getDatasetInfo(series); 184 return ((XYDataset) di.data).getYValue(di.series, item); 185 } 186 187 196 public int getItemCount(int series) { 197 DatasetInfo di = getDatasetInfo(series); 198 return ((XYDataset) di.data).getItemCount(di.series); 199 } 200 201 205 216 public Number getHighValue(int series, int item) { 217 DatasetInfo di = getDatasetInfo(series); 218 return ((HighLowDataset) di.data).getHighValue(di.series, item); 219 } 220 221 232 public Number getLowValue(int series, int item) { 233 DatasetInfo di = getDatasetInfo(series); 234 return ((HighLowDataset) di.data).getLowValue(di.series, item); 235 } 236 237 248 public Number getOpenValue(int series, int item) { 249 DatasetInfo di = getDatasetInfo(series); 250 return ((HighLowDataset) di.data).getOpenValue(di.series, item); 251 } 252 253 264 public Number getCloseValue(int series, int item) { 265 DatasetInfo di = getDatasetInfo(series); 266 return ((HighLowDataset) di.data).getCloseValue(di.series, item); 267 } 268 269 280 public Number getVolumeValue(int series, int item) { 281 DatasetInfo di = getDatasetInfo(series); 282 return ((HighLowDataset) di.data).getVolumeValue(di.series, item); 283 } 284 285 289 297 public Number getStartXValue(int series, int item) { 298 DatasetInfo di = getDatasetInfo(series); 299 if (di.data instanceof IntervalXYDataset) { 300 return ((IntervalXYDataset) di.data).getStartXValue(di.series, item); 301 } 302 else { 303 return getXValue(series, item); 304 } 305 } 306 307 315 public Number getEndXValue(int series, int item) { 316 DatasetInfo di = getDatasetInfo(series); 317 if (di.data instanceof IntervalXYDataset) { 318 return ((IntervalXYDataset) di.data).getEndXValue(di.series, item); 319 } 320 else { 321 return getXValue(series, item); 322 } 323 } 324 325 333 public Number getStartYValue(int series, int item) { 334 DatasetInfo di = getDatasetInfo(series); 335 if (di.data instanceof IntervalXYDataset) { 336 return ((IntervalXYDataset) di.data).getStartYValue(di.series, item); 337 } 338 else { 339 return getYValue(series, item); 340 } 341 } 342 343 351 public Number getEndYValue(int series, int item) { 352 DatasetInfo di = getDatasetInfo(series); 353 if (di.data instanceof IntervalXYDataset) { 354 return ((IntervalXYDataset) di.data).getEndYValue(di.series, item); 355 } 356 else { 357 return getYValue(series, item); 358 } 359 } 360 361 365 372 public SeriesDataset getParent() { 373 374 SeriesDataset parent = null; 375 for (int i = 0; i < datasetInfo.size(); i++) { 376 SeriesDataset child = getDatasetInfo(i).data; 377 if (child instanceof CombinationDataset) { 378 SeriesDataset childParent = ((CombinationDataset) child).getParent(); 379 if (parent == null) { 380 parent = childParent; 381 } 382 else if (parent != childParent) { 383 return null; 384 } 385 } 386 else { 387 return null; 388 } 389 } 390 return parent; 391 392 } 393 394 404 public int[] getMap() { 405 406 int[] map = null; 407 for (int i = 0; i < datasetInfo.size(); i++) { 408 SeriesDataset child = getDatasetInfo(i).data; 409 if (child instanceof CombinationDataset) { 410 int[] childMap = ((CombinationDataset) child).getMap(); 411 if (childMap == null) { 412 return null; 413 } 414 map = joinMap(map, childMap); 415 } 416 else { 417 return null; 418 } 419 } 420 return map; 421 } 422 423 427 434 public int getChildPosition(Dataset child) { 435 436 int n = 0; 437 for (int i = 0; i < datasetInfo.size(); i++) { 438 SeriesDataset childDataset = getDatasetInfo(i).data; 439 if (childDataset instanceof CombinedDataset) { 440 int m = ((CombinedDataset) childDataset).getChildPosition(child); 441 if (m >= 0) { 442 return n + m; 443 } 444 n++; 445 } 446 else { 447 if (child == childDataset) { 448 return n; 449 } 450 n++; 451 } 452 } 453 return -1; 454 } 455 456 460 467 private DatasetInfo getDatasetInfo(int series) { 468 return (DatasetInfo) datasetInfo.get(series); 469 } 470 471 479 private int[] joinMap(int[] a, int[] b) { 480 if (a == null) { 481 return b; 482 } 483 if (b == null) { 484 return a; 485 } 486 int[] result = new int[a.length + b.length]; 487 System.arraycopy(a, 0, result, 0, a.length); 488 System.arraycopy(b, 0, result, a.length, b.length); 489 return result; 490 } 491 492 497 private class DatasetInfo { 498 499 500 private SeriesDataset data; 501 502 503 private int series; 504 505 511 DatasetInfo(SeriesDataset data, int series) { 512 this.data = data; 513 this.series = series; 514 } 515 } 516 517 } 518 | Popular Tags |