1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.awt.*; 28 import java.util.*; 29 30 31 36 public abstract class GraphChart2D extends Chart2D { 37 38 39 42 static final int LABELS_BOTTOM = 0; 43 44 47 static final int LABELS_LEFT = 1; 48 49 50 private GraphChart2DProperties graphChart2DProps; 51 private Vector graphPropsVector = new Vector (5, 5); 52 private Vector datasetsVector = new Vector (5, 5); 53 private Vector multiColorsPropsVector = new Vector (5, 5); 54 private Vector warningRegionPropsVector = new Vector (5, 5); 55 private boolean needsUpdate; 56 57 58 65 public GraphChart2D() { 66 needsUpdate = true; 67 } 68 69 70 74 public final void setGraphChart2DProperties (GraphChart2DProperties props) { 75 76 needsUpdate = true; 77 props.addGraphChart2D (this); 78 if (graphChart2DProps != null) graphChart2DProps.removeGraphChart2D (this); 79 graphChart2DProps = props; 80 } 81 82 83 97 public final void setLayoutRatios (float graphW, float graphH, float legendW) { 98 99 needsUpdate = true; 100 GraphChartArea chart = (GraphChartArea)getObjectArea(); 101 102 chart.setGraphToWidthRatio (graphW); 103 chart.setGraphToHeightRatio (graphH); 104 chart.setLegendToWidthRatio (legendW); 105 chart.setLegendToHeightRatio (1f); 106 107 float numbersW, numbersH, labelsW, labelsH; 108 if (getGraphChartType() == LABELS_BOTTOM) { 109 110 numbersW = 1f - graphW - legendW; 111 numbersH = graphH; 112 labelsW = graphW; 113 labelsH = 1f - graphH; 114 115 chart.setYAxisToWidthRatio (numbersW); 116 chart.setYAxisToHeightRatio (numbersH); 117 chart.setXAxisToWidthRatio (labelsW); 118 chart.setXAxisToHeightRatio (labelsH); 119 } 120 else { 121 122 numbersW = graphW; 123 numbersH = 1 - graphH; 124 labelsW = 1 - graphW - legendW; 125 labelsH = graphH; 126 127 chart.setYAxisToWidthRatio (labelsW); 128 chart.setYAxisToHeightRatio (labelsH); 129 chart.setXAxisToWidthRatio (numbersW); 130 chart.setXAxisToHeightRatio (numbersH); 131 } 132 133 chart.setAutoSetLayoutRatios (true); 134 } 135 136 137 141 public final GraphChart2DProperties getGraphChart2DProperties() { 142 return graphChart2DProps; 143 } 144 145 146 151 public final GraphProperties getGraphProperties (int i) { 152 return (GraphProperties)graphPropsVector.get (i); 153 } 154 155 156 161 public final Dataset getDataset (int i) { 162 return (Dataset)datasetsVector.get (i); 163 } 164 165 166 171 public final MultiColorsProperties getMultiColorsProperties (int i) { 172 return (MultiColorsProperties)multiColorsPropsVector.get (i); 173 } 174 175 176 181 public final WarningRegionProperties getWarningRegionProperties (int i) { 182 return (WarningRegionProperties)warningRegionPropsVector.get (i); 183 } 184 185 186 190 public final void addGraphProperties (GraphProperties graphProps) { 191 needsUpdate = true; 192 graphProps.addGraphChart2D (this); 193 graphPropsVector.add (graphProps); 194 } 195 196 197 201 public final void addDataset (Dataset dataset) { 202 needsUpdate = true; 203 dataset.addChart2D (this); 204 datasetsVector.add (dataset); 205 } 206 207 208 212 public final void addMultiColorsProperties (MultiColorsProperties multiColorsProps) { 213 needsUpdate = true; 214 multiColorsProps.addObject2D (this); 215 multiColorsPropsVector.add (multiColorsProps); 216 } 217 218 219 223 public final void addWarningRegionProperties (WarningRegionProperties warningRegionProps) { 224 needsUpdate = true; 225 warningRegionProps.addGraphChart2D (this); 226 warningRegionPropsVector.add (warningRegionProps); 227 } 228 229 230 234 public final void removeGraphProperties (GraphProperties graphProps) { 235 needsUpdate = true; 236 graphProps.removeGraphChart2D (this); 237 graphPropsVector.remove (graphProps); 238 } 239 240 241 245 public final void removeDataset (Dataset dataset) { 246 needsUpdate = true; 247 dataset.removeChart2D (this); 248 datasetsVector.remove (dataset); 249 } 250 251 252 256 public final void removeMultiColorsProperties (MultiColorsProperties multiColorsProps) { 257 needsUpdate = true; 258 multiColorsProps.removeObject2D (this); 259 multiColorsPropsVector.remove (multiColorsProps); 260 } 261 262 263 267 public final void removeWarningRegionProperties (WarningRegionProperties warningRegionProps) { 268 needsUpdate = true; 269 warningRegionProps.removeGraphChart2D (this); 270 warningRegionPropsVector.remove (warningRegionProps); 271 } 272 273 274 278 abstract int getGraphChartType(); 279 280 281 285 final int getNumSetsTotal() { 286 287 int numSets = 0; 288 for (int i = 0; i < datasetsVector.size(); ++i) { 289 numSets += ((Dataset)datasetsVector.get (i)).getNumSets(); 290 } 291 return numSets; 292 } 293 294 295 299 final Vector getGraphPropertiesVector() { 300 return graphPropsVector; 301 } 302 303 304 308 final Vector getDatasetsVector() { 309 return datasetsVector; 310 } 311 312 313 317 final Vector getMultiColorsPropertiesVector() { 318 return multiColorsPropsVector; 319 } 320 321 322 326 final Vector getWarningRegionPropertiesVector() { 327 return warningRegionPropsVector; 328 } 329 330 331 335 final boolean getNeedsUpdateGraphChart2D() { 336 337 if (needsUpdate) return true; 338 if (getNeedsUpdateChart2D()) return true; 339 if (graphChart2DProps.getGraphChart2DNeedsUpdate (this)) return true; 340 341 for (int i = 0; i < graphPropsVector.size(); ++i) { 342 if (((GraphProperties)graphPropsVector.get (i)). 343 getGraphChart2DNeedsUpdate (this)) return true; 344 } 345 for (int i = 0; i < datasetsVector.size(); ++i) { 346 if (((Dataset)datasetsVector.get (i)). 347 getChart2DNeedsUpdate (this)) return true; 348 } 349 for (int i = 0; i < multiColorsPropsVector.size(); ++i) { 350 if (((MultiColorsProperties)multiColorsPropsVector.get (i)). 351 getObject2DNeedsUpdate (this)) return true; 352 } 353 for (int i = 0; i < warningRegionPropsVector.size(); ++i) { 354 if (((WarningRegionProperties)warningRegionPropsVector.get (i)). 355 getGraphChart2DNeedsUpdate (this)) return true; 356 } 357 358 return false; 359 } 360 361 362 369 final boolean validateGraphChart2D (boolean debug) { 370 371 if (debug) System.out.println ("Validating GraphChart2D"); 372 373 boolean valid = true; 374 375 if (!validateChart2D (debug)) valid = false; 376 377 GraphChartArea chart = (GraphChartArea)getObjectArea(); 378 379 if ( 380 chart.getXAxisToWidthRatio() < 0f || chart.getXAxisToWidthRatio() > 1f || 381 chart.getXAxisToHeightRatio() < 0f || chart.getXAxisToHeightRatio() > 1f || 382 chart.getYAxisToWidthRatio() < 0f || chart.getYAxisToWidthRatio() > 1f || 383 chart.getYAxisToHeightRatio() < 0f || chart.getYAxisToHeightRatio() > 1f || 384 chart.getGraphToWidthRatio() < 0f || chart.getGraphToWidthRatio() > 1f || 385 chart.getGraphToHeightRatio() < 0f || chart.getGraphToHeightRatio() > 1f || 386 chart.getLegendToWidthRatio() < 0f || chart.getLegendToWidthRatio() > 1f || 387 chart.getLegendToHeightRatio() < 0f || chart.getLegendToHeightRatio() > 1f) { 388 valid = false; 389 if (debug) System.out.println ("Chart components ratios need to be between 0 and 1"); 390 } 391 392 if (graphChart2DProps == null) { 393 valid = false; 394 if (debug) System.out.println ("GraphChart2DProperties is null"); 395 } 396 else if (!graphChart2DProps.validate (debug)) valid = false; 397 398 if (graphPropsVector.size() != datasetsVector.size() || 400 datasetsVector.size() != multiColorsPropsVector.size()) { 401 valid = false; 402 if (debug) System.out.println ( 403 "Number of GraphProperties, Datasets, and MultiColorsPropertes objects must be equal"); 404 } 405 406 if (valid) { 408 if (graphPropsVector.size() < 1) { 409 valid = false; 410 if (debug) System.out.println ( 411 "Need at least one of each GraphProperties, Dataset, and MultiColorsProperties"); 412 } 413 } 414 415 if (valid) { 416 417 for (int i = 0; i < graphPropsVector.size(); ++i) { 418 419 if (debug) System.out.println ("Checking GraphProperties Object " + i); 420 if (!((GraphProperties)graphPropsVector.get (i)).validate (debug)) valid = false; 421 } 422 for (int i = 0; i < datasetsVector.size(); ++i) { 423 424 if (debug) System.out.println ("Checking Datasets Object " + i); 425 if (!((Dataset)datasetsVector.get (i)).validate (debug)) valid = false; 426 } 427 for (int i = 0; i < multiColorsPropsVector.size(); ++i) { 428 429 if (debug) System.out.println ("Checking MultiColorsProperties Object " + i); 430 if ( 431 !((MultiColorsProperties)multiColorsPropsVector.get (i)).validate (debug)) valid = false; 432 } 433 for (int i = 0; i < warningRegionPropsVector.size(); ++i) { 434 435 if (debug) System.out.println ("Checking WarningRegionProperties Object " + i); 436 if (!((WarningRegionProperties)warningRegionPropsVector.get (i)).validate (debug)) { 437 valid = false; 438 } 439 } 440 } 441 442 if (valid) { 444 445 if (getLegendProperties().getLegendExistence() && 446 getLegendProperties().getLegendLabelsTexts().length != getNumSetsTotal()) { 447 valid = false; 448 if (debug) System.out.println ( 449 "Number of legend labels must equal total number of sets for all datasets"); 450 } 451 } 452 453 if (valid) { 455 456 for (int i = 0; i < datasetsVector.size(); ++i) { 457 458 MultiColorsProperties multiColorsProps = 459 (MultiColorsProperties)multiColorsPropsVector.get (i); 460 if (((Dataset)datasetsVector.get (i)).getNumSets() > 0 && 461 multiColorsProps.getColorsCustomize() && 462 multiColorsProps.getColorsCustom().length < 1) { 463 valid = false; 464 if (debug) System.out.println ("Not enough custom colors for MultiColorsProperties " + i); 465 } 466 } 467 } 468 469 if (valid) { 471 472 int numCats = ((Dataset)datasetsVector.get (0)).getNumCats(); 473 for (int i = 1; i < datasetsVector.size(); ++i) { 474 475 if (numCats != ((Dataset)datasetsVector.get (i)).getNumCats()) { 476 valid = false; 477 if (debug) System.out.println ( 478 "Dataset doesn't have same number of cats as first dataset " + i); 479 } 480 } 481 } 482 483 if (valid) { 485 486 if (graphChart2DProps.getLabelsAxisExistence() && 487 graphChart2DProps.getLabelsAxisLabelsTexts().length != 488 ((Dataset)datasetsVector.get (0)).getNumCats()) { 489 valid = false; 490 if (debug) System.out.println ( 491 "Number of labels axis labels must equal number of cats in each dataset"); 492 } 493 } 494 495 if (valid) { 497 498 if (graphChart2DProps.getGraphComponentsColoringByCat() && 499 ((Dataset)datasetsVector.get (0)).getNumCats() > 0 && 500 graphChart2DProps.getGraphComponentsColorsByCat().getColorsCustomize() && 501 graphChart2DProps.getGraphComponentsColorsByCat().getColorsCustom().length < 1) { 502 valid = false; 503 if (debug) System.out.println ("Not enough custom colors for cat coloring"); 504 } 505 } 506 507 if (debug) { 508 if (valid) System.out.println ("GraphChart2D was valid"); 509 else { 510 System.out.println ( 511 "Possibly unable to check for all invalidities because of early invalidity"); 512 System.out.println ("GraphChart2D was invalid"); 513 } 514 } 515 516 return valid; 517 } 518 519 520 523 final void updateGraphChart2D() { 524 525 if (getNeedsUpdateGraphChart2D()) { 526 527 needsUpdate = false; 528 529 updateChart2D(); 530 531 graphChart2DProps.updateGraphChart2D (this); 532 for (int i = 0; i < datasetsVector.size(); ++i) { 533 ((Dataset)datasetsVector.get (i)).updateChart2D (this); 534 } 535 for (int i = 0; i < multiColorsPropsVector.size(); ++i) { 536 ((MultiColorsProperties)multiColorsPropsVector.get (i)).updateObject2D (this); 537 } 538 for (int i = 0; i < graphPropsVector.size(); ++i) { 539 ((GraphProperties)graphPropsVector.get (i)).updateGraphChart2D (this); 540 } 541 for (int i = 0; i < warningRegionPropsVector.size(); ++i) { 542 ((WarningRegionProperties)warningRegionPropsVector.get (i)).updateGraphChart2D (this); 543 } 544 545 int chartType = getGraphChartType(); 546 GraphChartArea graphChart = (GraphChartArea)getObjectArea(); 547 548 graphChart.setDatasetVector (datasetsVector); 550 551 GraphProperties backgroundGraphProps = 553 (GraphProperties)graphPropsVector.get (graphPropsVector.size() - 1); 554 Vector graphVector = new Vector (graphPropsVector.size(), 0); 555 556 for (int i = graphPropsVector.size() - 2; i >= 0; --i) { 557 GraphArea graph = 558 chartType == LABELS_BOTTOM ? (GraphArea)new LBGraphArea() : (GraphArea)new LLGraphArea(); 559 ((GraphProperties)graphPropsVector.get (i)).configureGraphArea ( 560 backgroundGraphProps, chartType, graph); 561 graphVector.add (graph); 562 } 563 564 GraphArea graph = 565 chartType == LABELS_BOTTOM ? (GraphArea)new LBGraphArea() : (GraphArea)new LLGraphArea(); 566 backgroundGraphProps.configureGraphArea (chartType, graph); 567 graphVector.add (graph); 568 569 graphChart.setGraphVector (graphVector); 570 571 Vector warningRegionsVector = new Vector (warningRegionPropsVector.size(), 0); 573 for (int i = 0; i < warningRegionPropsVector.size(); ++i) { 574 WarningRegion warningRegion = 575 ((WarningRegionProperties)warningRegionPropsVector.get (i)).configureWarningRegion(); 576 warningRegion.setGraphType (chartType); 577 warningRegionsVector.add (warningRegion); 578 579 } 580 graphChart.setWarningRegions (warningRegionsVector); 581 582 int numSets = getNumSetsTotal(); 584 Color[] multiColors = new Color[numSets]; 585 int k = 0; 586 for (int i = 0; i < multiColorsPropsVector.size(); ++i) { 587 MultiColorsProperties multiColorsProps = 588 (MultiColorsProperties)multiColorsPropsVector.get (i); 589 int numSetsThis = ((Dataset)datasetsVector.get (i)).getNumSets(); 590 Color[] producer = multiColorsProps.getColorsArray (numSetsThis); 591 for (int j = 0; j < producer.length; ++j) { 592 multiColors[k] = producer[j]; 593 ++k; 594 } 595 } 596 graphChart.setDatasetColors (multiColors); 597 598 graphChart.setCustomGreatestValue ( 599 graphChart2DProps.getChartDatasetCustomizeGreatestValue(), 600 graphChart2DProps.getChartDatasetCustomGreatestValue()); 601 graphChart.setCustomLeastValue ( 602 graphChart2DProps.getChartDatasetCustomizeLeastValue(), 603 graphChart2DProps.getChartDatasetCustomLeastValue()); 604 graphChart.setGraphableToAvailableRatio ( 605 graphChart2DProps.getChartGraphableToAvailableRatio()); 606 graphChart.setNumPlotAxisLabels (graphChart2DProps.getNumbersAxisNumLabels()); 607 608 graphChart.setGraphComponentsColoringByCat ( 609 graphChart2DProps.getGraphComponentsColoringByCat()); 610 if (graphChart2DProps.getGraphComponentsColoringByCat()) { 611 graphChart.setGraphComponentsColorsByCat ( 612 graphChart2DProps.getGraphComponentsColorsByCat().getColorsArray ( 613 ((Dataset)datasetsVector.get (0)).getNumCats())); 614 } 615 616 AxisArea labelsAxis; 617 AxisArea numbersAxis; 618 TextListArea labelsAxisTextList; 619 TextListArea numbersAxisTextList; 620 if (chartType == LABELS_BOTTOM) { 621 labelsAxis = graphChart.getXAxis(); 622 numbersAxis = graphChart.getYAxis(); 623 labelsAxisTextList = ((XAxisArea)labelsAxis).getTextList(); 624 numbersAxisTextList = ((YAxisArea)numbersAxis).getTextList(); 625 } 626 else { 627 labelsAxis = graphChart.getYAxis(); 628 numbersAxis = graphChart.getXAxis(); 629 labelsAxisTextList = ((YAxisArea)labelsAxis).getTextList(); 630 numbersAxisTextList = ((XAxisArea)numbersAxis).getTextList(); 631 } 632 633 labelsAxis.setTicksAlignment (graphChart2DProps.getLabelsAxisTicksAlignment()); 634 labelsAxisTextList.setLabels (graphChart2DProps.getLabelsAxisLabelsTexts()); 635 636 labelsAxis.setTitleExistence (graphChart2DProps.getLabelsAxisTitleExistence()); 637 labelsAxis.setTitle (graphChart2DProps.getLabelsAxisTitleText()); 638 labelsAxis.setFontPointModel (graphChart2DProps.getLabelsAxisTitleFontPointModel()); 639 labelsAxis.setFontName (graphChart2DProps.getLabelsAxisTitleFontName()); 640 labelsAxis.setFontColor (graphChart2DProps.getLabelsAxisTitleFontColor()); 641 labelsAxis.setFontStyle (graphChart2DProps.getLabelsAxisTitleFontStyle()); 642 labelsAxis.setBetweenTitleAndSpaceGapExistence ( 643 graphChart2DProps.getLabelsAxisTitleBetweenRestGapExistence()); 644 labelsAxis.setBetweenTitleAndSpaceGapThicknessModel ( 645 graphChart2DProps.getLabelsAxisTitleBetweenRestGapThicknessModel()); 646 labelsAxis.setTicksSizeModel (graphChart2DProps.getLabelsAxisTicksExistence() ? 647 graphChart2DProps.getLabelsAxisTicksSizeModel() : new Dimension()); 648 labelsAxis.setTicksColor (graphChart2DProps.getLabelsAxisTicksColor()); 649 labelsAxisTextList.setBulletsOutline (graphChart2DProps.getLabelsAxisTicksOutlineExistence()); 650 labelsAxisTextList.setBulletsOutlineColor (graphChart2DProps.getLabelsAxisTicksOutlineColor()); 651 labelsAxisTextList.setFontPointModel (graphChart2DProps.getLabelsAxisLabelsFontPointModel()); 652 labelsAxisTextList.setFontName (graphChart2DProps.getLabelsAxisLabelsFontName()); 653 labelsAxisTextList.setFontColor (graphChart2DProps.getLabelsAxisLabelsFontColor()); 654 labelsAxisTextList.setFontStyle (graphChart2DProps.getLabelsAxisLabelsFontStyle()); 655 labelsAxisTextList.setBetweenBulletsGapExistence ( 656 graphChart2DProps.getLabelsAxisBetweenLabelsOrTicksGapExistence()); 657 labelsAxisTextList.setBetweenLabelsGapExistence ( 658 graphChart2DProps.getLabelsAxisBetweenLabelsOrTicksGapExistence()); 659 labelsAxisTextList.setBetweenBulletsGapThicknessModel ( 660 graphChart2DProps.getLabelsAxisBetweenLabelsOrTicksGapThicknessModel()); 661 labelsAxisTextList.setBetweenLabelsGapThicknessModel ( 662 graphChart2DProps.getLabelsAxisBetweenLabelsOrTicksGapThicknessModel()); 663 labelsAxisTextList.setBetweenBulletsAndLabelsGapExistence ( 664 graphChart2DProps.getLabelsAxisBetweenLabelsAndTicksGapExistence()); 665 labelsAxisTextList.setBetweenBulletsAndLabelsGapThicknessModel ( 666 graphChart2DProps.getLabelsAxisBetweenLabelsAndTicksGapThicknessModel()); 667 668 if (!graphChart2DProps.getLabelsAxisExistence()) { 669 int numCats = ((Dataset)datasetsVector.get (0)).getNumCats(); 670 labelsAxis.setTitleExistence (false); 671 labelsAxisTextList.setBetweenBulletsAndLabelsGapExistence (false); 672 labelsAxisTextList.setBetweenBulletsGapExistence (false); 673 labelsAxisTextList.setBetweenLabelsGapExistence (false); 674 labelsAxisTextList.setBulletsSizeModel (new Dimension()); 675 labelsAxisTextList.setFontPointModel (0); 676 String [] labels = new String [numCats]; 677 for (int i = 0; i < numCats; ++i) labels[i] = ""; 678 labelsAxisTextList.setLabels (labels); 679 } 680 681 numbersAxis.setTitleExistence (graphChart2DProps.getNumbersAxisTitleExistence()); 682 numbersAxis.setTitle (graphChart2DProps.getNumbersAxisTitleText()); 683 numbersAxis.setFontPointModel (graphChart2DProps.getNumbersAxisTitleFontPointModel()); 684 numbersAxis.setFontName (graphChart2DProps.getNumbersAxisTitleFontName()); 685 numbersAxis.setFontColor (graphChart2DProps.getNumbersAxisTitleFontColor()); 686 numbersAxis.setFontStyle (graphChart2DProps.getNumbersAxisTitleFontStyle()); 687 numbersAxis.setBetweenTitleAndSpaceGapExistence ( 688 graphChart2DProps.getNumbersAxisTitleBetweenRestGapExistence()); 689 numbersAxis.setBetweenTitleAndSpaceGapThicknessModel ( 690 graphChart2DProps.getNumbersAxisTitleBetweenRestGapThicknessModel()); 691 numbersAxis.setTicksSizeModel (graphChart2DProps.getNumbersAxisTicksExistence() ? 692 graphChart2DProps.getNumbersAxisTicksSizeModel() : new Dimension()); 693 numbersAxis.setTicksColor (graphChart2DProps.getNumbersAxisTicksColor()); 694 numbersAxisTextList.setBulletsOutline (graphChart2DProps.getNumbersAxisTicksOutlineExistence()); 695 numbersAxisTextList.setBulletsOutlineColor (graphChart2DProps.getNumbersAxisTicksOutlineColor()); 696 numbersAxisTextList.setFontPointModel (graphChart2DProps.getNumbersAxisLabelsFontPointModel()); 697 numbersAxisTextList.setFontName (graphChart2DProps.getNumbersAxisLabelsFontName()); 698 numbersAxisTextList.setFontColor (graphChart2DProps.getNumbersAxisLabelsFontColor()); 699 numbersAxisTextList.setFontStyle (graphChart2DProps.getNumbersAxisLabelsFontStyle()); 700 numbersAxisTextList.setBetweenBulletsGapExistence ( 701 graphChart2DProps.getNumbersAxisBetweenLabelsOrTicksGapExistence()); 702 numbersAxisTextList.setBetweenLabelsGapExistence ( 703 graphChart2DProps.getNumbersAxisBetweenLabelsOrTicksGapExistence()); 704 numbersAxisTextList.setBetweenBulletsGapThicknessModel ( 705 graphChart2DProps.getNumbersAxisBetweenLabelsOrTicksGapThicknessModel()); 706 numbersAxisTextList.setBetweenLabelsGapThicknessModel ( 707 graphChart2DProps.getNumbersAxisBetweenLabelsOrTicksGapThicknessModel()); 708 numbersAxisTextList.setBetweenBulletsAndLabelsGapExistence ( 709 graphChart2DProps.getNumbersAxisBetweenLabelsAndTicksGapExistence()); 710 numbersAxisTextList.setBetweenBulletsAndLabelsGapThicknessModel ( 711 graphChart2DProps.getNumbersAxisBetweenLabelsAndTicksGapThicknessModel()); 712 } 713 } 714 } | Popular Tags |