| 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.axis.CategoryLabelPositions; 29 import org.jfree.chart.plot.PlotOrientation; 30 import org.jfree.chart.title.TextTitle; 31 import org.jfree.data.UnknownKeyException; 32 import org.jfree.data.category.DefaultCategoryDataset; 33 import org.jfree.ui.RectangleEdge; 34 import org.pentaho.core.connection.IPentahoDataTypes; 35 import org.pentaho.core.connection.IPentahoResultSet; 36 import org.pentaho.core.connection.PentahoDataTransmuter; 37 import org.pentaho.core.session.IPentahoSession; 38 39 public class CategoryDatasetChartDefinition extends DefaultCategoryDataset implements ChartDefinition { 40 41 private static final String STACKED_NODE_NAME = "is-stacked"; 43 private static final String ORIENTATION_NODE_NAME = "orientation"; 45 private static final String DOMAIN_TITLE_NODE_NAME = "domain-title"; 47 private static final String DOMAIN_TITLE_FONT_NODE_NAME = "domain-title-font"; 49 private static final String RANGE_TITLE_NODE_NAME = "range-title"; 51 private static final String RANGE_TITLE_FONT_NODE_NAME = "range-title-font"; 53 private static final String DOMAIN_LABEL_ROTATION_ANGLE_NODE_NAME = "domain-label-rotation"; 55 private static final String DOMAIN_LABEL_ROTATION_DIRECTION_NODE_NAME = "domain-label-rotation-dir"; 57 private int chartType = JFreeChartEngine.UNDEFINED_CHART_TYPE; 58 59 private String title = ""; 62 private RectangleEdge titlePosition = RectangleEdge.TOP; 63 64 private Font titleFont = TextTitle.DEFAULT_FONT; 65 66 private List subTitles = new ArrayList (); 67 68 private Paint chartBackgroundPaint = Color.WHITE; 69 70 private Image chartBackgroundImage = null; 71 72 private boolean borderVisible = false; 73 74 private Paint borderPaint = Color.BLACK; 75 76 private int width = 200; 77 78 private int height = 200; 79 80 private PlotOrientation orientation = PlotOrientation.VERTICAL; 82 83 private Paint plotBackgroundPaint = Color.WHITE; 84 85 private Image plotBackgroundImage = null; 86 87 private boolean legendIncluded = true; 88 89 private boolean threeD = false; 90 91 private boolean stacked = false; 92 93 private Paint [] paintSequence = null; 94 95 private CategoryLabelPositions domainLabelPositions = new CategoryLabelPositions(); 96 97 private String domainTitle = null; 98 99 private Font domainTitleFont = TextTitle.DEFAULT_FONT; 100 101 private String rangeTitle = null; 102 103 private Font rangeTitleFont = TextTitle.DEFAULT_FONT; 104 105 private IPentahoSession session; 106 107 110 private static final long serialVersionUID = 1717509132920946530L; 111 112 public CategoryDatasetChartDefinition(IPentahoSession session) { 113 super(); 114 this.session = session; 115 } 116 117 public CategoryDatasetChartDefinition(int chartType, IPentahoResultSet data, boolean byRow, IPentahoSession session) { 118 this(session); 119 this.chartType = chartType; 120 if (byRow) { 121 setDataByRow(data); 122 } else { 123 setDataByColumn(data); 124 } 125 } 126 127 public CategoryDatasetChartDefinition(IPentahoResultSet data, boolean byRow, Node chartAttributes, IPentahoSession session) { 128 this(JFreeChartEngine.UNDEFINED_CHART_TYPE, data, byRow, session); 129 setChartAttributes(chartAttributes); 130 } 131 132 private void setChartAttributes(Node chartAttributes) { 133 if (chartAttributes == null) { 134 return; 135 } 136 setChartType(chartAttributes.selectSingleNode(TYPE_NODE_NAME)); 139 140 setChartBackground(chartAttributes.selectSingleNode(CHART_BACKGROUND_NODE_NAME)); 142 143 setPlotBackground(chartAttributes.selectSingleNode(PLOT_BACKGROUND_NODE_NAME)); 145 146 setOrientation(chartAttributes.selectSingleNode(ORIENTATION_NODE_NAME)); 148 149 setLegendIncluded(chartAttributes.selectSingleNode(INCLUDE_LEGEND_NODE_NAME)); 151 152 setTitle(chartAttributes.selectSingleNode(TITLE_NODE_NAME)); 154 155 addSubTitles(chartAttributes.selectNodes(SUBTITLE_NODE_NAME)); 157 158 setPaintSequence(chartAttributes.selectSingleNode(PALETTE_NODE_NAME)); 160 161 setStacked(chartAttributes.selectSingleNode(STACKED_NODE_NAME)); 163 164 setThreeD(chartAttributes.selectSingleNode(THREED_NODE_NAME)); 166 167 setWidth(chartAttributes.selectSingleNode(WIDTH_NODE_NAME)); 169 170 setHeight(chartAttributes.selectSingleNode(HEIGHT_NODE_NAME)); 172 173 setCategoryLabelRotation(chartAttributes.selectSingleNode(DOMAIN_LABEL_ROTATION_DIRECTION_NODE_NAME), chartAttributes.selectSingleNode(DOMAIN_LABEL_ROTATION_ANGLE_NODE_NAME)); 175 176 setBorderVisible(chartAttributes.selectSingleNode(CHART_BORDER_VISIBLE_NODE_NAME)); 178 179 setBorderPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(CHART_BORDER_PAINT_NODE_NAME))); 181 182 setTitlePosition(chartAttributes.selectSingleNode(TITLE_POSITION_NODE_NAME)); 184 185 setTitleFont(chartAttributes.selectSingleNode(TITLE_FONT_NODE_NAME)); 187 188 setDomainTitle(chartAttributes.selectSingleNode(DOMAIN_TITLE_NODE_NAME)); 190 191 setDomainTitleFont(chartAttributes.selectSingleNode(DOMAIN_TITLE_FONT_NODE_NAME)); 193 194 setRangeTitle(chartAttributes.selectSingleNode(RANGE_TITLE_NODE_NAME)); 196 197 setRangeTitleFont(chartAttributes.selectSingleNode(RANGE_TITLE_FONT_NODE_NAME)); 199 } 200 201 private void setDataByColumn(IPentahoResultSet data) { 202 setDataByRow(PentahoDataTransmuter.pivot(data)); 203 } 204 205 private void setDataByRow(IPentahoResultSet data) { 206 if (data == null) { 207 return; } 210 boolean hasRowHeaders = data.getMetaData().getRowHeaders() != null; 211 boolean hasColumnHeaders = data.getMetaData().getColumnHeaders() != null; 212 if (!hasRowHeaders || !hasColumnHeaders) { 213 data = PentahoDataTransmuter.transmute(data, false); 214 } 215 String [] rowHeaders = null; 216 String [] columnHeaders = null; 217 try { 218 rowHeaders = PentahoDataTransmuter.getCollapsedHeaders(IPentahoDataTypes.AXIS_ROW, data, '|'); 219 columnHeaders = PentahoDataTransmuter.getCollapsedHeaders(IPentahoDataTypes.AXIS_COLUMN, data, '|'); 220 } catch (Exception e) { 221 e.printStackTrace(); 223 } 224 int row = 0; 225 Object [] rowData = data.next(); 226 while (rowData != null) { 227 for (int column = 0; column < rowData.length; column++) { 228 if (rowData[column] instanceof Number ) { 229 Number currentNumber = null; 230 try { currentNumber = getValue(rowHeaders[row], columnHeaders[column]); 232 } catch (UnknownKeyException uke) { currentNumber = new Double (0.0); 235 } 236 if (currentNumber == null) { 237 currentNumber = new Double (0.0); 238 } 239 double currentValue = currentNumber.doubleValue(); 240 double newValue = ((Number ) rowData[column]).doubleValue(); 241 setValue(new Double (currentValue + newValue), rowHeaders[row], columnHeaders[column]); 242 } 243 } 244 row++; 245 rowData = data.next(); 246 } 247 } 248 249 253 public void setChartBackgroundPaint(Paint chartBackgroundPaint) { 254 if (chartBackgroundPaint != null) { 255 this.chartBackgroundPaint = chartBackgroundPaint; 256 } 257 } 258 259 264 public Font getTitleFont() { 265 return titleFont; 266 } 267 268 public void setTitleFont(Font titleFont) { 269 this.titleFont = titleFont; 270 } 271 272 public void setTitleFont(Node titleFontNode) { 273 Font font = JFreeChartEngine.getFont(titleFontNode); 274 if (font != null) { 275 setTitleFont(font); 276 } 277 } 278 279 282 public Paint getChartBackgroundPaint() { 283 return chartBackgroundPaint; 284 } 285 286 289 public int getChartType() { 290 return chartType; 291 } 292 293 public static int getChartType(String typeStr) { 294 if (typeStr != null) { 295 if (PIE_CHART_STR.equalsIgnoreCase(typeStr)) { 296 return JFreeChartEngine.PIE_CHART_TYPE; 297 } else if (PIE_GRID_CHART_STR.equalsIgnoreCase(typeStr)) { 298 return JFreeChartEngine.PIE_GRID_CHART_TYPE; 299 } else if (BAR_CHART_STR.equalsIgnoreCase(typeStr)) { 300 return JFreeChartEngine.BAR_CHART_TYPE; 301 } else if (LINE_CHART_STR.equalsIgnoreCase(typeStr)) { 302 return JFreeChartEngine.LINE_CHART_TYPE; 303 } else if (AREA_CHART_STR.equalsIgnoreCase(typeStr)) { 304 return JFreeChartEngine.AREA_CHART_TYPE; 305 } 306 } 307 return JFreeChartEngine.UNDEFINED_CHART_TYPE; 308 } 309 310 public void setChartType(Node chartTypeNode) { 311 if (chartTypeNode != null) { 312 String typeStr = chartTypeNode.getText(); 313 setChartType(getChartType(typeStr)); 314 } 315 } 316 317 321 public void setChartType(int chartType) { 322 this.chartType = chartType; 323 } 324 325 328 public boolean isThreeD() { 329 return threeD; 330 } 331 332 public void setThreeD(Node threeDNode) { 333 if (threeDNode != null) { 334 String boolStr = threeDNode.getText(); 335 Boolean booleanValue = new Boolean (boolStr); 336 setThreeD(booleanValue.booleanValue()); 337 } 338 } 339 340 344 public void setThreeD(boolean threeD) { 345 this.threeD = threeD; 346 } 347 348 351 public boolean isStacked() { 352 return stacked; 353 } 354 355 public void setStacked(Node stackedNode) { 356 if (stackedNode != null) { 357 String boolStr = stackedNode.getText(); 358 Boolean booleanValue = new Boolean (boolStr); 359 setStacked(booleanValue.booleanValue()); 360 } 361 } 362 363 367 public void setStacked(boolean stacked) { 368 this.stacked = stacked; 369 } 370 371 374 public int getHeight() { 375 return height; 376 } 377 378 public void setHeight(Node heightNode) { 379 if (heightNode != null) { 380 setHeight(Integer.parseInt(heightNode.getText())); 381 } 382 } 383 384 388 public void setHeight(int height) { 389 this.height = height; 390 } 391 392 395 public int getWidth() { 396 return width; 397 } 398 399 public void setWidth(Node widthNode) { 400 if (widthNode != null) { 401 setWidth(Integer.parseInt(widthNode.getText())); 402 } 403 } 404 405 409 public void setWidth(int width) { 410 this.width = width; 411 } 412 413 416 public String getTitle() { 417 return title; 418 } 419 420 public void setTitle(Node chartTitleNode) { 421 if (chartTitleNode != null) { 422 setTitle(chartTitleNode.getText()); 423 } 424 } 425 426 430 public void setTitle(String title) { 431 this.title = title; 432 } 433 434 437 public Paint [] getPaintSequence() { 438 return paintSequence; 439 } 440 441 public void setPaintSequence(Node paletteNode) { 442 if (paletteNode != null) { 443 List colorNodes = paletteNode.selectNodes(COLOR_NODE_NAME); 444 Paint [] paints = new Paint [colorNodes.size()]; 445 for (int i = 0; i < colorNodes.size(); i++) { 446 paints[i] = JFreeChartEngine.getPaint((Node) colorNodes.get(i)); 447 } 448 setPaintSequence(paints); 449 } 450 } 451 452 456 public void setPaintSequence(Paint [] paintSequence) { 457 this.paintSequence = paintSequence; 458 } 459 460 463 public List getSubtitles() { 464 return subTitles; 465 } 466 467 public void addSubTitles(List subTitleNodes) { 468 if (subTitleNodes != null) { 469 Iterator iter = subTitleNodes.iterator(); 470 while (iter.hasNext()) { 471 addSubTitle(((Node) iter.next()).getText()); 472 } 473 } 474 } 475 476 public void addSubTitle(String subTitle) { 477 subTitles.add(subTitle); 478 } 479 480 483 public Image getChartBackgroundImage() { 484 return chartBackgroundImage; 485 } 486 487 public void setChartBackgroundImage(Node chartBackgroundImageNode) { 488 setChartBackgroundImage(JFreeChartEngine.getImage(chartBackgroundImageNode, getSession())); 489 } 490 491 495 public void setChartBackgroundImage(Image chartBackgroundImage) { 496 this.chartBackgroundImage = chartBackgroundImage; 497 } 498 499 502 public boolean isLegendIncluded() { 503 return legendIncluded; 504 } 505 506 public void setLegendIncluded(Node legendNode) { 507 if (legendNode != null) { 508 String boolStr = legendNode.getText(); 509 Boolean booleanValue = new Boolean (boolStr); 510 setLegendIncluded(booleanValue.booleanValue()); 511 } 512 } 513 514 518 public void setLegendIncluded(boolean legendIncluded) { 519 this.legendIncluded = legendIncluded; 520 } 521 522 public void setPlotBackgroundPaint(Paint plotBackgroundPaint) { 523 if (plotBackgroundPaint != null) { 524 this.plotBackgroundPaint = plotBackgroundPaint; 525 } 526 } 527 528 public Paint getPlotBackgroundPaint() { 529 return plotBackgroundPaint; 530 } 531 532 535 public Image getPlotBackgroundImage() { 536 return plotBackgroundImage; 537 } 538 539 public void setPlotBackgroundImage(Node plotBackgroundImageNode) { 540 setPlotBackgroundImage(JFreeChartEngine.getImage(plotBackgroundImageNode, getSession())); 541 } 542 543 547 public void setPlotBackgroundImage(Image plotBackgroundImage) { 548 this.plotBackgroundImage = plotBackgroundImage; 549 } 550 551 554 public PlotOrientation getOrientation() { 555 return orientation; 556 } 557 558 public void setOrientation(Node orientationNode) { 559 if (orientationNode != null) { 560 String orientationStr = orientationNode.getText(); 561 if (VERTICAL_ORIENTATION.equalsIgnoreCase(orientationStr)) { 562 setOrientation(PlotOrientation.VERTICAL); 563 } else if (HORIZONTAL_ORIENTATION.equalsIgnoreCase(orientationStr)) { 564 setOrientation(PlotOrientation.HORIZONTAL); 565 } 566 } 567 } 568 569 573 public void setOrientation(PlotOrientation orientation) { 574 this.orientation = orientation; 575 } 576 577 public CategoryLabelPositions getCategoryLabelPositions() { 578 return domainLabelPositions; 579 } 580 581 public void setCategoryLabelRotation(Node rotationDirection, Node rotationAngle) { 582 String direction = "down"; if (rotationDirection != null) { 585 direction = rotationDirection.getText(); 586 } 587 588 if (rotationAngle != null) { 589 if ("up".equalsIgnoreCase(direction)) { setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Double.parseDouble(rotationAngle.getText()))); 591 } else { 592 setCategoryLabelPositions(CategoryLabelPositions.createDownRotationLabelPositions(Double.parseDouble(rotationAngle.getText()))); 593 } 594 } 595 } 596 597 601 public void setCategoryLabelPositions(CategoryLabelPositions categoryLabelPositions) { 602 this.domainLabelPositions = categoryLabelPositions; 603 } 604 605 608 public boolean isBorderVisible() { 609 return borderVisible; 610 } 611 612 public void setBorderVisible(Node borderVisibleNode) { 613 if (borderVisibleNode != null) { 614 String boolStr = borderVisibleNode.getText(); 615 Boolean booleanValue = new Boolean (boolStr); 616 setBorderVisible(booleanValue.booleanValue()); 617 } 618 } 619 620 624 public void setBorderVisible(boolean borderVisible) { 625 this.borderVisible = borderVisible; 626 } 627 628 631 public Paint getBorderPaint() { 632 return borderPaint; 633 } 634 635 639 public void setBorderPaint(Paint borderPaint) { 640 if (borderPaint != null) { 641 this.borderPaint = borderPaint; 642 } 643 } 644 645 public void setTitlePosition(Node titlePositionNode) { 646 if (titlePositionNode != null) { 647 String titlePositionStr = titlePositionNode.getText(); 648 if ("top".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.TOP); 650 } else if ("left".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.LEFT); 652 } else if ("bottom".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.BOTTOM); 654 } else if ("right".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.RIGHT); 656 } 657 } 658 } 659 660 663 public RectangleEdge getTitlePosition() { 664 return titlePosition; 665 } 666 667 671 public void setTitlePosition(RectangleEdge titlePosition) { 672 this.titlePosition = titlePosition; 673 } 674 675 public void setChartBackground(Node chartBackgroundNode) { 676 if (chartBackgroundNode != null) { 677 Node backgroundTypeNode = chartBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME); 678 if (backgroundTypeNode != null) { 679 String backgroundTypeStr = backgroundTypeNode.getText(); 680 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 681 setChartBackgroundPaint(JFreeChartEngine.getPaint(chartBackgroundNode)); 682 setChartBackgroundImage((Image ) null); 683 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 684 setChartBackgroundImage(chartBackgroundNode); 685 setChartBackgroundPaint(null); 686 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 687 setChartBackgroundPaint(JFreeChartEngine.getTexturePaint(chartBackgroundNode, getWidth(), getHeight(), getSession())); 688 setChartBackgroundImage((Image ) null); 689 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 690 setChartBackgroundPaint(JFreeChartEngine.getGradientPaint(chartBackgroundNode, getWidth(), getHeight())); 691 setChartBackgroundImage((Image ) null); 692 } 693 } 694 } 695 } 696 697 public void setPlotBackground(Node plotBackgroundNode) { 698 if (plotBackgroundNode != null) { 699 Node backgroundTypeNode = plotBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME); 700 if (backgroundTypeNode != null) { 701 String backgroundTypeStr = backgroundTypeNode.getText(); 702 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 703 setPlotBackgroundPaint(JFreeChartEngine.getPaint(plotBackgroundNode)); 704 setPlotBackgroundImage((Image ) null); 705 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 706 setPlotBackgroundImage(plotBackgroundNode); 707 setPlotBackgroundPaint(null); 708 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 709 setPlotBackgroundPaint(JFreeChartEngine.getTexturePaint(plotBackgroundNode, getWidth(), getHeight(), getSession())); 710 setPlotBackgroundImage((Image ) null); 711 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 712 setPlotBackgroundPaint(JFreeChartEngine.getGradientPaint(plotBackgroundNode, getWidth(), getHeight())); 713 setPlotBackgroundImage((Image ) null); 714 } 715 } 716 } 717 } 718 719 public void setDomainTitle(Node titleNode) { 720 if (titleNode != null) { 721 setDomainTitle(titleNode.getText()); 722 } 723 } 724 725 728 public String getDomainTitle() { 729 return domainTitle; 730 } 731 732 736 public void setDomainTitle(String domainTitle) { 737 this.domainTitle = domainTitle; 738 } 739 740 public void setRangeTitle(Node titleNode) { 741 if (titleNode != null) { 742 setRangeTitle(titleNode.getText()); 743 } 744 } 745 746 749 public String getRangeTitle() { 750 return rangeTitle; 751 } 752 753 757 public void setRangeTitle(String rangeTitle) { 758 this.rangeTitle = rangeTitle; 759 } 760 761 public void setDomainTitleFont(Node titleFontNode) { 762 Font font = JFreeChartEngine.getFont(titleFontNode); 763 if (font != null) { 764 setDomainTitleFont(font); 765 } 766 } 767 768 771 public Font getDomainTitleFont() { 772 return domainTitleFont; 773 } 774 775 779 public void setDomainTitleFont(Font domainTitleFont) { 780 this.domainTitleFont = domainTitleFont; 781 } 782 783 public void setRangeTitleFont(Node titleFontNode) { 784 Font font = JFreeChartEngine.getFont(titleFontNode); 785 if (font != null) { 786 setRangeTitleFont(font); 787 } 788 } 789 790 793 public Font getRangeTitleFont() { 794 return rangeTitleFont; 795 } 796 797 801 public void setRangeTitleFont(Font rangeTitleFont) { 802 this.rangeTitleFont = rangeTitleFont; 803 } 804 805 public boolean isDisplayLabels() { 806 return false; 808 } 809 810 public IPentahoSession getSession() { 811 return session; 812 } 813 814 public void setSession(IPentahoSession session) { 815 this.session = session; 816 } 817 } 818 | Popular Tags |