1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.util.Vector ; 28 import java.awt.geom.*; 29 30 31 59 public final class Dataset { 60 61 62 private Vector setVector; 63 private float greatest; 64 private float least; 65 66 private boolean needsUpdate = true; 67 private Vector needsUpdateVector = new Vector (5, 5); 68 private Vector chart2DVector = new Vector (5, 5); 69 70 71 75 public Dataset() { 76 77 needsUpdate = true; 78 setDatasetToDefaults(); 79 } 80 81 82 91 public Dataset (int sets, int cats, int items) { 92 93 needsUpdate = true; 94 constructor (sets, cats, items); 95 } 96 97 98 103 public Dataset (Dataset dataset) { 104 105 needsUpdate = true; 106 set (dataset); 107 } 108 109 110 114 public final void setDatasetToDefaults() { 115 116 needsUpdate = true; 117 constructor (0, 0, 0); 118 } 119 120 121 131 public final void set (int set, int cat, int item, float value) { 132 133 needsUpdate = true; 134 ((Vector )((Vector )setVector.get (set)).get (cat)).set (item, new Float (value)); 135 } 136 137 138 143 public final void set (Dataset dataset) { 144 145 needsUpdate = true; 146 constructor (dataset.getNumSets(), dataset.getNumCats(), dataset.getNumItems()); 147 int numSets = getNumSets(); 148 int numCats = getNumCats(); 149 int numItems = getNumItems(); 150 for (int i = 0; i < numSets; ++i) { 151 for (int j = 0; j < numCats; ++j) { 152 for (int k = 0; k < numItems; ++k) { 153 set (i, j, k, dataset.get (i, j, k)); 154 } 155 } 156 } 157 } 158 159 160 167 public final void setSize (int sets, int cats, int items) { 168 169 needsUpdate = true; 170 if (setVector.size() > sets) setVector.setSize (sets); 172 for (int i = setVector.size(); i < sets; ++i) setVector.add (new Vector (cats, 10)); 173 for (int i = 0; i < sets; ++i) { 175 Vector catVector = (Vector )setVector.get (i); 176 if (catVector.size() > cats) catVector.setSize (cats); 177 for (int j = catVector.size(); j < cats; ++j) catVector.add (new Vector (items, 10)); 178 } 179 for (int i = 0; i < sets; ++i) { 181 Vector catVector = (Vector )setVector.get (i); 182 for (int j = 0; j < cats; ++j) { 183 Vector itemVector = (Vector )catVector.get (j); 184 if (itemVector.size() > items) itemVector.setSize (items); 185 for (int k = itemVector.size(); k < items; ++k) itemVector.add (new Float (0f)); 186 } 187 } 188 } 189 190 191 201 public final float get (int set, int cat, int item) { 202 203 return ((Float )((Vector )((Vector )setVector.get (set)).get (cat)).get (item)).floatValue(); 204 } 205 206 207 211 public final int getNumSets() { 212 return setVector.size(); 213 } 214 215 216 222 public final int getNumCats() { 223 224 if (getNumSets() > 0) return ((Vector )setVector.get (0)).size(); 225 else return 0; 226 } 227 228 229 235 public final int getNumItems() { 236 237 if (getNumSets() > 0 && getNumCats() > 0) { 238 return ((Vector )(((Vector )setVector.get (0))).get (0)).size(); 239 } 240 else return 0; 241 } 242 243 244 249 public final float getGreatest() { 250 251 update(); 252 return greatest; 253 } 254 255 256 261 public final float getLeast() { 262 263 update(); 264 return least; 265 } 266 267 268 279 public final float getAverage (int set, int cat, int item) { 280 281 float average = 0f; 282 if (getNumSets() > 0 && getNumCats() > 0 && getNumItems() > 0) { 283 if (set == -1) { 284 float sum = 0f; 285 for (int i = 0; i < getNumSets(); ++i) { 286 sum += get (i, cat, item); 287 } 288 average = sum / getNumSets(); 289 } 290 if (cat == -1) { 291 float sum = 0f; 292 for (int j = 0; j < getNumCats(); ++j) { 293 sum += get (set, j, item); 294 } 295 average = sum / getNumCats(); 296 } 297 if (item == -1) { 298 float sum = 0f; 299 for (int k = 0; k < getNumItems(); ++k) { 300 sum += get (set, cat, k); 301 } 302 average = sum / getNumItems(); 303 } 304 } 305 return average; 306 } 307 308 309 316 public final void add (int set, int cat, int item, float value) { 317 318 needsUpdate = true; 319 for (int i = setVector.size(); i <= set; ++i) { 320 setVector.add (new Vector (10, 10)); 321 } 322 Vector catVector = (Vector )setVector.get (set); 323 for (int i = catVector.size(); i <= cat; ++i) { 324 catVector.add (new Vector (10, 10)); 325 } 326 Vector itemVector = (Vector )catVector.get (cat); 327 for (int i = itemVector.size(); i <= item; ++i) { 328 itemVector.add (new Float (0f)); 329 } 330 itemVector.set (item, new Float (value)); 331 } 332 333 334 342 public final void addMovingAverage (Dataset dataset, int scope) { 343 344 int numItemsTotal = dataset.getNumSets() > 0 ? dataset.getNumCats() * dataset.getNumItems() : 0; 346 347 if (numItemsTotal >= 3) { 349 if (scope < 3) scope = 3; 351 else { 352 if (scope > numItemsTotal) scope = numItemsTotal; 353 if (scope % 2 == 0) scope--; 354 } 355 356 int internalSetOffset = getNumSets(); int itemOffset = (scope / 2); Vector itemGroup = new Vector (scope); 361 ItemsForSet items = new ItemsForSet (dataset, 0, 0); 363 itemGroup.add (items); 364 for (int i = 0; i < scope - 1; ++i) { 365 items = items.getNextItemsForSet(); 366 itemGroup.add (items); 367 } 368 369 ItemsForSet firstItems = (ItemsForSet)itemGroup.get (0); items = (ItemsForSet)itemGroup.get (itemOffset); 372 float firstAverage = getAverageOfItemsForSetGroup (itemGroup); 373 add (internalSetOffset, items.getCat(), items.getItem(), firstAverage); 374 itemGroup.remove (0); 375 itemGroup.add (((ItemsForSet)itemGroup.lastElement()).getNextItemsForSet()); 377 for (int i = itemOffset + 1; i < numItemsTotal - itemOffset - 1; ++i) { 379 items = (ItemsForSet)itemGroup.get (itemOffset); 380 add (internalSetOffset, items.getCat(), items.getItem(), 381 getAverageOfItemsForSetGroup (itemGroup)); 382 itemGroup.remove (0); 383 itemGroup.add(((ItemsForSet)itemGroup.lastElement()).getNextItemsForSet()); 384 } 385 386 float lastAverage = firstAverage; 388 ItemsForSet lastItems = items.getNextItemsForSet(); if (numItemsTotal > scope) { 390 391 items = (ItemsForSet)itemGroup.get (itemOffset); 393 lastAverage = getAverageOfItemsForSetGroup (itemGroup); 394 add (internalSetOffset, items.getCat(), items.getItem(), lastAverage); 395 lastItems = items.getNextItemsForSet(); 396 } 397 398 for (int i = 0; i < itemOffset; ++i) { 400 add (internalSetOffset, firstItems.getCat(), firstItems.getItem(), firstAverage); 401 firstItems = firstItems.getNextItemsForSet(); 402 } 403 404 for (int i = numItemsTotal - itemOffset; i < numItemsTotal; ++i) { 406 add (internalSetOffset, lastItems.getCat(), lastItems.getItem(), lastAverage); 407 lastItems = lastItems.getNextItemsForSet(); 408 } 409 } 410 else if (numItemsTotal > 0) { int currSet = setVector.size(); 412 for (int j = 0; j < dataset.getNumCats(); ++j) { 413 for (int k = 0; k < dataset.getNumItems(); ++k) { 414 add (currSet, j, k, dataset.getAverage (-1, j, k)); 415 } 416 } 417 needsUpdate = true; 418 } 419 } 420 421 422 432 public final void setMovingAverage (int set, Dataset dataset, int scope) { 433 434 int numItemsTotal = dataset.getNumSets() > 0 ? dataset.getNumCats() * dataset.getNumItems() : 0; 436 437 if (numItemsTotal >= 3) { 439 if (scope < 3) scope = 3; 441 else { 442 if (scope > numItemsTotal) scope = numItemsTotal; 443 if (scope % 2 == 0) scope--; 444 } 445 446 int internalSetOffset = set; int itemOffset = (scope / 2); Vector itemGroup = new Vector (scope); 451 ItemsForSet items = new ItemsForSet (dataset, 0, 0); 453 itemGroup.add (items); 454 for (int i = 0; i < scope - 1; ++i) { 455 items = items.getNextItemsForSet(); 456 itemGroup.add (items); 457 } 458 459 ItemsForSet firstItems = (ItemsForSet)itemGroup.get (0); items = (ItemsForSet)itemGroup.get (itemOffset); 462 float firstAverage = getAverageOfItemsForSetGroup (itemGroup); 463 set (internalSetOffset, items.getCat(), items.getItem(), firstAverage); 464 itemGroup.remove (0); 465 itemGroup.add (((ItemsForSet)itemGroup.lastElement()).getNextItemsForSet()); 467 for (int i = itemOffset + 1; i < numItemsTotal - itemOffset - 1; ++i) { 469 items = (ItemsForSet)itemGroup.get (itemOffset); 470 set (internalSetOffset, items.getCat(), items.getItem(), 471 getAverageOfItemsForSetGroup (itemGroup)); 472 itemGroup.remove (0); 473 itemGroup.add(((ItemsForSet)itemGroup.lastElement()).getNextItemsForSet()); 474 } 475 476 float lastAverage = firstAverage; 478 ItemsForSet lastItems = items.getNextItemsForSet(); if (numItemsTotal > scope) { 480 481 items = (ItemsForSet)itemGroup.get (itemOffset); 483 lastAverage = getAverageOfItemsForSetGroup (itemGroup); 484 set (internalSetOffset, items.getCat(), items.getItem(), lastAverage); 485 lastItems = items.getNextItemsForSet(); 486 } 487 488 for (int i = 0; i < itemOffset; ++i) { 490 set (internalSetOffset, firstItems.getCat(), firstItems.getItem(), firstAverage); 491 firstItems = firstItems.getNextItemsForSet(); 492 } 493 494 for (int i = numItemsTotal - itemOffset; i < numItemsTotal; ++i) { 496 set (internalSetOffset, lastItems.getCat(), lastItems.getItem(), lastAverage); 497 lastItems = lastItems.getNextItemsForSet(); 498 } 499 } 500 else if (numItemsTotal > 0) { int currSet = setVector.size(); 502 for (int j = 0; j < dataset.getNumCats(); ++j) { 503 for (int k = 0; k < dataset.getNumItems(); ++k) { 504 set (currSet, j, k, dataset.getAverage (-1, j, k)); 505 } 506 } 507 needsUpdate = true; 508 } 509 } 510 511 512 545 public final void remove (int set, int cat, int item) { 546 547 needsUpdate = true; 548 549 if (set == -1 && cat == -1 && item == -1) { 551 constructor (0, 0, 0); 552 return; 553 } 554 555 int numSets = getNumSets(); 556 int numCats = getNumCats(); 557 int numItems = getNumItems(); 558 if (set != -1 && set >= numSets) return; 559 if (cat != -1 && cat >= numCats) return; 560 if (item != -1 && item >= numItems) return; 561 562 if (set != -1 && cat == -1 && item == -1) { 564 setVector.remove (set); 565 } 566 else if (set == -1 && cat != -1 && item == -1) { 568 for (int i = 0; i < numSets; ++i) { 569 ((Vector )setVector.get (i)).remove (cat); 570 } 571 } 572 else if (set != 1 && cat != 1 && item == -1) { 574 ((Vector )setVector.get (set)).remove (cat); 575 } 576 else if (set == -1 && cat == -1 && item != 1) { 578 for (int i = 0; i < numSets; ++i) { 579 Vector catVector = (Vector )setVector.get (i); 580 for (int j = 0; j < numCats; ++i) { 581 ((Vector )catVector.get (j)).remove (item); 582 } 583 } 584 } 585 else if (set == -1 && cat != -1 && item != -1) { 587 for (int i = 0; i < numSets; ++i) { 588 ((Vector )((Vector )setVector.get (i)).get (cat)).remove (item); 589 } 590 } 591 else if (set != -1 && cat == -1 && item != -1) { 593 for (int j = 0; j < numCats; ++j) { 594 ((Vector )((Vector )setVector.get (set)).get (j)).remove (item); 595 } 596 } 597 else if (set != -1 && cat != -1 && item != -1) { 599 ((Vector )((Vector )setVector.get (set)).get (cat)).remove (item); 600 } 601 } 602 603 604 605 623 public final void doShiftLower (float[] values) { 624 needsUpdate = true; 625 for (int i = 0; i < setVector.size() && i < values.length; ++i) { 626 Vector catVector = (Vector )setVector.get (i); 627 for (int j = 0; j < catVector.size() - 1; ++j) { 628 Vector itemVector = (Vector )catVector.get (j); 629 itemVector.remove (0); 630 Vector itemVectorNext = (Vector )catVector.get (j + 1); 631 itemVector.add (itemVectorNext.get (0)); 632 } 633 Vector itemVector = (Vector )catVector.get (catVector.size() - 1); 634 itemVector.remove (0); 635 itemVector.add (new Float (values[i])); 636 } 637 } 638 639 640 650 public final void doConvertToStacked() { 651 652 needsUpdate = true; 653 for (int j = 0; j < getNumCats(); ++j) { 654 for (int k = 0; k < getNumItems(); ++k) { 655 float posSum = 0f; 656 float negSum = 0f; 657 for (int i = 0; i < getNumSets(); ++i) { 658 float item = get (i, j, k); 659 if (item >= 0f) { 660 posSum = posSum + get (i, j, k); 661 set (i, j, k, posSum); 662 } 663 else { 664 negSum = negSum + get (i, j, k); 665 set (i, j, k, negSum); 666 } 667 } 668 } 669 } 670 } 671 672 673 677 final float[][] getOldGraphStruct() { 678 679 float[][] dataset; 680 if (setVector.size() == 0) dataset = new float[0][0]; 681 else { 682 dataset = new float 683 [setVector.size()] 684 [((Vector )setVector.get(0)).size() * 685 ((Vector )((Vector )setVector.get(0)).get(0)).size()]; 686 for (int i = 0; i < setVector.size(); ++i) { 687 Vector catVector = (Vector )setVector.get (i); 688 for (int j = 0; j < catVector.size(); ++j) { 689 Vector itemVector = (Vector )catVector.get (j); 690 for (int k = 0; k < itemVector.size(); ++k) { 691 dataset[i][j*itemVector.size() + k] = 692 ((Float )itemVector.get (k)).floatValue(); 693 } 694 } 695 } 696 } 697 return dataset; 698 } 699 700 701 705 final float[] getOldPieStruct() { 706 707 float[] dataset = new float[setVector.size()]; 708 for (int i = 0; i < setVector.size(); ++i) { 709 float sum = 0; 710 Vector catVector = (Vector )setVector.get(i); 711 for (int j = 0; j < catVector.size(); ++j) { 712 Vector itemVector = (Vector )catVector.get(j); 713 for (int k = 0; k < itemVector.size(); ++k) { 714 sum += ((Float )itemVector.get(k)).floatValue(); 715 } 716 } 717 dataset[i] = sum; 718 } 719 return dataset; 720 } 721 722 723 728 final boolean getChart2DNeedsUpdate (Chart2D chart2D) { 729 if (needsUpdate) return true; 730 int index = -1; 731 if ((index = chart2DVector.indexOf (chart2D)) != -1) { 732 return ((Boolean )needsUpdateVector.get (index)).booleanValue(); 733 } 734 return false; 735 } 736 737 738 742 final void addChart2D (Chart2D chart2D) { 743 744 if (!chart2DVector.contains (chart2D)) { 745 chart2DVector.add (chart2D); 746 needsUpdateVector.add (new Boolean (true)); 747 } 748 } 749 750 751 755 final void removeChart2D (Chart2D chart2D) { 756 757 int index = -1; 758 if ((index = chart2DVector.indexOf (chart2D)) != -1) { 759 chart2DVector.remove (index); 760 needsUpdateVector.remove (index); 761 } 762 } 763 764 765 772 final boolean validate (boolean debug) { 773 774 if (debug) System.out.println ("Validating Dataset"); 775 776 boolean valid = true; 777 778 int numSets = setVector.size(); 779 int numCats = 0; 780 Vector catVector = null; 781 if (numSets > 0) { 782 catVector = (Vector )setVector.get (0); 783 numCats = catVector.size(); 784 } 785 int numItems = 0; 786 Vector itemVector = null; 787 if (numCats > 0) { 788 itemVector = (Vector )catVector.get (0); 789 numItems = itemVector.size(); 790 } 791 792 for (int i = 0; i < numSets && valid; ++i) { 793 catVector = (Vector )setVector.get (i); 794 if (numCats != catVector.size()) { 795 valid = false; 796 break; 797 } 798 for (int j = 0; j < catVector.size() && valid; ++j) { 799 itemVector = (Vector )catVector.get (j); 800 if (numItems != itemVector.size()) { 801 valid = false; 802 break; 803 } 804 } 805 } 806 807 if (debug && !valid) System.out.println ("Problem with Dataset"); 808 809 if (debug) { 810 if (valid) System.out.println ("Dataset was valid"); 811 else System.out.println ("Dataset was invalid"); 812 } 813 814 return valid; 815 } 816 817 818 822 final void updateChart2D (Chart2D chart2D) { 823 824 if (getChart2DNeedsUpdate (chart2D)) { 825 826 update(); 827 828 int index = -1; 829 if ((index = chart2DVector.indexOf (chart2D)) != -1) { 830 needsUpdateVector.set (index, new Boolean (false)); 831 } 832 } 833 } 834 835 836 private void constructor (int sets, int cats, int items) { 837 838 needsUpdate = true; 839 setVector = new Vector (sets, 10); 840 for (int i = 0; i < sets; ++i) { 841 Vector catVector = new Vector (cats, 10); 842 setVector.add (i, catVector); 843 for (int j = 0; j < cats; ++j) { 844 Vector itemVector = new Vector (items, 10); 845 catVector.add (j, itemVector); 846 for (int k = 0; k < items; ++k) { 847 itemVector.add (k, new Float (0f)); 848 } 849 } 850 } 851 } 852 853 854 private void update() { 855 856 if (needsUpdate) { 857 858 for (int i = 0; i < needsUpdateVector.size(); ++i) { 859 needsUpdateVector.set (i, new Boolean (true)); 860 } 861 needsUpdate = false; 862 863 greatest = -9999999999999999f; 864 least = 9999999999999999f; 865 for (int i = 0; i < setVector.size(); ++i) { 866 Vector catVector = (Vector )setVector.get (i); 867 for (int j = 0; j < catVector.size(); ++j) { 868 Vector itemVector = (Vector )catVector.get (j); 869 for (int k = 0; k < itemVector.size(); ++k) { 870 float value = ((Float )itemVector.get (k)).floatValue(); 871 greatest = value > greatest ? value : greatest; 872 least = value < least ? value : least; 873 } } } 874 if (greatest < least) greatest = least = 0; 875 } 876 } 877 878 879 private float getAverageOfItemsForSetGroup (Vector group) { 880 881 float sum = 0f; 882 for (int i = 0; i < group.size(); ++i) { 883 sum += ((ItemsForSet)group.get(i)).getSum(); 884 } 885 return sum / group.size(); 886 } 887 888 889 private class ItemsForSet { 890 891 private int cat; 892 private int item; 893 private Dataset dataset; 894 895 ItemsForSet (Dataset dataset, int cat, int item) { 896 897 this.cat = cat; 898 this.item = item; 899 this.dataset = dataset; 900 } 901 902 private int getCat() { 903 return cat; 904 } 905 906 private int getItem() { 907 return item; 908 } 909 910 private float getSum() { 911 float sum = 0; 912 int numSets = dataset.getNumSets(); 913 for (int i = 0; i < numSets; ++i) { 914 sum += dataset.get (i, cat, item); 915 } 916 return sum; 917 } 918 919 private ItemsForSet getNextItemsForSet() { 920 int nextCat = cat; 921 int nextItem = item; 922 if (item >= (dataset.getNumItems() - 1)) { 923 nextCat++; 924 nextItem = 0; 925 } 926 else { 927 nextItem++; 928 } 929 if (nextCat >= dataset.getNumCats()) return null; 930 else return new ItemsForSet (dataset, nextCat, nextItem); 931 } 932 } 933 } | Popular Tags |