1 16 17 package org.pentaho.plugin.jfreechart; 18 19 import java.awt.Color ; 20 import java.awt.Font ; 21 import java.awt.Image ; 22 import java.awt.Paint ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.dom4j.Node; 28 import org.jfree.chart.plot.PlotOrientation; 29 import org.jfree.chart.title.TextTitle; 30 import org.jfree.data.xy.XYSeriesCollection; 31 import org.jfree.data.xy.XYSeries; 32 import org.jfree.ui.RectangleEdge; 33 import org.pentaho.core.connection.IPentahoResultSet; 34 import org.pentaho.core.session.IPentahoSession; 35 36 public class XYSeriesCollectionChartDefinition extends XYSeriesCollection implements ChartDefinition { 37 38 private static final String STACKED_NODE_NAME = "is-stacked"; 40 private static final String ORIENTATION_NODE_NAME = "orientation"; 42 private static final String DOMAIN_TITLE_NODE_NAME = "domain-title"; 44 private static final String DOMAIN_TITLE_FONT_NODE_NAME = "domain-title-font"; 46 private static final String RANGE_TITLE_NODE_NAME = "range-title"; 48 private static final String RANGE_TITLE_FONT_NODE_NAME = "range-title-font"; 50 private static final String DOMAIN_VERTICAL_TICK_LABELS_NODE_NAME = "domain-vertical-tick-labels"; 52 private static final String DOMAIN_INCLUDES_ZERO_NODE_NAME = "domain-includes-zero"; 54 private static final String DOMAIN_STICKY_ZERO_NODE_NAME = "domain-sticky-zero"; 56 private static final String CHART_BORDER_PAINT_NODE_NAME = "border-paint"; 58 private int chartType = JFreeChartEngine.UNDEFINED_CHART_TYPE; 59 60 private String title = ""; 63 private RectangleEdge titlePosition = RectangleEdge.TOP; 64 65 private Font titleFont = TextTitle.DEFAULT_FONT; 66 67 private List subTitles = new ArrayList (); 68 69 private Paint chartBackgroundPaint = Color.WHITE; 70 71 private Image chartBackgroundImage = null; 72 73 private boolean borderVisible = false; 74 75 private Paint borderPaint = Color.BLACK; 76 77 private int width = 200; 78 79 private int height = 200; 80 81 private PlotOrientation orientation = PlotOrientation.VERTICAL; 83 84 private Paint plotBackgroundPaint = Color.WHITE; 85 86 private Image plotBackgroundImage = null; 87 88 private boolean legendIncluded = true; 89 90 private boolean threeD = false; 91 92 private boolean stacked = false; 93 94 private boolean domainVerticalTickLabels = false; 95 96 private boolean domainIncludesZero = true; 97 98 private boolean domainStickyZero = true; 99 100 private Paint [] paintSequence = null; 101 102 private String domainTitle = null; 103 104 private Font domainTitleFont = TextTitle.DEFAULT_FONT; 105 106 private String rangeTitle = null; 107 108 private Font rangeTitleFont = TextTitle.DEFAULT_FONT; 109 110 private IPentahoSession session; 112 113 116 private static final long serialVersionUID = 1717509132920946530L; 117 118 public XYSeriesCollectionChartDefinition(IPentahoSession session) { 119 super(); 120 this.session = session; 121 } 122 123 public XYSeriesCollectionChartDefinition(int chartType, IPentahoResultSet data, boolean byRow, IPentahoSession session) { 124 this(session); 125 this.chartType = chartType; 126 if (byRow) { 127 setDataByRow(data); 128 } else { 129 setDataByColumn(data); 130 } 131 } 132 133 public XYSeriesCollectionChartDefinition(IPentahoResultSet data, boolean byRow, Node chartAttributes, IPentahoSession session) { 134 this(JFreeChartEngine.UNDEFINED_CHART_TYPE, data, byRow, session); 135 setChartAttributes(chartAttributes); 136 } 137 138 private void setChartAttributes(Node chartAttributes) { 139 if (chartAttributes == null) { 140 return; 141 } 142 setChartType(chartAttributes.selectSingleNode(TYPE_NODE_NAME)); 145 146 setChartBackground(chartAttributes.selectSingleNode(CHART_BACKGROUND_NODE_NAME)); 148 149 setPlotBackground(chartAttributes.selectSingleNode(PLOT_BACKGROUND_NODE_NAME)); 151 152 setOrientation(chartAttributes.selectSingleNode(ORIENTATION_NODE_NAME)); 154 155 setLegendIncluded(chartAttributes.selectSingleNode(INCLUDE_LEGEND_NODE_NAME)); 157 158 setTitle(chartAttributes.selectSingleNode(TITLE_NODE_NAME)); 160 161 addSubTitles(chartAttributes.selectNodes(SUBTITLE_NODE_NAME)); 163 164 setPaintSequence(chartAttributes.selectSingleNode(PALETTE_NODE_NAME)); 166 167 setStacked(chartAttributes.selectSingleNode(STACKED_NODE_NAME)); 169 170 setThreeD(chartAttributes.selectSingleNode(THREED_NODE_NAME)); 172 173 setWidth(chartAttributes.selectSingleNode(WIDTH_NODE_NAME)); 175 176 setHeight(chartAttributes.selectSingleNode(HEIGHT_NODE_NAME)); 178 179 setDomainVerticalTickLabels(chartAttributes.selectSingleNode(DOMAIN_VERTICAL_TICK_LABELS_NODE_NAME)); 181 182 setBorderVisible(chartAttributes.selectSingleNode(CHART_BORDER_VISIBLE_NODE_NAME)); 184 185 setBorderPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(CHART_BORDER_PAINT_NODE_NAME))); 187 188 setTitlePosition(chartAttributes.selectSingleNode(TITLE_POSITION_NODE_NAME)); 190 191 setTitleFont(chartAttributes.selectSingleNode(TITLE_FONT_NODE_NAME)); 193 194 setDomainTitle(chartAttributes.selectSingleNode(DOMAIN_TITLE_NODE_NAME)); 196 197 setDomainTitleFont(chartAttributes.selectSingleNode(DOMAIN_TITLE_FONT_NODE_NAME)); 199 200 setRangeTitle(chartAttributes.selectSingleNode(RANGE_TITLE_NODE_NAME)); 202 203 setRangeTitleFont(chartAttributes.selectSingleNode(RANGE_TITLE_FONT_NODE_NAME)); 205 206 setDomainIncludesZero(chartAttributes.selectSingleNode(DOMAIN_INCLUDES_ZERO_NODE_NAME)); 208 209 setDomainStickyZero(chartAttributes.selectSingleNode(DOMAIN_STICKY_ZERO_NODE_NAME)); 211 } 212 213 private void setDataByColumn(IPentahoResultSet data) { 214 if (data == null) { 216 return; } 219 boolean firstPass = true; 220 String lastSeries = ""; String seriesName = ""; Object [] rowData = data.next(); 223 XYSeries wrkSeries = new XYSeries( seriesName ); 224 while (rowData != null) { 225 seriesName = rowData[0].toString(); 226 if ( firstPass || !seriesName.equalsIgnoreCase( lastSeries ) ) { 227 if ( !firstPass ) { 228 addSeries( wrkSeries ); 229 } 230 wrkSeries = new XYSeries( seriesName ); 231 lastSeries = seriesName; 232 firstPass = false; 233 } 234 if (rowData[1] instanceof Number && rowData[2] instanceof Number ) { 235 wrkSeries.add( ((Number ) rowData[1]).doubleValue(), ((Number ) rowData[2]).doubleValue() ); 236 } 237 rowData = data.next(); 238 } 239 if ( !firstPass ) { 240 addSeries( wrkSeries ); 241 } 242 } 243 244 private void setDataByRow(IPentahoResultSet data) { 245 if (data == null) { 247 return; } 250 Object [] rowData = data.next(); 251 while (rowData != null) { 252 String seriesName = (String ) rowData[0]; 253 XYSeries wrkSeries = new XYSeries( seriesName ); 254 for (int column = 1; column < rowData.length - 1; column = column + 2) { 255 wrkSeries.add( ((Number ) rowData[column]).doubleValue(), ((Number ) rowData[column+1]).doubleValue() ); 256 } 257 addSeries( wrkSeries ); 258 rowData = data.next(); 259 } 260 } 261 262 266 public void setChartBackgroundPaint(Paint chartBackgroundPaint) { 267 if (chartBackgroundPaint != null) { 268 this.chartBackgroundPaint = chartBackgroundPaint; 269 } 270 } 271 272 277 public Font getTitleFont() { 278 return titleFont; 279 } 280 281 public void setTitleFont(Font titleFont) { 282 this.titleFont = titleFont; 283 } 284 285 public void setTitleFont(Node titleFontNode) { 286 Font font = JFreeChartEngine.getFont(titleFontNode); 287 if (font != null) { 288 setTitleFont(font); 289 } 290 } 291 292 295 public Paint getChartBackgroundPaint() { 296 return chartBackgroundPaint; 297 } 298 299 302 public int getChartType() { 303 return chartType; 304 } 305 306 public static int getChartType(String typeStr) { 307 if (typeStr != null) { 308 if (LINE_CHART_STR.equalsIgnoreCase(typeStr)) { 309 return JFreeChartEngine.LINE_CHART_TYPE; 310 } else if (AREA_CHART_STR.equalsIgnoreCase(typeStr)) { 311 return JFreeChartEngine.AREA_CHART_TYPE; 312 } else if (STEP_CHART_STR.equalsIgnoreCase(typeStr)) { 313 return JFreeChartEngine.STEP_CHART_TYPE; 314 } else if (STEP_AREA_CHART_STR.equalsIgnoreCase(typeStr)) { 315 return JFreeChartEngine.STEP_AREA_CHART_TYPE; 316 } else if (DIFFERENCE_CHART_STR.equalsIgnoreCase(typeStr)) { 317 return JFreeChartEngine.DIFFERENCE_CHART_TYPE; 318 } else if (DOT_CHART_STR.equalsIgnoreCase(typeStr)) { 319 return JFreeChartEngine.DOT_CHART_TYPE; 320 } 321 } 322 return JFreeChartEngine.UNDEFINED_CHART_TYPE; 323 } 324 325 public void setChartType(Node chartTypeNode) { 326 if (chartTypeNode != null) { 327 String typeStr = chartTypeNode.getText(); 328 setChartType(getChartType(typeStr)); 329 } 330 } 331 332 336 public void setChartType(int chartType) { 337 this.chartType = chartType; 338 } 339 340 343 public boolean isThreeD() { 344 return threeD; 345 } 346 347 public void setThreeD(Node threeDNode) { 348 if (threeDNode != null) { 349 String boolStr = threeDNode.getText(); 350 Boolean booleanValue = new Boolean (boolStr); 351 setThreeD(booleanValue.booleanValue()); 352 } 353 } 354 355 359 public void setThreeD(boolean threeD) { 360 this.threeD = threeD; 361 } 362 363 366 public boolean isStacked() { 367 return stacked; 368 } 369 370 public void setStacked(Node stackedNode) { 371 if (stackedNode != null) { 372 String boolStr = stackedNode.getText(); 373 Boolean booleanValue = new Boolean (boolStr); 374 setStacked(booleanValue.booleanValue()); 375 } 376 } 377 378 382 public void setStacked(boolean stacked) { 383 this.stacked = stacked; 384 } 385 386 389 public boolean isDomainVerticalTickLabels() { 390 return domainVerticalTickLabels; 391 } 392 393 public void setDomainVerticalTickLabels(Node domainVerticalTickLabelsNode) { 394 if (domainVerticalTickLabelsNode != null) { 395 String boolStr = domainVerticalTickLabelsNode.getText(); 396 Boolean booleanValue = new Boolean (boolStr); 397 setDomainVerticalTickLabels(booleanValue.booleanValue()); 398 } 399 } 400 401 405 public void setDomainVerticalTickLabels(boolean domainVerticalTickLabels) { 406 this.domainVerticalTickLabels = domainVerticalTickLabels; 407 } 408 409 412 public boolean isDomainIncludesZero() { 413 return domainIncludesZero; 414 } 415 416 public void setDomainIncludesZero(Node domainIncludesZeroNode) { 417 if (domainIncludesZeroNode != null) { 418 String boolStr = domainIncludesZeroNode.getText(); 419 Boolean booleanValue = new Boolean (boolStr); 420 setDomainIncludesZero(booleanValue.booleanValue()); 421 } 422 } 423 424 428 public void setDomainIncludesZero(boolean domainIncludesZero) { 429 this.domainIncludesZero = domainIncludesZero; 430 } 431 432 435 public boolean isDomainStickyZero() { 436 return domainStickyZero; 437 } 438 439 public void setDomainStickyZero(Node domainStickyZeroNode) { 440 if (domainStickyZeroNode != null) { 441 String boolStr = domainStickyZeroNode.getText(); 442 Boolean booleanValue = new Boolean (boolStr); 443 setDomainStickyZero(booleanValue.booleanValue()); 444 } 445 } 446 447 451 public void setDomainStickyZero(boolean domainStickyZero) { 452 this.domainStickyZero = domainStickyZero; 453 } 454 455 458 public int getHeight() { 459 return height; 460 } 461 462 public void setHeight(Node heightNode) { 463 if (heightNode != null) { 464 setHeight(Integer.parseInt(heightNode.getText())); 465 } 466 } 467 468 472 public void setHeight(int height) { 473 this.height = height; 474 } 475 476 479 public int getWidth() { 480 return width; 481 } 482 483 public void setWidth(Node widthNode) { 484 if (widthNode != null) { 485 setWidth(Integer.parseInt(widthNode.getText())); 486 } 487 } 488 489 493 public void setWidth(int width) { 494 this.width = width; 495 } 496 497 500 public String getTitle() { 501 return title; 502 } 503 504 public void setTitle(Node chartTitleNode) { 505 if (chartTitleNode != null) { 506 setTitle(chartTitleNode.getText()); 507 } 508 } 509 510 514 public void setTitle(String title) { 515 this.title = title; 516 } 517 518 521 public Paint [] getPaintSequence() { 522 return paintSequence; 523 } 524 525 public void setPaintSequence(Node paletteNode) { 526 if (paletteNode != null) { 527 List colorNodes = paletteNode.selectNodes(COLOR_NODE_NAME); 528 Paint [] paints = new Paint [colorNodes.size()]; 529 for (int i = 0; i < colorNodes.size(); i++) { 530 paints[i] = JFreeChartEngine.getPaint((Node) colorNodes.get(i)); 531 } 532 setPaintSequence(paints); 533 } 534 } 535 536 540 public void setPaintSequence(Paint [] paintSequence) { 541 this.paintSequence = paintSequence; 542 } 543 544 547 public List getSubtitles() { 548 return subTitles; 549 } 550 551 public void addSubTitles(List subTitleNodes) { 552 if (subTitleNodes != null) { 553 Iterator iter = subTitleNodes.iterator(); 554 while (iter.hasNext()) { 555 addSubTitle(((Node) iter.next()).getText()); 556 } 557 } 558 } 559 560 public void addSubTitle(String subTitle) { 561 subTitles.add(subTitle); 562 } 563 564 567 public Image getChartBackgroundImage() { 568 return chartBackgroundImage; 569 } 570 571 public void setChartBackgroundImage(Node chartBackgroundImageNode) { 572 setChartBackgroundImage(JFreeChartEngine.getImage(chartBackgroundImageNode, getSession())); 573 } 574 575 579 public void setChartBackgroundImage(Image chartBackgroundImage) { 580 this.chartBackgroundImage = chartBackgroundImage; 581 } 582 583 586 public boolean isLegendIncluded() { 587 return legendIncluded; 588 } 589 590 public void setLegendIncluded(Node legendNode) { 591 if (legendNode != null) { 592 String boolStr = legendNode.getText(); 593 Boolean booleanValue = new Boolean (boolStr); 594 setLegendIncluded(booleanValue.booleanValue()); 595 } 596 } 597 598 602 public void setLegendIncluded(boolean legendIncluded) { 603 this.legendIncluded = legendIncluded; 604 } 605 606 public void setPlotBackgroundPaint(Paint plotBackgroundPaint) { 607 if (plotBackgroundPaint != null) { 608 this.plotBackgroundPaint = plotBackgroundPaint; 609 } 610 } 611 612 public Paint getPlotBackgroundPaint() { 613 return plotBackgroundPaint; 614 } 615 616 619 public Image getPlotBackgroundImage() { 620 return plotBackgroundImage; 621 } 622 623 public void setPlotBackgroundImage(Node plotBackgroundImageNode) { 624 setPlotBackgroundImage(JFreeChartEngine.getImage(plotBackgroundImageNode, getSession())); 625 } 626 627 631 public void setPlotBackgroundImage(Image plotBackgroundImage) { 632 this.plotBackgroundImage = plotBackgroundImage; 633 } 634 635 638 public PlotOrientation getOrientation() { 639 return orientation; 640 } 641 642 public void setOrientation(Node orientationNode) { 643 if (orientationNode != null) { 644 String orientationStr = orientationNode.getText(); 645 if (VERTICAL_ORIENTATION.equalsIgnoreCase(orientationStr)) { 646 setOrientation(PlotOrientation.VERTICAL); 647 } else if (HORIZONTAL_ORIENTATION.equalsIgnoreCase(orientationStr)) { 648 setOrientation(PlotOrientation.HORIZONTAL); 649 } 650 } 651 } 652 653 657 public void setOrientation(PlotOrientation orientation) { 658 this.orientation = orientation; 659 } 660 661 664 public boolean isBorderVisible() { 665 return borderVisible; 666 } 667 668 public void setBorderVisible(Node borderVisibleNode) { 669 if (borderVisibleNode != null) { 670 String boolStr = borderVisibleNode.getText(); 671 Boolean booleanValue = new Boolean (boolStr); 672 setBorderVisible(booleanValue.booleanValue()); 673 } 674 } 675 676 680 public void setBorderVisible(boolean borderVisible) { 681 this.borderVisible = borderVisible; 682 } 683 684 687 public Paint getBorderPaint() { 688 return borderPaint; 689 } 690 691 695 public void setBorderPaint(Paint borderPaint) { 696 this.borderPaint = borderPaint; 697 } 698 699 public void setTitlePosition(Node titlePositionNode) { 700 if (titlePositionNode != null) { 701 String titlePositionStr = titlePositionNode.getText(); 702 if ("top".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.TOP); 704 } else if ("left".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.LEFT); 706 } else if ("bottom".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.BOTTOM); 708 } else if ("right".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.RIGHT); 710 } 711 } 712 } 713 714 717 public RectangleEdge getTitlePosition() { 718 return titlePosition; 719 } 720 721 725 public void setTitlePosition(RectangleEdge titlePosition) { 726 this.titlePosition = titlePosition; 727 } 728 729 public void setChartBackground(Node chartBackgroundNode) { 730 if (chartBackgroundNode != null) { 731 Node backgroundTypeNode = chartBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME); 732 if (backgroundTypeNode != null) { 733 String backgroundTypeStr = backgroundTypeNode.getText(); 734 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 735 setChartBackgroundPaint(JFreeChartEngine.getPaint(chartBackgroundNode)); 736 setChartBackgroundImage((Image ) null); 737 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 738 setChartBackgroundImage(chartBackgroundNode); 739 setChartBackgroundPaint(null); 740 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 741 setChartBackgroundPaint(JFreeChartEngine.getTexturePaint(chartBackgroundNode, getWidth(), getHeight(), getSession())); 742 setChartBackgroundImage((Image ) null); 743 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 744 setChartBackgroundPaint(JFreeChartEngine.getGradientPaint(chartBackgroundNode, getWidth(), getHeight())); 745 setChartBackgroundImage((Image ) null); 746 } 747 } 748 } 749 } 750 751 public void setPlotBackground(Node plotBackgroundNode) { 752 if (plotBackgroundNode != null) { 753 Node backgroundTypeNode = plotBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME); 754 if (backgroundTypeNode != null) { 755 String backgroundTypeStr = backgroundTypeNode.getText(); 756 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 757 setPlotBackgroundPaint(JFreeChartEngine.getPaint(plotBackgroundNode)); 758 setPlotBackgroundImage((Image ) null); 759 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 760 setPlotBackgroundImage(plotBackgroundNode); 761 setPlotBackgroundPaint(null); 762 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 763 setPlotBackgroundPaint(JFreeChartEngine.getTexturePaint(plotBackgroundNode, getWidth(), getHeight(), getSession())); 764 setPlotBackgroundImage((Image ) null); 765 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 766 setPlotBackgroundPaint(JFreeChartEngine.getGradientPaint(plotBackgroundNode, getWidth(), getHeight())); 767 setPlotBackgroundImage((Image ) null); 768 } 769 } 770 } 771 } 772 773 public void setDomainTitle(Node titleNode) { 774 if (titleNode != null) { 775 setDomainTitle(titleNode.getText()); 776 } 777 } 778 779 782 public String getDomainTitle() { 783 return domainTitle; 784 } 785 786 790 public void setDomainTitle(String domainTitle) { 791 this.domainTitle = domainTitle; 792 } 793 794 public void setRangeTitle(Node titleNode) { 795 if (titleNode != null) { 796 setRangeTitle(titleNode.getText()); 797 } 798 } 799 800 803 public String getRangeTitle() { 804 return rangeTitle; 805 } 806 807 811 public void setRangeTitle(String rangeTitle) { 812 this.rangeTitle = rangeTitle; 813 } 814 815 public void setDomainTitleFont(Node titleFontNode) { 816 Font font = JFreeChartEngine.getFont(titleFontNode); 817 if (font != null) { 818 setDomainTitleFont(font); 819 } 820 } 821 822 825 public Font getDomainTitleFont() { 826 return domainTitleFont; 827 } 828 829 833 public void setDomainTitleFont(Font domainTitleFont) { 834 this.domainTitleFont = domainTitleFont; 835 } 836 837 public void setRangeTitleFont(Node titleFontNode) { 838 Font font = JFreeChartEngine.getFont(titleFontNode); 839 if (font != null) { 840 setRangeTitleFont(font); 841 } 842 } 843 844 847 public Font getRangeTitleFont() { 848 return rangeTitleFont; 849 } 850 851 855 public void setRangeTitleFont(Font rangeTitleFont) { 856 this.rangeTitleFont = rangeTitleFont; 857 } 858 859 public boolean isDisplayLabels() { 860 return false; 862 } 863 864 public IPentahoSession getSession() { 865 return session; 866 } 867 868 public void setSession(IPentahoSession session) { 869 this.session = session; 870 } 871 } 872 | Popular Tags |