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.title.TextTitle; 29 import org.jfree.data.UnknownKeyException; 30 import org.jfree.data.general.DefaultPieDataset; 31 import org.jfree.ui.RectangleEdge; 32 import org.pentaho.core.connection.IPentahoDataTypes; 33 import org.pentaho.core.connection.IPentahoResultSet; 34 import org.pentaho.core.connection.PentahoDataTransmuter; 35 import org.pentaho.core.session.IPentahoSession; 36 37 public class PieDatasetChartDefinition extends DefaultPieDataset implements ChartDefinition { 38 41 private static final long serialVersionUID = 1L; 42 43 private static final String INTERIOR_GAP_NODE_NAME = "interior-gap"; 45 private static final String START_ANGLE_NODE_NAME = "start-angle"; 47 private static final String LABEL_FONT_NODE_NAME = "label-font"; 49 private static final String LABEL_PAINT_NODE_NAME = "label-paint"; 51 private static final String LABEL_BACKGROUND_PAINT_NODE_NAME = "label-background-paint"; 53 private static final String LABEL_GAP_NODE_NAME = "label-gap"; 55 private String title = ""; 58 private RectangleEdge titlePosition = RectangleEdge.TOP; 59 60 private Font titleFont = TextTitle.DEFAULT_FONT; 61 62 private List subTitles = new ArrayList (); 63 64 private Paint chartBackgroundPaint = Color.WHITE; 65 66 private Image chartBackgroundImage = null; 67 68 private boolean borderVisible = false; 69 70 private Paint borderPaint = Color.BLACK; 71 72 private int width = 200; 73 74 private int height = 200; 75 76 private Paint plotBackgroundPaint = Color.WHITE; 78 79 private Image plotBackgroundImage = null; 80 81 private Paint [] paintSequence = null; 82 83 private boolean legendIncluded = true; 84 85 private boolean threeD = false; 86 87 private boolean displayLabels = true; 88 89 private double interiorGap = 0.25; 91 92 private double startAngle = 90.0; 93 94 private Font labelFont = new Font ("SansSerif", Font.PLAIN, 10); 96 private Paint labelPaint = Color.BLACK; 97 98 private Paint labelBackgroundPaint = new Color (255, 255, 204); 99 100 private double labelGap = 0.10; 101 102 private IPentahoSession session; 104 105 108 public PieDatasetChartDefinition(IPentahoSession session) { 109 super(); 110 this.session = session; 111 } 112 113 116 public PieDatasetChartDefinition(IPentahoResultSet data, boolean byRow, IPentahoSession session) { 117 this(session); 118 if (byRow) { 119 setDataByRow(data); 120 } else { 121 setDataByColumn(data); 122 } 123 } 124 125 public PieDatasetChartDefinition(IPentahoResultSet data, boolean byRow, Node chartAttributes, IPentahoSession session) { 126 this(data, byRow, session); 127 setChartAttributes(chartAttributes); 128 } 129 130 private void setChartAttributes(Node chartAttributes) { 131 if (chartAttributes == null) { 132 return; 133 } 134 setChartBackground(chartAttributes.selectSingleNode(CHART_BACKGROUND_NODE_NAME)); 136 137 setPlotBackground(chartAttributes.selectSingleNode(PLOT_BACKGROUND_NODE_NAME)); 139 140 setLegendIncluded(chartAttributes.selectSingleNode(INCLUDE_LEGEND_NODE_NAME)); 142 143 setTitle(chartAttributes.selectSingleNode(TITLE_NODE_NAME)); 145 146 addSubTitles(chartAttributes.selectNodes(SUBTITLE_NODE_NAME)); 148 149 setPaintSequence(chartAttributes.selectSingleNode(PALETTE_NODE_NAME)); 151 152 setThreeD(chartAttributes.selectSingleNode(THREED_NODE_NAME)); 154 155 setWidth(chartAttributes.selectSingleNode(WIDTH_NODE_NAME)); 157 158 setHeight(chartAttributes.selectSingleNode(HEIGHT_NODE_NAME)); 160 161 setBorderVisible(chartAttributes.selectSingleNode(CHART_BORDER_VISIBLE_NODE_NAME)); 163 164 setBorderPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(CHART_BORDER_PAINT_NODE_NAME))); 166 167 setTitlePosition(chartAttributes.selectSingleNode(TITLE_POSITION_NODE_NAME)); 169 170 setTitleFont(chartAttributes.selectSingleNode(TITLE_FONT_NODE_NAME)); 172 173 setInteriorGap(chartAttributes.selectSingleNode(INTERIOR_GAP_NODE_NAME)); 175 176 setStartAngle(chartAttributes.selectSingleNode(START_ANGLE_NODE_NAME)); 178 179 setDisplayLabels(chartAttributes.selectSingleNode(DISPLAY_LABELS_NODE_NAME)); 181 182 setLabelFont(chartAttributes.selectSingleNode(LABEL_FONT_NODE_NAME)); 184 185 setLabelPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(LABEL_PAINT_NODE_NAME))); 187 188 setLabelBackgroundPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(LABEL_BACKGROUND_PAINT_NODE_NAME))); 190 191 setLabelGap(chartAttributes.selectSingleNode(LABEL_GAP_NODE_NAME)); 193 } 194 195 private void setDataByColumn(IPentahoResultSet data) { 196 setDataByRow(PentahoDataTransmuter.pivot(data)); 197 } 198 199 private void setDataByRow(IPentahoResultSet data) { 200 if (data == null) { 201 return; } 204 boolean hasColumnHeaders = data.getMetaData().getColumnHeaders() != null; 205 if (!hasColumnHeaders) { 206 data = PentahoDataTransmuter.transmute(data, false); 207 } 208 String [] columnHeaders = null; 209 try { 210 columnHeaders = PentahoDataTransmuter.getCollapsedHeaders(IPentahoDataTypes.AXIS_COLUMN, data, '|'); 211 } catch (Exception e) { 212 e.printStackTrace(); 214 } 215 int row = 0; 216 if (!hasColumnHeaders) { 217 data.next(); 218 row = 1; 219 } 220 Object [] rowData = data.next(); 221 while (rowData != null && row < data.getRowCount()+1) { 222 for (int column = 0; column < rowData.length; column++) { 223 if (rowData[column] instanceof Number ) { 224 225 Number currentNumber = null; 226 try { currentNumber = getValue(columnHeaders[column]); 228 } catch (UnknownKeyException uke) { currentNumber = new Double (0.0); 231 } 232 if (currentNumber == null) { 233 currentNumber = new Double (0.0); 234 } 235 double currentValue = currentNumber.doubleValue(); 236 237 double newValue = ((Number ) rowData[column]).doubleValue(); 238 setValue(columnHeaders[column], new Double (newValue + currentValue)); 239 } 240 } 241 rowData = data.next(); 242 row++; 243 } 244 } 245 246 public void setHeight(Node heightNode) { 247 if (heightNode != null) { 248 setHeight(Integer.parseInt(heightNode.getText())); 249 } 250 } 251 252 256 public void setHeight(int height) { 257 this.height = height; 258 } 259 260 public int getHeight() { 261 return height; 262 } 263 264 public void setWidth(Node widthNode) { 265 if (widthNode != null) { 266 setWidth(Integer.parseInt(widthNode.getText())); 267 } 268 } 269 270 274 public void setWidth(int width) { 275 this.width = width; 276 } 277 278 public int getWidth() { 279 return width; 280 } 281 282 public void setTitle(Node chartTitleNode) { 283 if (chartTitleNode != null) { 284 setTitle(chartTitleNode.getText()); 285 } 286 } 287 288 292 public void setTitle(String title) { 293 this.title = title; 294 } 295 296 public String getTitle() { 297 return title; 298 } 299 300 public void setTitleFont(Font titleFont) { 301 this.titleFont = titleFont; 302 } 303 304 public void setTitleFont(Node titleFontNode) { 305 Font font = JFreeChartEngine.getFont(titleFontNode); 306 if (font != null) { 307 setTitleFont(font); 308 } 309 } 310 311 public Font getTitleFont() { 312 return titleFont; 313 } 314 315 public void addSubTitles(List subTitleNodes) { 316 if (subTitleNodes != null) { 317 Iterator iter = subTitleNodes.iterator(); 318 while (iter.hasNext()) { 319 addSubTitle(((Node) iter.next()).getText()); 320 } 321 } 322 } 323 324 public void addSubTitle(String subTitle) { 325 subTitles.add(subTitle); 326 } 327 328 public List getSubtitles() { 329 return subTitles; 330 } 331 332 public void setChartBackground(Node chartBackgroundNode) { 333 if (chartBackgroundNode != null) { 334 Node backgroundTypeNode = chartBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME); 335 if (backgroundTypeNode != null) { 336 String backgroundTypeStr = backgroundTypeNode.getText(); 337 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 338 setChartBackgroundPaint(JFreeChartEngine.getPaint(chartBackgroundNode)); 339 setChartBackgroundImage((Image ) null); 340 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 341 setChartBackgroundImage(chartBackgroundNode); 342 setChartBackgroundPaint(null); 343 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 344 setChartBackgroundPaint(JFreeChartEngine.getTexturePaint(chartBackgroundNode, getWidth(), getHeight(), getSession())); 345 setChartBackgroundImage((Image ) null); 346 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 347 setChartBackgroundPaint(JFreeChartEngine.getGradientPaint(chartBackgroundNode, getWidth(), getHeight())); 348 setChartBackgroundImage((Image ) null); 349 } 350 } 351 } 352 } 353 354 358 public void setChartBackgroundPaint(Paint chartBackgroundPaint) { 359 if (chartBackgroundPaint != null) { 360 this.chartBackgroundPaint = chartBackgroundPaint; 361 } 362 } 363 364 public Paint getChartBackgroundPaint() { 365 return chartBackgroundPaint; 366 } 367 368 public void setChartBackgroundImage(Node chartBackgroundImageNode) { 369 setChartBackgroundImage(JFreeChartEngine.getImage(chartBackgroundImageNode, getSession())); 370 } 371 372 376 public void setChartBackgroundImage(Image chartBackgroundImage) { 377 this.chartBackgroundImage = chartBackgroundImage; 378 } 379 380 public Image getChartBackgroundImage() { 381 return chartBackgroundImage; 382 } 383 384 public void setBorderVisible(Node borderVisibleNode) { 385 if (borderVisibleNode != null) { 386 String boolStr = borderVisibleNode.getText(); 387 Boolean booleanValue = new Boolean (boolStr); 388 setBorderVisible(booleanValue.booleanValue()); 389 } 390 } 391 392 396 public void setBorderVisible(boolean borderVisible) { 397 this.borderVisible = borderVisible; 398 } 399 400 public boolean isBorderVisible() { 401 return borderVisible; 402 } 403 404 public Paint getBorderPaint() { 405 return borderPaint; 406 } 407 408 412 public void setBorderPaint(Paint borderPaint) { 413 if (borderPaint != null) { 414 this.borderPaint = borderPaint; 415 } 416 } 417 418 public void setTitlePosition(Node titlePositionNode) { 419 if (titlePositionNode != null) { 420 String titlePositionStr = titlePositionNode.getText(); 421 if ("top".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.TOP); 423 } else if ("left".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.LEFT); 425 } else if ("bottom".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.BOTTOM); 427 } else if ("right".equalsIgnoreCase(titlePositionStr)) { setTitlePosition(RectangleEdge.RIGHT); 429 } 430 } 431 } 432 433 437 public void setTitlePosition(RectangleEdge titlePosition) { 438 this.titlePosition = titlePosition; 439 } 440 441 public RectangleEdge getTitlePosition() { 442 return titlePosition; 443 } 444 445 public void setPaintSequence(Node paletteNode) { 446 if (paletteNode != null) { 447 List colorNodes = paletteNode.selectNodes(COLOR_NODE_NAME); 448 Paint [] paints = new Paint [colorNodes.size()]; 449 for (int i = 0; i < colorNodes.size(); i++) { 450 paints[i] = JFreeChartEngine.getPaint((Node) colorNodes.get(i)); 451 } 452 setPaintSequence(paints); 453 } 454 } 455 456 460 public void setPaintSequence(Paint [] paintSequence) { 461 this.paintSequence = paintSequence; 462 } 463 464 public Paint [] getPaintSequence() { 465 return paintSequence; 466 } 467 468 public void setPlotBackground(Node plotBackgroundNode) { 469 if (plotBackgroundNode != null) { 470 Node backgroundTypeNode = plotBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME); 471 if (backgroundTypeNode != null) { 472 String backgroundTypeStr = backgroundTypeNode.getText(); 473 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 474 setPlotBackgroundPaint(JFreeChartEngine.getPaint(plotBackgroundNode)); 475 setPlotBackgroundImage((Image ) null); 476 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 477 setPlotBackgroundImage(plotBackgroundNode); 478 setPlotBackgroundPaint(null); 479 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 480 setPlotBackgroundPaint(JFreeChartEngine.getTexturePaint(plotBackgroundNode, getWidth(), getHeight(), getSession())); 481 setPlotBackgroundImage((Image ) null); 482 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) { 483 setPlotBackgroundPaint(JFreeChartEngine.getGradientPaint(plotBackgroundNode, getWidth(), getHeight())); 484 setPlotBackgroundImage((Image ) null); 485 } 486 } 487 } 488 } 489 490 public void setPlotBackgroundPaint(Paint plotBackgroundPaint) { 491 if (plotBackgroundPaint != null) { 492 this.plotBackgroundPaint = plotBackgroundPaint; 493 } 494 } 495 496 public Paint getPlotBackgroundPaint() { 497 return plotBackgroundPaint; 498 } 499 500 504 public void setPlotBackgroundImage(Image plotBackgroundImage) { 505 this.plotBackgroundImage = plotBackgroundImage; 506 } 507 508 public void setPlotBackgroundImage(Node plotBackgroundImageNode) { 509 setPlotBackgroundImage(JFreeChartEngine.getImage(plotBackgroundImageNode, getSession())); 510 } 511 512 public Image getPlotBackgroundImage() { 513 return plotBackgroundImage; 514 } 515 516 public void setLegendIncluded(Node legendNode) { 517 if (legendNode != null) { 518 String boolStr = legendNode.getText(); 519 Boolean booleanValue = new Boolean (boolStr); 520 setLegendIncluded(booleanValue.booleanValue()); 521 } 522 } 523 524 528 public void setLegendIncluded(boolean legendIncluded) { 529 this.legendIncluded = legendIncluded; 530 } 531 532 public boolean isLegendIncluded() { 533 return legendIncluded; 534 } 535 536 public void setThreeD(Node threeDNode) { 537 if (threeDNode != null) { 538 String boolStr = threeDNode.getText(); 539 Boolean booleanValue = new Boolean (boolStr); 540 setThreeD(booleanValue.booleanValue()); 541 } 542 } 543 544 548 public void setThreeD(boolean threeD) { 549 this.threeD = threeD; 550 } 551 552 public boolean isThreeD() { 553 return threeD; 554 } 555 556 private void setInteriorGap(Node interiorGapNode) { 557 if (interiorGapNode != null) { 558 String gapNodeStr = interiorGapNode.getText(); 559 Double doubleValue = new Double (gapNodeStr); 560 setInteriorGap(doubleValue.doubleValue()); 561 } 562 } 563 564 568 public void setInteriorGap(double interiorGap) { 569 this.interiorGap = interiorGap; 570 } 571 572 public double getInteriorGap() { 573 return interiorGap; 574 } 575 576 private void setStartAngle(Node startAngleNode) { 577 if (startAngleNode != null) { 578 String gapNodeStr = startAngleNode.getText(); 579 Double doubleValue = new Double (gapNodeStr); 580 setStartAngle(doubleValue.doubleValue()); 581 } 582 } 583 584 588 public void setStartAngle(double startAngle) { 589 this.startAngle = startAngle; 590 } 591 592 public double getStartAngle() { 593 return startAngle; 594 } 595 596 private void setLabelFont(Node labelFontNode) { 597 Font font = JFreeChartEngine.getFont(labelFontNode); 598 if (font != null) { 599 setLabelFont(font); 600 } 601 } 602 603 public void setLabelFont(Font font) { 604 labelFont = font; 605 } 606 607 public Font getLabelFont() { 608 return labelFont; 610 } 611 612 616 public void setLabelPaint(Paint labelPaint) { 617 if (labelPaint != null) { 618 this.labelPaint = labelPaint; 619 } 620 } 621 622 625 public Paint getLabelPaint() { 626 return labelPaint; 627 } 628 629 public Paint getLabelBackgroundPaint() { 630 return labelBackgroundPaint; 632 } 633 634 638 public void setLabelBackgroundPaint(Paint labelBackgroundPaint) { 639 if (labelBackgroundPaint != null) { 640 this.labelBackgroundPaint = labelBackgroundPaint; 641 } 642 } 643 644 public double getLabelGap() { 645 return labelGap; 646 } 647 648 652 public void setLabelGap(Node labelGapNode) { 653 if (labelGapNode != null) { 654 String gapNodeStr = labelGapNode.getText(); 655 Double doubleValue = new Double (gapNodeStr); 656 setLabelGap(doubleValue.doubleValue()); 657 } 658 } 659 660 public void setLabelGap(double labelGap) { 661 this.labelGap = labelGap; 662 } 663 664 public boolean isDisplayLabels() { 665 return displayLabels; 666 } 667 668 public void setDisplayLabels(Node threeDNode) { 669 if (threeDNode != null) { 670 String boolStr = threeDNode.getText(); 671 Boolean booleanValue = new Boolean (boolStr); 672 setDisplayLabels(booleanValue.booleanValue()); 673 } 674 } 675 676 public void setDisplayLabels(boolean displayLabels) { 677 this.displayLabels = displayLabels; 678 } 679 680 public IPentahoSession getSession() { 681 return session; 682 } 683 684 public void setSession(IPentahoSession session) { 685 this.session = session; 686 } 687 } 688 | Popular Tags |