1 11 package com.tonbeller.jpivot.chart; 12 13 import java.awt.Color ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.io.StringWriter ; 17 import java.io.Writer ; 18 import java.text.DecimalFormat ; 19 import java.text.DecimalFormatSymbols ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Locale ; 24 25 import javax.servlet.http.HttpSession ; 26 import javax.xml.parsers.DocumentBuilder ; 27 28 import org.apache.log4j.Logger; 29 import org.jfree.chart.ChartRenderingInfo; 30 import org.jfree.chart.JFreeChart; 31 import org.jfree.chart.axis.CategoryLabelPosition; 32 import org.jfree.chart.axis.CategoryLabelPositions; 33 import org.jfree.chart.axis.CategoryLabelWidthType; 34 import org.jfree.chart.entity.ChartEntity; 35 import org.jfree.chart.entity.EntityCollection; 36 import org.jfree.chart.entity.StandardEntityCollection; 37 import org.jfree.chart.plot.CategoryPlot; 38 import org.jfree.chart.plot.PiePlot; 39 import org.jfree.chart.plot.PiePlot3D; 40 import org.jfree.chart.plot.Plot; 41 import org.jfree.chart.plot.PlotOrientation; 42 import org.jfree.chart.servlet.ServletUtilities; 43 import org.jfree.chart.title.LegendTitle; 44 import org.jfree.chart.title.TextTitle; 45 import org.jfree.chart.title.Title; 46 import org.jfree.chart.urls.CategoryURLGenerator; 47 import org.jfree.chart.urls.StandardCategoryURLGenerator; 48 import org.jfree.chart.urls.StandardPieURLGenerator; 49 import org.jfree.data.category.CategoryDataset; 50 import org.jfree.data.category.DefaultCategoryDataset; 51 import org.jfree.data.general.PieDataset; 52 import org.jfree.text.TextBlockAnchor; 53 import org.jfree.ui.HorizontalAlignment; 54 import org.jfree.ui.RectangleAnchor; 55 import org.jfree.ui.RectangleEdge; 56 import org.jfree.ui.RectangleInsets; 57 import org.jfree.ui.TextAnchor; 58 import org.jfree.ui.VerticalAlignment; 59 import org.jfree.util.TableOrder; 60 import org.w3c.dom.Document ; 61 import org.w3c.dom.Element ; 62 63 import com.tonbeller.jpivot.core.ModelChangeEvent; 64 import com.tonbeller.jpivot.core.ModelChangeListener; 65 import com.tonbeller.jpivot.olap.model.Cell; 66 import com.tonbeller.jpivot.olap.model.Member; 67 import com.tonbeller.jpivot.olap.model.OlapModel; 68 import com.tonbeller.jpivot.olap.model.Position; 69 import com.tonbeller.jpivot.olap.model.Result; 70 import com.tonbeller.jpivot.olap.navi.DrillThrough; 71 import com.tonbeller.jpivot.olap.navi.MemberTree; 72 import com.tonbeller.wcf.component.Component; 73 import com.tonbeller.wcf.component.ComponentSupport; 74 import com.tonbeller.wcf.controller.Dispatcher; 75 import com.tonbeller.wcf.controller.DispatcherSupport; 76 import com.tonbeller.wcf.controller.RequestContext; 77 import com.tonbeller.wcf.controller.RequestListener; 78 import com.tonbeller.wcf.table.ITableComponent; 79 import com.tonbeller.wcf.table.TableModel; 80 import com.tonbeller.wcf.utils.DomUtils; 81 import com.tonbeller.wcf.utils.XmlUtils; 82 83 88 public class ChartComponent extends ComponentSupport implements ModelChangeListener { 89 private static Logger logger = Logger.getLogger(ChartComponent.class); 90 91 String ref; 92 Document document; 93 OlapModel olapModel; 94 boolean dirty = true; 95 Locale locale; 96 97 Result result; 98 Iterator cellIterator; 99 int dimCount; 100 Element rootElement; 101 int colCount; 102 103 112 String CHART_SERVLET = "/DisplayChart"; 113 final String CHART_SERVLET_KEY = "chartServlet"; 114 boolean baseDisplayURLSet = false; 115 String webControllerURL = ""; 116 117 String filename = null; 118 final int DEFAULT_CHART_WIDTH = 500; 120 final int DEFAULT_CHART_HEIGHT = 300; 121 String chartTitle = ""; 122 java.awt.Font titleFont = JFreeChart.DEFAULT_TITLE_FONT; 123 String fontName = "SansSerif"; 124 int fontStyle = java.awt.Font.BOLD; 125 int fontSize = 18; 126 String slicerFontName = "SansSerif"; 127 int slicerFontStyle = java.awt.Font.PLAIN; 128 int slicerFontSize = 12; 129 String axisFontName = "SansSerif"; 130 int axisFontStyle = java.awt.Font.PLAIN; 131 int axisFontSize = 12; 132 String axisTickFontName = "SansSerif"; 133 int axisTickFontStyle = java.awt.Font.PLAIN; 134 int axisTickFontSize = 12; 135 String legendFontName = "SansSerif"; 136 int legendFontStyle = java.awt.Font.PLAIN; 137 int legendFontSize = 10; 138 int legendPosition = 3; int slicerPosition = 1; int slicerAlignment = 3; int bgColorR = 255; 143 int bgColorG = 255; 144 int bgColorB = 255; 145 int chartType = 1; 146 int chartHeight = DEFAULT_CHART_HEIGHT; 147 int chartWidth = DEFAULT_CHART_WIDTH; 148 String horizAxisLabel = ""; 149 String vertAxisLabel = ""; 150 boolean showLegend = true; 151 boolean showSlicer = true; 152 boolean showTooltips = true; 153 boolean drillThroughEnabled = false; 154 int tickLabelRotate = 30; 156 ChartRenderingInfo info = null; 157 158 Dispatcher dispatcher = new DispatcherSupport(); 159 160 169 public ChartComponent(String id, Component parent, String ref, String baseDisplayURL, String controllerURL, RequestContext context) { 170 super(id, parent); 171 this.ref = ref; 172 173 this.olapModel = (OlapModel) context.getModelReference(ref); 174 this.olapModel.addModelChangeListener(this); 175 this.locale = context.getLocale(); 176 getDispatcher().addRequestListener(null, null, dispatcher); 178 String chartServlet = baseDisplayURL; 180 if ( chartServlet == null ) { 181 chartServlet = context.getServletContext().getInitParameter(CHART_SERVLET_KEY); 182 } else { 183 baseDisplayURLSet = true; 184 } 185 if ( chartServlet != null ) { 186 this.CHART_SERVLET = chartServlet; 187 } 188 if (controllerURL != null) { 189 this.webControllerURL = controllerURL; 190 } 191 } 192 193 public ChartComponent(String id, Component parent, String ref, RequestContext context) { 194 this(id, parent, ref, null, null, context); 195 } 196 197 200 public void initialize(RequestContext context) throws Exception { 201 super.initialize(context); 202 } 203 204 211 public Document render(RequestContext context) throws Exception { 212 if (dirty) { 214 dispatcher.clear(); 216 this.result = olapModel.getResult(); 217 this.cellIterator = result.getCells().iterator(); 218 this.dimCount = result.getAxes().length; 219 DefaultCategoryDataset dataset = null; 220 switch (dimCount) { 221 case 1 : 222 logger.info("1-dim data"); 223 dataset = build1dimDataset(); 224 break; 225 case 2 : 226 logger.info("2-dim data"); 227 dataset = build2dimDataset(); 228 break; 229 default : 230 logger.error("less than 1 or more than 2 dimensions"); 231 throw new IllegalArgumentException ("ChartRenderer requires a 1 or 2 dimensional result"); 232 } 233 dirty = false; 235 236 titleFont = new java.awt.Font (fontName, fontStyle, fontSize); 238 239 CategoryURLGenerator urlGenerator = new jpivotCategoryURLGenerator(webControllerURL); 240 241 JFreeChart chart=null; 243 switch (chartType) { 244 case 1 : 245 chart = ChartFactory.createBarChart( 246 chartTitle, 247 titleFont, 248 horizAxisLabel, 249 vertAxisLabel, 250 dataset, 251 PlotOrientation.VERTICAL, 252 showLegend, 253 showTooltips, 254 drillThroughEnabled, 255 urlGenerator 256 ); 257 break; 258 case 2 : 259 chart = ChartFactory.createBarChart3D( 260 chartTitle, 261 titleFont, 262 horizAxisLabel, 263 vertAxisLabel, 264 dataset, 265 PlotOrientation.VERTICAL, 266 showLegend, 267 showTooltips, 268 drillThroughEnabled, 269 urlGenerator 270 ); 271 break; 272 case 3 : 273 chart = ChartFactory.createBarChart( 274 chartTitle, 275 titleFont, 276 horizAxisLabel, 277 vertAxisLabel, 278 dataset, 279 PlotOrientation.HORIZONTAL, 280 showLegend, 281 showTooltips, 282 drillThroughEnabled, 283 urlGenerator 284 ); 285 break; 286 case 4 : 287 chart = ChartFactory.createBarChart3D( 288 chartTitle, 289 titleFont, 290 horizAxisLabel, 291 vertAxisLabel, 292 dataset, 293 PlotOrientation.HORIZONTAL, 294 showLegend, 295 showTooltips, 296 drillThroughEnabled, 297 urlGenerator 298 ); 299 break; 300 case 5 : 301 chart = ChartFactory.createStackedBarChart( 302 chartTitle, 303 titleFont, 304 horizAxisLabel, 305 vertAxisLabel, 306 dataset, 307 PlotOrientation.VERTICAL, 308 showLegend, 309 showTooltips, 310 drillThroughEnabled, 311 urlGenerator 312 ); 313 break; 314 case 6 : 315 chart = ChartFactory.createStackedBarChart3D( 316 chartTitle, 317 titleFont, 318 horizAxisLabel, 319 vertAxisLabel, 320 dataset, 321 PlotOrientation.VERTICAL, 322 showLegend, 323 showTooltips, 324 drillThroughEnabled, 325 urlGenerator 326 ); 327 break; 328 case 7 : 329 chart = ChartFactory.createStackedBarChart( 330 chartTitle, 331 titleFont, 332 horizAxisLabel, 333 vertAxisLabel, 334 dataset, 335 PlotOrientation.HORIZONTAL, 336 showLegend, 337 showTooltips, 338 drillThroughEnabled, 339 urlGenerator 340 ); 341 break; 342 case 8 : 343 chart = ChartFactory.createStackedBarChart3D( 344 chartTitle, 345 titleFont, 346 horizAxisLabel, 347 vertAxisLabel, 348 dataset, 349 PlotOrientation.HORIZONTAL, 350 showLegend, 351 showTooltips, 352 drillThroughEnabled, 353 urlGenerator 354 ); 355 break; 356 case 9 : 357 chart = ChartFactory.createLineChart( 358 chartTitle, 359 titleFont, 360 horizAxisLabel, 361 vertAxisLabel, 362 dataset, 363 PlotOrientation.VERTICAL, 364 showLegend, 365 showTooltips, 366 drillThroughEnabled, 367 urlGenerator 368 ); 369 break; 370 case 10 : 371 chart = ChartFactory.createLineChart( 372 chartTitle, 373 titleFont, 374 horizAxisLabel, 375 vertAxisLabel, 376 dataset, 377 PlotOrientation.HORIZONTAL, 378 showLegend, 379 showTooltips, 380 drillThroughEnabled, 381 urlGenerator 382 ); 383 break; 384 case 11 : 385 chart = ChartFactory.createAreaChart( 386 chartTitle, 387 titleFont, 388 horizAxisLabel, 389 vertAxisLabel, 390 dataset, 391 PlotOrientation.VERTICAL, 392 showLegend, 393 showTooltips, 394 drillThroughEnabled, 395 urlGenerator 396 ); 397 break; 398 case 12 : 399 chart = ChartFactory.createAreaChart( 400 chartTitle, 401 titleFont, 402 horizAxisLabel, 403 vertAxisLabel, 404 dataset, 405 PlotOrientation.HORIZONTAL, 406 showLegend, 407 showTooltips, 408 drillThroughEnabled, 409 urlGenerator 410 ); 411 break; 412 413 case 13 : 414 chart = ChartFactory.createStackedAreaChart( 415 chartTitle, 416 titleFont, 417 horizAxisLabel, 418 vertAxisLabel, 419 dataset, 420 PlotOrientation.VERTICAL, 421 showLegend, 422 showTooltips, 423 drillThroughEnabled, 424 urlGenerator 425 ); 426 break; 427 case 14 : 428 chart = ChartFactory.createStackedAreaChart( 429 chartTitle, 430 titleFont, 431 horizAxisLabel, 432 vertAxisLabel, 433 dataset, 434 PlotOrientation.HORIZONTAL, 435 showLegend, 436 showTooltips, 437 drillThroughEnabled, 438 urlGenerator 439 ); 440 break; 441 442 case 15 : 443 chart = ChartFactory.createPieChart( 444 chartTitle, 445 titleFont, 446 dataset, 447 TableOrder.BY_COLUMN, 448 showLegend, 449 showTooltips, 450 drillThroughEnabled, 451 new jpivotPieURLGenerator(TableOrder.BY_COLUMN,dataset, webControllerURL) 452 ); 453 break; 454 case 16 : 455 chart = ChartFactory.createPieChart( 456 chartTitle, 457 titleFont, 458 dataset, 459 TableOrder.BY_ROW, 460 showLegend, 461 showTooltips, 462 drillThroughEnabled, 463 new jpivotPieURLGenerator(TableOrder.BY_ROW,dataset, webControllerURL) 464 ); 465 break; 466 case 17 : 467 chart = ChartFactory.create3DPieChart( 468 chartTitle, 469 titleFont, 470 dataset, 471 TableOrder.BY_COLUMN, 472 showLegend, 473 showTooltips, 474 drillThroughEnabled, 475 new jpivotPieURLGenerator(TableOrder.BY_COLUMN,dataset, webControllerURL) 476 ); 477 478 break; 479 case 18 : 480 chart = ChartFactory.create3DPieChart( 481 chartTitle, 482 titleFont, 483 dataset, 484 TableOrder.BY_ROW, 485 showLegend, 486 showTooltips, 487 drillThroughEnabled, 488 new jpivotPieURLGenerator(TableOrder.BY_ROW,dataset, webControllerURL) 489 ); 490 491 break; 492 493 default : 494 throw new Exception ("An unknown Chart Type was requested"); 495 } 496 try { 497 chart.setBackgroundPaint(new java.awt.Color (bgColorR, bgColorG, bgColorB)); 498 499 java.awt.Font slicerFont = new java.awt.Font (slicerFontName, slicerFontStyle, slicerFontSize); 500 java.awt.Font axisFont = new java.awt.Font (axisFontName, axisFontStyle, axisFontSize); 501 java.awt.Font axisTickFont = new java.awt.Font (axisTickFontName, axisTickFontStyle, axisTickFontSize); 502 java.awt.Font legendFont = new java.awt.Font (legendFontName, legendFontStyle, legendFontSize); 503 Plot plot = chart.getPlot(); 504 505 if ( plot instanceof CategoryPlot ) { 506 CategoryPlot catPlot = (CategoryPlot) plot; 507 catPlot.getDomainAxis().setLabelFont(axisFont); 508 catPlot.getRangeAxis().setLabelFont(axisFont); 509 catPlot.getDomainAxis().setTickLabelFont(axisTickFont); 510 catPlot.getRangeAxis().setTickLabelFont(axisTickFont); 511 catPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(100.0f); 512 double angle = -2.0 * Math.PI / 360.0 * (double) tickLabelRotate; 513 CategoryLabelPositions oldp = catPlot.getDomainAxis().getCategoryLabelPositions(); 514 CategoryLabelPositions newp = new CategoryLabelPositions( 515 oldp.getLabelPosition(RectangleEdge.TOP), 516 new CategoryLabelPosition( 517 RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, 518 TextAnchor.TOP_RIGHT, angle, 519 CategoryLabelWidthType.RANGE, 0.0f), 520 oldp.getLabelPosition(RectangleEdge.LEFT), 521 oldp.getLabelPosition(RectangleEdge.RIGHT) 522 ); 523 catPlot.getDomainAxis().setCategoryLabelPositions(newp); 524 } 525 else if ( plot instanceof PiePlot3D ) { 526 PiePlot3D piePlot = (PiePlot3D) plot; 527 piePlot.setLabelFont(axisFont); 529 piePlot.setDirection(org.jfree.util.Rotation.CLOCKWISE); 532 piePlot.setForegroundAlpha(0.5f); 533 piePlot.setNoDataMessage("No data to display"); 534 } 535 else if ( plot instanceof PiePlot ) { 536 PiePlot piePlot = (PiePlot) plot; 537 piePlot.setLabelFont(axisFont); 540 542 } 543 LegendTitle legend = (LegendTitle) chart.getLegend(); 544 if ( legend != null ) { 545 legend.setItemFont(legendFont); 546 565 RectangleEdge legendRectEdge=RectangleEdge.BOTTOM; 566 switch (legendPosition){ 567 case 0: 568 legendRectEdge = RectangleEdge.LEFT; 569 break; 570 case 1: 571 legendRectEdge = RectangleEdge.TOP; 572 break; 573 case 2: 574 legendRectEdge = RectangleEdge.RIGHT; 575 break; 576 case 3: 577 legendRectEdge = RectangleEdge.BOTTOM; 578 break; 579 } 580 legend.setPosition(legendRectEdge); 581 } 582 if ( showSlicer ) { 583 RectangleEdge slicerRectPos=RectangleEdge.BOTTOM; 584 HorizontalAlignment slicerHorizAlignment=HorizontalAlignment.LEFT; 585 586 switch (slicerPosition){ 587 case 0: 588 slicerRectPos = RectangleEdge.TOP; 589 break; 590 case 1: 591 slicerRectPos = RectangleEdge.BOTTOM; 592 break; 593 case 2: 594 slicerRectPos = RectangleEdge.RIGHT; 595 break; 596 case 3: 597 slicerRectPos = RectangleEdge.LEFT; 598 break; 599 } 600 601 switch (slicerAlignment){ 602 case 4: 603 slicerHorizAlignment = HorizontalAlignment.CENTER; 604 break; 605 case 3: 606 slicerHorizAlignment = HorizontalAlignment.LEFT; 607 break; 608 case 2: 609 slicerHorizAlignment = HorizontalAlignment.RIGHT; 610 break; 611 } 612 TextTitle slicer = new TextTitle(buildSlicer(), 613 slicerFont, 614 Color.BLACK, 615 slicerRectPos, 616 slicerHorizAlignment, 617 VerticalAlignment.CENTER, 618 new RectangleInsets(0,0,0,0)); 619 620 621 slicer.setPosition(slicerRectPos); 622 chart.addSubtitle(slicer); 623 } 624 info = new ChartRenderingInfo(new StandardEntityCollection()); 625 HttpSession session = context.getSession(); 627 filename = ServletUtilities.saveChartAsPNG( 628 chart, 629 chartWidth, 630 chartHeight, 631 info, 632 session); 633 634 } 635 catch (Exception e) { 636 filename = "public_error_500x300.png"; 637 dirty = true; 638 } 639 } 640 DocumentBuilder parser = XmlUtils.getParser(); 642 String xchart = "<xchart>" + writeImageMap(filename, info, false) + "</xchart>"; 644 650 InputStream stream = 652 new ByteArrayInputStream (xchart.getBytes("UTF-8")); 653 654 document = parser.parse(stream); 655 Element root = document.getDocumentElement(); 656 String graphURL = getGraphURL(context); 658 659 Element img = document.createElement("img"); 660 img.setAttribute("src", graphURL); 661 img.setAttribute("width", new Integer (chartWidth).toString()); 662 img.setAttribute("height", new Integer (chartHeight).toString()); 663 img.setAttribute("style", "border:0;"); 664 img.setAttribute("usemap", "#"+filename); 665 root.appendChild(img); 666 667 return document; 668 } 669 670 public String getGraphURL(RequestContext context) { 671 String graphURL = ""; 672 if (baseDisplayURLSet) { 673 graphURL = CHART_SERVLET; 674 } else { 675 graphURL = context.getRequest().getContextPath() + CHART_SERVLET; 676 } 677 graphURL = graphURL + (graphURL.indexOf('?') >= 0 ? "&" : "?") + "filename=" + getFilename(); 678 return graphURL; 679 } 680 681 693 public String writeImageMap(String name, 694 ChartRenderingInfo info, boolean useOverLibForToolTips) { 695 StringBuffer sb = new StringBuffer (); 696 sb.append("<map name=\"" + name + "\">"); 697 EntityCollection entities = info.getEntityCollection(); 698 Iterator iterator = entities.iterator(); 699 700 while (iterator.hasNext()) { 701 ChartEntity entity = (ChartEntity) iterator.next(); 702 705 String area=""; 706 707 if (useOverLibForToolTips) 708 area = entity.getImageMapAreaTag(new org.jfree.chart.imagemap.OverLIBToolTipTagFragmentGenerator(), new org.jfree.chart.imagemap.StandardURLTagFragmentGenerator()); 709 else 710 area = entity.getImageMapAreaTag(new org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator(), new org.jfree.chart.imagemap.StandardURLTagFragmentGenerator()); 711 area = area.replaceAll("&", "&"); 713 if (area.length() > 0) { 716 sb.append(area); 717 } 718 } 719 sb.append("</map>"); 720 return sb.toString(); 721 } 722 723 726 private String buildSlicer() { 727 StringBuffer slicer = new StringBuffer (); 728 slicer.append("Slicer: "); 729 Iterator pi = result.getSlicer().getPositions().iterator(); 730 while (pi.hasNext()) { 731 Position p = (Position) pi.next(); 732 Member[] members = p.getMembers(); 733 for (int i = 0; i < members.length; i++) { 734 slicer.append(members[i].getLevel().getLabel()); 735 slicer.append("="); 736 slicer.append(members[i].getLabel()); 737 if ( i < members.length-1 ) { 738 slicer.append(", "); 739 } 740 } 741 } 742 return slicer.toString(); 743 } 744 745 749 private DefaultCategoryDataset build1dimDataset() { 750 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 751 752 List columnPositions = result.getAxes()[0].getPositions(); 754 colCount = columnPositions.size(); 755 List cells = result.getCells(); 757 758 String series = "Series"; 759 760 for (int i = 0; i < colCount; i++) { 762 Member[] colMembers = ((Position) columnPositions.get(i)).getMembers(); 763 764 StringBuffer key = new StringBuffer (); 765 for (int j = 0; j < colMembers.length; j++) { 767 key.append(colMembers[j].getLabel() + "."); 769 } 770 dataset.addValue(getNumberValue((Cell)cells.get(i)), series, key.toString()); 771 } 772 return dataset; 773 } 774 775 781 private Number getNumberValue(Cell cell) { 782 Object value = cell.getValue(); 785 786 DecimalFormatSymbols dfs = new DecimalFormatSymbols (locale); 787 DecimalFormat formatter = new DecimalFormat (); 788 formatter.setDecimalFormatSymbols(dfs); 789 Number number = null; 790 try { 791 number = (Number )value; 792 } 794 catch (Exception e) { 795 number = null; 796 } 797 return number; 798 } 799 806 private String buildName( MemberTree myTree, Member [] members){ 807 String retValue = new String (); 808 HashMap levelMap = new HashMap (); 809 HashMap hierarchyMap = new HashMap (); 810 for (int j=members.length-1;j>=0;j--){ 811 Member member = members[j]; 812 while (member!=null){ 813 if (!levelMap.containsValue(member.getLevel())){ 815 levelMap.put(member.getLevel().toString(),member.getLevel()); 816 if (member.getRootDistance()==0){ 817 if (!hierarchyMap.containsValue(member.getLevel().getHierarchy()) 819 || myTree.getRootMembers(member.getLevel().getHierarchy()).length>1){ 820 hierarchyMap.put(member.getLevel().getHierarchy().toString(),member.getLevel().getHierarchy()); 821 retValue=member.getLabel()+"."+retValue; 822 } 823 } else{ 824 hierarchyMap.put(member.getLevel().getHierarchy().toString(),member.getLevel().getHierarchy()); 825 retValue=member.getLabel()+"."+retValue; 826 } 827 } 828 member = myTree.getParent(member); 829 } 830 } 831 return retValue; 832 } 833 837 private DefaultCategoryDataset build2dimDataset() { 838 839 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 840 841 843 List columnPositions = result.getAxes()[0].getPositions(); int colCount = columnPositions.size(); 845 846 848 List rowPositions = result.getAxes()[1].getPositions(); int rowCount = rowPositions.size(); 850 List cells = result.getCells(); 851 852 MemberTree myTree = ((MemberTree) olapModel.getExtension(MemberTree.ID)); 854 855 866 867 for(int i=0;i<colCount;i++){ 868 Position p= (Position) columnPositions.get(i); 869 Member colMembers[] =p.getMembers(); 870 871 String label=buildName(myTree, colMembers); 873 874 for(int k=0;k<rowCount;k++){ 876 Position rp= (Position) rowPositions.get(k); 877 Member rowMembers[] =rp.getMembers(); 878 879 String key=buildName(myTree, rowMembers); 881 882 Cell cell = (Cell) cells.get((k * colCount) + i); 883 dataset.addValue(getNumberValue(cell), label.toString(), key.toString()); 884 } 885 886 } 887 return dataset; 888 } 889 890 893 public int getColCount() { 894 return colCount; 895 } 896 897 900 public boolean isDirty() { 901 return dirty; 902 } 903 904 public void setDirty(boolean dirty) { 905 this.dirty = dirty; 906 } 907 908 public void modelChanged(ModelChangeEvent e) { 909 this.dirty = true; 910 } 911 912 public void structureChanged(ModelChangeEvent e) { 913 this.dirty = true; 914 } 915 916 919 public int getChartHeight() { 920 return chartHeight; 921 } 922 923 926 public void setChartHeight(int chartHeight) { 927 this.chartHeight = chartHeight; 928 this.dirty = true; 929 } 930 931 934 public String getChartTitle() { 935 return chartTitle; 936 } 937 938 941 public void setChartTitle(String chartTitle) { 942 this.chartTitle = chartTitle; 943 this.dirty = true; 944 } 945 946 949 public int getChartType() { 950 return chartType; 951 } 952 953 956 public void setChartType(int chartType) { 957 this.chartType = chartType; 958 this.dirty = true; 959 } 960 961 964 public int getChartWidth() { 965 return chartWidth; 966 } 967 968 971 public void setChartWidth(int chartWidth) { 972 this.chartWidth = chartWidth; 973 this.dirty = true; 974 } 975 976 979 public String getHorizAxisLabel() { 980 return horizAxisLabel; 981 } 982 983 986 public void setHorizAxisLabel(String axisLabel) { 987 horizAxisLabel = axisLabel; 988 this.dirty = true; 989 } 990 991 994 public boolean getShowLegend() { 995 return showLegend; 996 } 997 998 1001 public void setShowLegend(boolean showLegend) { 1002 this.showLegend = showLegend; 1003 this.dirty = true; 1004 } 1005 1006 1009 public String getFontName() { 1010 return fontName; 1011 } 1012 1013 1016 public void setFontName(String fontname) { 1017 this.fontName = fontname; 1018 this.dirty = true; 1019 } 1020 1021 1024 public String getVertAxisLabel() { 1025 return vertAxisLabel; 1026 } 1027 1028 1031 public void setVertAxisLabel(String axisLabel) { 1032 vertAxisLabel = axisLabel; 1033 this.dirty = true; 1034 } 1035 1036 1039 public int getFontSize() { 1040 return fontSize; 1041 } 1042 1043 1046 public void setFontSize(int fontSize) { 1047 this.fontSize = fontSize; 1048 this.dirty = true; 1049 } 1050 1051 1054 public int getFontStyle() { 1055 return fontStyle; 1056 } 1057 1058 1061 public void setFontStyle(int fontStyle) { 1062 this.fontStyle = fontStyle; 1063 this.dirty = true; 1064 } 1065 1066 1069 public int getBgColorB() { 1070 return bgColorB; 1071 } 1072 1073 1076 public void setBgColorB(int bgColorB) { 1077 this.bgColorB = checkRGB(bgColorB); 1078 this.dirty = true; 1079 } 1080 1081 1084 public int getBgColorG() { 1085 return bgColorG; 1086 } 1087 1088 1091 public void setBgColorG(int bgColorG) { 1092 this.bgColorG = checkRGB(bgColorG); 1093 this.dirty = true; 1094 } 1095 1096 1099 public int getBgColorR() { 1100 return bgColorR; 1101 } 1102 1103 1106 public void setBgColorR(int bgColorR) { 1107 this.bgColorR = checkRGB(bgColorR); 1108 this.dirty = true; 1109 } 1110 1111 1114 private int checkRGB(int v) { 1115 if ( v > 255 ) { 1116 v = 255; 1117 } 1118 else if ( v < 0 ) { 1119 v = 0; 1120 } 1121 return v; 1122 } 1123 1128 public class jpivotPieURLGenerator extends StandardPieURLGenerator { 1129 1130 private String prefix = ""; 1131 1132 private List cells = result.getCells(); 1133 1134 private int rowCount; 1135 1136 private TableOrder order; 1138 jpivotPieURLGenerator() { 1139 } 1140 1141 jpivotPieURLGenerator(String prefix) { 1142 this.prefix = prefix; 1143 } 1144 1148 jpivotPieURLGenerator(TableOrder order, DefaultCategoryDataset dataset){ 1149 this.order= order; 1150 this.rowCount=dataset.getRowCount(); 1151 } 1152 1153 1156 jpivotPieURLGenerator(TableOrder order, DefaultCategoryDataset dataset, String controllerURL){ 1157 this(order, dataset); 1158 this.prefix = controllerURL; 1159 } 1160 1172 public String generateURL(PieDataset data, Comparable key, int pieIndex) { 1173 String url = prefix; 1174 int index = data.getIndex(key); 1175 1176 int cellpos; 1177 if (order == TableOrder.BY_COLUMN){ 1178 cellpos = (pieIndex*rowCount)+index; 1179 } else { 1180 cellpos = pieIndex+(rowCount*index); 1181 } 1182 1183 if ( canDrillThrough((Cell) cells.get(cellpos)) && (!((Cell) cells.get(cellpos)).isNull()) ) { 1184 String id = DomUtils.randomId(); 1185 dispatcher.addRequestListener( 1186 id, 1187 null, 1188 new DrillThroughHandler((Cell) cells.get(cellpos))); 1189 1190 boolean firstParameter = url.indexOf("?") == -1; 1191 url += firstParameter ? "?" : "&"; 1192 url += id; 1193 return url; 1194 } 1195 else { 1196 return null; 1197 } 1198 } 1199 } 1200 1201 1206 public class jpivotCategoryURLGenerator extends StandardCategoryURLGenerator { 1207 1208 1209 private String prefix = ""; 1210 1211 1212 private String seriesParameterName = "col"; 1213 1214 1215 private String categoryParameterName = "row"; 1216 1217 private List cells = result.getCells(); 1218 1219 jpivotCategoryURLGenerator() { 1220 } 1221 1222 jpivotCategoryURLGenerator(String prefix) { 1223 this.prefix = prefix; 1224 } 1225 1237 public String generateURL(CategoryDataset data, int series, int category) { 1238 String url = prefix; 1239 int cellpos = (category * colCount) + series; 1243 if ( canDrillThrough((Cell) cells.get(cellpos)) && (!((Cell) cells.get(cellpos)).isNull()) ) { 1244 String id = DomUtils.randomId(); 1245 dispatcher.addRequestListener( 1246 id, 1247 null, 1248 new DrillThroughHandler((Cell) cells.get(cellpos))); 1249 1250 boolean firstParameter = url.indexOf("?") == -1; 1251 url += firstParameter ? "?" : "&"; 1252 url += id; 1253 return url; 1254 } 1255 else { 1256 return null; 1257 } 1258 } 1259 } 1260 1261 1267 class DrillThroughHandler implements RequestListener { 1295 Cell cell; 1296 DrillThroughHandler(Cell cell) { 1297 this.cell = cell; 1298 } 1299 public void request(RequestContext context) throws Exception { 1300 1301 if ( canDrillThrough(cell) ) { 1302 1303 HttpSession session = context.getSession(); 1304 final String drillTableRef = olapModel.getID() + ".drillthroughtable"; 1305 ITableComponent tc = 1306 (ITableComponent) session.getAttribute(drillTableRef); 1307 TableModel tm = drillThrough(cell); 1309 tc.setModel(tm); 1310 tc.setVisible(true); 1311 } 1312 } 1313 } 1314 1315 protected boolean canDrillThrough(Cell cell) { 1316 return ((DrillThrough) olapModel.getExtension(DrillThrough.ID)).canDrillThrough((Cell) cell.getRootDecoree()); 1317 } 1318 1319 1324 protected TableModel drillThrough(Cell cell) { 1325 return ((DrillThrough) olapModel.getExtension(DrillThrough.ID)).drillThrough((Cell) cell.getRootDecoree()); 1326 } 1327 1328 public boolean isDrillThroughEnabled() { 1329 return drillThroughEnabled; 1330 } 1331 1332 public void setDrillThroughEnabled(boolean drillThroughEnabled) { 1333 this.drillThroughEnabled = drillThroughEnabled; 1334 } 1335 1336 1339 public String getAxisFontName() { 1340 return axisFontName; 1341 } 1342 1343 1346 public void setAxisFontName(String axisFontName) { 1347 this.axisFontName = axisFontName; 1348 } 1349 1350 1353 public int getAxisFontSize() { 1354 return axisFontSize; 1355 } 1356 1357 1360 public void setAxisFontSize(int axisFontSize) { 1361 this.axisFontSize = axisFontSize; 1362 } 1363 1364 1367 public int getAxisFontStyle() { 1368 return axisFontStyle; 1369 } 1370 1371 1374 public void setAxisFontStyle(int axisFontStyle) { 1375 this.axisFontStyle = axisFontStyle; 1376 } 1377 1378 1381 public String getLegendFontName() { 1382 return legendFontName; 1383 } 1384 1385 1388 public void setLegendFontName(String legendFontName) { 1389 this.legendFontName = legendFontName; 1390 } 1391 1392 1395 public int getLegendFontSize() { 1396 return legendFontSize; 1397 } 1398 1399 1402 public void setLegendFontSize(int legendFontSize) { 1403 this.legendFontSize = legendFontSize; 1404 } 1405 1406 1409 public int getLegendFontStyle() { 1410 return legendFontStyle; 1411 } 1412 1413 1416 public void setLegendFontStyle(int legendFontStyle) { 1417 this.legendFontStyle = legendFontStyle; 1418 } 1419 1420 1423 public int getSlicerAlignment() { 1424 return slicerAlignment; 1425 } 1426 1427 1430 public void setSlicerAlignment(int slicerAlignment) { 1431 this.slicerAlignment = slicerAlignment; 1432 } 1433 1434 1437 public String getSlicerFontName() { 1438 return slicerFontName; 1439 } 1440 1441 1444 public void setSlicerFontName(String slicerFontName) { 1445 this.slicerFontName = slicerFontName; 1446 } 1447 1448 1451 public int getSlicerFontSize() { 1452 return slicerFontSize; 1453 } 1454 1455 1458 public void setSlicerFontSize(int slicerFontSize) { 1459 this.slicerFontSize = slicerFontSize; 1460 } 1461 1462 1465 public int getSlicerFontStyle() { 1466 return slicerFontStyle; 1467 } 1468 1469 1472 public void setSlicerFontStyle(int slicerFontStyle) { 1473 this.slicerFontStyle = slicerFontStyle; 1474 } 1475 1476 1479 public int getSlicerPosition() { 1480 return slicerPosition; 1481 } 1482 1483 1486 public void setSlicerPosition(int slicerPosition) { 1487 this.slicerPosition = slicerPosition; 1488 } 1489 1490 1493 public int getLegendPosition() { 1494 return legendPosition; 1495 } 1496 1497 1500 public void setLegendPosition(int legendPosition) { 1501 this.legendPosition = legendPosition; 1502 } 1503 1504 1507 public String getAxisTickFontName() { 1508 return axisTickFontName; 1509 } 1510 1511 1514 public void setAxisTickFontName(String axisTickFontName) { 1515 this.axisTickFontName = axisTickFontName; 1516 } 1517 1518 1521 public int getAxisTickFontSize() { 1522 return axisTickFontSize; 1523 } 1524 1525 1528 public void setAxisTickFontSize(int axisTickFontSize) { 1529 this.axisTickFontSize = axisTickFontSize; 1530 } 1531 1532 1535 public int getAxisTickFontStyle() { 1536 return axisTickFontStyle; 1537 } 1538 1539 1542 public void setAxisTickFontStyle(int axisTickFontStyle) { 1543 this.axisTickFontStyle = axisTickFontStyle; 1544 } 1545 1548 public int getTickLabelRotate() { 1549 return tickLabelRotate; 1550 } 1551 1554 public void setTickLabelRotate(int tickLabelRotate) { 1555 this.tickLabelRotate = tickLabelRotate; 1556 } 1557 1560 public boolean isShowSlicer() { 1561 return showSlicer; 1562 } 1563 1564 1567 public void setShowSlicer(boolean showSlicer) { 1568 this.showSlicer = showSlicer; 1569 } 1570 1571 1574 public String getFilename() { 1575 return filename; 1576 } 1577} 1578 | Popular Tags |