1 17 18 package org.pentaho.plugin.jfreechart; 19 20 import java.awt.Color ; 21 import java.awt.Font ; 22 import java.awt.GradientPaint ; 23 import java.awt.Image ; 24 import java.awt.Paint ; 25 import java.awt.TexturePaint ; 26 import java.awt.geom.Rectangle2D ; 27 import java.awt.image.BufferedImage ; 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStreamWriter ; 33 import java.io.PrintWriter ; 34 import java.io.Writer ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 import java.util.TimeZone ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 41 import javax.imageio.ImageIO ; 42 43 import org.apache.batik.dom.GenericDOMImplementation; 44 import org.apache.batik.svggen.SVGGraphics2D; 45 import org.dom4j.Node; 46 import org.jfree.chart.ChartRenderingInfo; 47 import org.jfree.chart.ChartUtilities; 48 import org.jfree.chart.JFreeChart; 49 import org.jfree.chart.axis.CategoryAxis; 50 import org.jfree.chart.axis.DateAxis; 51 import org.jfree.chart.axis.NumberAxis; 52 import org.jfree.chart.axis.NumberAxis3D; 53 import org.jfree.chart.axis.ValueAxis; 54 import org.jfree.chart.labels.ItemLabelAnchor; 55 import org.jfree.chart.labels.ItemLabelPosition; 56 import org.jfree.chart.labels.PieToolTipGenerator; 57 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 58 import org.jfree.chart.labels.StandardPieSectionLabelGenerator; 59 import org.jfree.chart.labels.StandardXYToolTipGenerator; 60 import org.jfree.chart.labels.StandardPieToolTipGenerator; 61 import org.jfree.chart.plot.CategoryPlot; 62 import org.jfree.chart.plot.DefaultDrawingSupplier; 63 import org.jfree.chart.plot.MeterInterval; 64 import org.jfree.chart.plot.MeterPlot; 65 import org.jfree.chart.plot.MultiplePiePlot; 66 import org.jfree.chart.plot.PiePlot; 67 import org.jfree.chart.plot.PiePlot3D; 68 import org.jfree.chart.plot.Plot; 69 import org.jfree.chart.plot.PlotOrientation; 70 import org.jfree.chart.plot.XYPlot; 71 import org.jfree.chart.renderer.category.AreaRenderer; 72 import org.jfree.chart.renderer.category.BarRenderer; 73 import org.jfree.chart.renderer.category.BarRenderer3D; 74 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 75 import org.jfree.chart.renderer.category.LineRenderer3D; 76 import org.jfree.chart.renderer.category.StackedAreaRenderer; 77 import org.jfree.chart.renderer.category.StackedBarRenderer; 78 import org.jfree.chart.renderer.category.StackedBarRenderer3D; 79 import org.jfree.chart.renderer.xy.XYItemRenderer; 80 import org.jfree.chart.renderer.xy.XYAreaRenderer; 81 import org.jfree.chart.renderer.xy.XYDifferenceRenderer; 82 import org.jfree.chart.renderer.xy.XYDotRenderer; 83 import org.jfree.chart.renderer.xy.XYLine3DRenderer; 84 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 85 import org.jfree.chart.renderer.xy.XYStepRenderer; 86 import org.jfree.chart.renderer.xy.XYStepAreaRenderer; 87 import org.jfree.chart.title.TextTitle; 88 import org.jfree.chart.urls.PieURLGenerator; 89 import org.jfree.chart.urls.StandardCategoryURLGenerator; 90 import org.jfree.chart.urls.StandardXYURLGenerator; 91 import org.jfree.chart.urls.StandardPieURLGenerator; 92 import org.jfree.data.Range; 93 import org.jfree.data.general.Dataset; 94 import org.jfree.ui.RectangleEdge; 95 import org.jfree.ui.TextAnchor; 96 import org.jfree.util.TableOrder; 97 import org.pentaho.core.repository.ISolutionRepository; 98 import org.pentaho.core.session.IPentahoSession; 99 import org.pentaho.core.system.PentahoSystem; 100 import org.pentaho.messages.Messages; 101 import org.pentaho.messages.util.LocaleHelper; 102 import org.pentaho.util.logging.ILogger; 103 import org.w3c.dom.Document ; 104 105 public class JFreeChartEngine { 106 107 public static final int OUTPUT_PNG = 1; 108 109 public static final int OUTPUT_SVG = 2; 110 111 private static final String X1_NODE_NAME = "x1"; 114 private static final String Y1_NODE_NAME = "y1"; 116 private static final String X2_NODE_NAME = "x2"; 118 private static final String Y2_NODE_NAME = "y2"; 120 private static final String COLOR1_NODE_NAME = "color1"; 122 private static final String COLOR2_NODE_NAME = "color2"; 124 private static final String CYCLIC_NODE_NAME = "cyclic"; 126 private static final String TEXTURE_IMAGE_NODE_NAME = "texture-image"; 128 private static final String FONT_FAMILY_NODE_NAME = "font-family"; 130 private static final String SIZE_NODE_NAME = "size"; 132 private static final String IS_ITALIC_NODE_NAME = "is-italic"; 134 private static final String IS_BOLD_NODE_NAME = "is-bold"; 136 public static final int UNDEFINED_CHART_TYPE = -1; 137 138 public static final int DIAL_CHART_TYPE = 0; 139 140 public static final int THERMOMETER_CHART_TYPE = 1; 141 142 public static final int PIE_CHART_TYPE = 2; 143 144 public static final int PIE_GRID_CHART_TYPE = 3; 145 146 public static final int BAR_CHART_TYPE = 4; 147 148 public static final int LINE_CHART_TYPE = 5; 149 150 public static final int AREA_CHART_TYPE = 6; 151 152 public static final int STEP_CHART_TYPE = 7; 153 154 public static final int STEP_AREA_CHART_TYPE = 8; 155 156 public static final int DIFFERENCE_CHART_TYPE = 9; 157 158 public static final int DOT_CHART_TYPE = 10; 159 160 172 private static JFreeChart createDial(DialWidgetDefinition widget, String title, String units) { 173 MeterPlot meter = new MeterPlot(widget); 174 175 updatePlot(meter, widget); 176 meter.setUnits(units); 177 178 meter.setValuePaint(widget.getValuePaint()); 179 meter.setTickPaint(widget.getTickPaint()); 180 int tickSize = widget.getTickSize(); 181 if (tickSize == 0) { 182 meter.setTickLabelsVisible(false); 183 } else { 184 meter.setTickLabelsVisible(true); 185 meter.setTickSize(tickSize); 186 } 187 if (widget.getValueFont() != null) { 188 meter.setValueFont(widget.getValueFont()); 189 } else { 190 meter.setValuePaint( getTransparent() ); 191 } 192 Font titleFont = widget.getTitleFont(); 194 195 JFreeChart chart = new JFreeChart(title, titleFont, meter, false); 196 197 199 return chart; 200 } 201 202 private static JFreeChart createXYSeriesCollectionChart(XYSeriesCollectionChartDefinition chartDefinition) { 203 JFreeChart chart = null; 204 String domainAxisLabel = null; 206 String rangeAxisLabel = null; 207 boolean tooltips = true; 208 boolean urls = true; 209 211 String title = chartDefinition.getTitle(); 212 boolean legend = chartDefinition.isLegendIncluded(); 213 214 NumberAxis domainAxis = chartDefinition.isThreeD() ? new NumberAxis3D(domainAxisLabel) : new NumberAxis(domainAxisLabel); 215 domainAxis.setAutoRangeIncludesZero( chartDefinition.isDomainIncludesZero() ); 216 domainAxis.setAutoRangeStickyZero( chartDefinition.isDomainStickyZero() ); 217 ValueAxis rangeAxis = new NumberAxis(rangeAxisLabel); 218 219 XYItemRenderer renderer = null; 220 switch (chartDefinition.getChartType()) { 221 case LINE_CHART_TYPE: 222 renderer = chartDefinition.isThreeD() ? new XYLine3DRenderer() : new XYLineAndShapeRenderer(true, false); 223 break; 224 case AREA_CHART_TYPE: 225 renderer = new XYAreaRenderer(); 226 break; 227 case STEP_CHART_TYPE: 228 renderer = new XYStepRenderer(); 229 break; 230 case STEP_AREA_CHART_TYPE: 231 renderer = new XYStepAreaRenderer(); 232 break; 233 case DIFFERENCE_CHART_TYPE: 234 renderer = new XYDifferenceRenderer(); 235 break; 236 case DOT_CHART_TYPE: 237 renderer = new XYDotRenderer(); 238 break; 239 default: 240 break; 241 } 242 if (tooltips) { 243 renderer.setToolTipGenerator(new StandardXYToolTipGenerator()); 244 } 245 if (urls) { 246 renderer.setURLGenerator(new StandardXYURLGenerator()); 247 } 248 249 XYPlot plot = new XYPlot(chartDefinition, domainAxis, rangeAxis, renderer); 250 updatePlot(plot, chartDefinition); 251 252 chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 253 254 return chart; 255 256 } 257 258 private static JFreeChart createTimeSeriesCollectionChart(TimeSeriesCollectionChartDefinition chartDefinition) { 259 JFreeChart chart = null; 260 String domainAxisLabel = null; 262 String rangeAxisLabel = null; 263 boolean tooltips = true; 264 boolean urls = true; 265 267 String title = chartDefinition.getTitle(); 268 boolean legend = chartDefinition.isLegendIncluded(); 269 270 DateAxis domainAxis = new DateAxis(domainAxisLabel, TimeZone.getDefault()); 271 ValueAxis rangeAxis = new NumberAxis(rangeAxisLabel); 272 273 XYItemRenderer renderer = null; 274 switch (chartDefinition.getChartType()) { 275 case LINE_CHART_TYPE: 276 renderer = chartDefinition.isThreeD() ? new XYLine3DRenderer() : new XYLineAndShapeRenderer(true, false); 277 break; 278 case AREA_CHART_TYPE: 279 renderer = new XYAreaRenderer(); 280 break; 281 case STEP_CHART_TYPE: 282 renderer = new XYStepRenderer(); 283 break; 284 case STEP_AREA_CHART_TYPE: 285 renderer = new XYStepAreaRenderer(); 286 break; 287 case DIFFERENCE_CHART_TYPE: 288 renderer = new XYDifferenceRenderer(); 289 break; 290 case DOT_CHART_TYPE: 291 renderer = new XYDotRenderer(); 292 break; 293 default: 294 break; 295 } 296 if (tooltips) { 297 renderer.setToolTipGenerator(new StandardXYToolTipGenerator()); 298 } 299 if (urls) { 300 renderer.setURLGenerator(new StandardXYURLGenerator()); 301 } 302 303 XYPlot plot = new XYPlot(chartDefinition, domainAxis, rangeAxis, renderer); 304 updatePlot(plot, chartDefinition); 305 306 chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 307 308 return chart; 309 310 } 311 312 private static JFreeChart createCategoryDatasetChart(CategoryDatasetChartDefinition chartDefinition) { 313 JFreeChart chart = null; 314 switch (chartDefinition.getChartType()) { 315 case BAR_CHART_TYPE: 316 chart = createBarChart(chartDefinition); 317 break; 318 case LINE_CHART_TYPE: 319 chart = createLineChart(chartDefinition); 320 break; 321 case PIE_GRID_CHART_TYPE: 322 chart = createMultiplePieChart(chartDefinition); 323 break; 324 case AREA_CHART_TYPE: 325 chart = createAreaChart(chartDefinition); 326 break; 327 default: 328 break; 329 } 330 return chart; 331 } 332 333 private static JFreeChart createPieDatasetChart(PieDatasetChartDefinition chartDefinition) { 334 boolean tooltips = true; 336 boolean urls = true; 337 339 String title = chartDefinition.getTitle(); 340 boolean legend = chartDefinition.isLegendIncluded(); 341 342 PiePlot plot = null; 343 plot = chartDefinition.isThreeD() ? new PiePlot3D(chartDefinition) : new PiePlot(chartDefinition); 344 updatePlot(plot, chartDefinition); 345 JFreeChart pieChart = new JFreeChart(title, chartDefinition.getTitleFont(), plot, legend); 346 TextTitle seriesTitle = new TextTitle("Series Title", new Font ("SansSerif", Font.BOLD, 12)); seriesTitle.setPosition(RectangleEdge.BOTTOM); 348 pieChart.setTitle(title); 349 pieChart.setBackgroundPaint(chartDefinition.getChartBackgroundPaint()); 350 351 if (tooltips) { 352 PieToolTipGenerator tooltipGenerator = new StandardPieToolTipGenerator(); 353 plot.setToolTipGenerator(tooltipGenerator); 354 } 355 356 if (urls) { 357 PieURLGenerator urlGenerator = new StandardPieURLGenerator(); 358 plot.setURLGenerator(urlGenerator); 359 } 360 361 return pieChart; 362 } 363 364 private static void updatePlot(Plot plot, ChartDefinition chartDefinition) { 365 plot.setBackgroundPaint(chartDefinition.getPlotBackgroundPaint()); 366 plot.setBackgroundImage(chartDefinition.getPlotBackgroundImage()); 367 368 if (chartDefinition.getPaintSequence() != null) { 370 DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(chartDefinition.getPaintSequence(), DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, 371 DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE); 372 plot.setDrawingSupplier(drawingSupplier); 373 } 374 plot.setOutlineStroke(null); 376 if (plot instanceof CategoryPlot) { 377 CategoryPlot categoryPlot = (CategoryPlot) plot; 378 CategoryDatasetChartDefinition categoryDatasetChartDefintion = (CategoryDatasetChartDefinition) chartDefinition; 379 categoryPlot.setOrientation(categoryDatasetChartDefintion.getOrientation()); 380 CategoryAxis domainAxis = categoryPlot.getDomainAxis(); 381 if (domainAxis != null) { 382 domainAxis.setLabel(categoryDatasetChartDefintion.getDomainTitle()); 383 domainAxis.setLabelFont(categoryDatasetChartDefintion.getDomainTitleFont()); 384 domainAxis.setCategoryLabelPositions(categoryDatasetChartDefintion.getCategoryLabelPositions()); 385 } 386 ValueAxis valueAxis = categoryPlot.getRangeAxis(); 387 if (valueAxis != null) { 388 valueAxis.setLabel(categoryDatasetChartDefintion.getRangeTitle()); 389 valueAxis.setLabelFont(categoryDatasetChartDefintion.getRangeTitleFont()); 390 } 391 } 392 if (plot instanceof PiePlot) { 393 PiePlot pie = (PiePlot) plot; 394 PieDatasetChartDefinition pieDefinition = (PieDatasetChartDefinition) chartDefinition; 395 pie.setInteriorGap(pieDefinition.getInteriorGap()); 396 pie.setStartAngle(pieDefinition.getStartAngle()); 397 pie.setLabelFont(pieDefinition.getLabelFont()); 398 if (pieDefinition.getLabelPaint() != null) { 399 pie.setLabelPaint(pieDefinition.getLabelPaint()); 400 } 401 pie.setLabelBackgroundPaint(pieDefinition.getLabelBackgroundPaint()); 402 if( pieDefinition.isLegendIncluded() ) { 403 StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator("{0}"); pie.setLegendLabelGenerator(labelGen); 405 } 406 407 pie.setLabelGap(pieDefinition.getLabelGap()); 408 if (!pieDefinition.isDisplayLabels()) { 409 pie.setLabelGenerator(null); 410 } else { 411 if( pieDefinition.isLegendIncluded() ) { 412 StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator("{1} ({2})"); pie.setLabelGenerator(labelGen); 414 } else { 415 StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator("{0} = {1} ({2})"); pie.setLabelGenerator(labelGen); 417 } 418 } 419 } 420 if (plot instanceof MultiplePiePlot) { 421 MultiplePiePlot pies = (MultiplePiePlot) plot; 422 CategoryDatasetChartDefinition categoryDatasetChartDefintion = (CategoryDatasetChartDefinition) chartDefinition; 423 pies.setDataset(categoryDatasetChartDefintion); 424 } 425 if (plot instanceof MeterPlot) { 426 MeterPlot meter = (MeterPlot) plot; 427 DialWidgetDefinition widget = (DialWidgetDefinition) chartDefinition; 428 List intervals = widget.getIntervals(); 429 Iterator intervalIterator = intervals.iterator(); 430 while (intervalIterator.hasNext()) { 431 MeterInterval interval = (MeterInterval) intervalIterator.next(); 432 meter.addInterval(interval); 433 } 434 435 meter.setNeedlePaint(widget.getNeedlePaint()); 436 meter.setDialShape(widget.getDialShape()); 437 meter.setDialBackgroundPaint(widget.getPlotBackgroundPaint()); 438 meter.setRange(new Range(widget.getMinimum(), widget.getMaximum())); 439 440 } 441 if (plot instanceof XYPlot) { 442 XYPlot xyPlot = (XYPlot) plot; 443 if (chartDefinition instanceof XYSeriesCollectionChartDefinition) { 444 XYSeriesCollectionChartDefinition xySeriesCollectionChartDefintion = (XYSeriesCollectionChartDefinition) chartDefinition; 445 xyPlot.setOrientation(xySeriesCollectionChartDefintion.getOrientation()); 446 ValueAxis domainAxis = xyPlot.getDomainAxis(); 447 if (domainAxis != null) { 448 domainAxis.setLabel(xySeriesCollectionChartDefintion.getDomainTitle()); 449 domainAxis.setLabelFont(xySeriesCollectionChartDefintion.getDomainTitleFont()); 450 domainAxis.setVerticalTickLabels(xySeriesCollectionChartDefintion.isDomainVerticalTickLabels()); 451 } 452 ValueAxis rangeAxis = xyPlot.getRangeAxis(); 453 if (rangeAxis != null) { 454 rangeAxis.setLabel(xySeriesCollectionChartDefintion.getRangeTitle()); 455 rangeAxis.setLabelFont(xySeriesCollectionChartDefintion.getRangeTitleFont()); 456 } 457 } else if (chartDefinition instanceof TimeSeriesCollectionChartDefinition) { 458 TimeSeriesCollectionChartDefinition timeSeriesCollectionChartDefintion = (TimeSeriesCollectionChartDefinition) chartDefinition; 459 xyPlot.setOrientation(timeSeriesCollectionChartDefintion.getOrientation()); 460 ValueAxis domainAxis = xyPlot.getDomainAxis(); 461 if (domainAxis != null) { 462 domainAxis.setLabel(timeSeriesCollectionChartDefintion.getDomainTitle()); 463 domainAxis.setLabelFont(timeSeriesCollectionChartDefintion.getDomainTitleFont()); 464 domainAxis.setVerticalTickLabels(timeSeriesCollectionChartDefintion.isDomainVerticalTickLabels()); 465 } 466 ValueAxis rangeAxis = xyPlot.getRangeAxis(); 467 if (rangeAxis != null) { 468 rangeAxis.setLabel(timeSeriesCollectionChartDefintion.getRangeTitle()); 469 rangeAxis.setLabelFont(timeSeriesCollectionChartDefintion.getRangeTitleFont()); 470 } 471 } 472 } 473 } 474 475 private static JFreeChart createMultiplePieChart(CategoryDatasetChartDefinition chartDefinition) { 476 TableOrder order = TableOrder.BY_ROW; 478 482 String title = chartDefinition.getTitle(); 483 boolean legend = chartDefinition.isLegendIncluded(); 484 485 if (order == null) { 486 throw new IllegalArgumentException (Messages.getString("JFreeChartEngine.ERROR_0001_NULL_ORDER_ARGUMENT")); } 488 MultiplePiePlot plot = new MultiplePiePlot(chartDefinition); 489 updatePlot(plot, chartDefinition); 490 plot.setDataExtractOrder(order); 491 492 JFreeChart pieChart = new JFreeChart(title, chartDefinition.getTitleFont(), plot, legend); 493 TextTitle seriesTitle = new TextTitle("Series Title", new Font ("SansSerif", Font.BOLD, 12)); seriesTitle.setPosition(RectangleEdge.BOTTOM); 495 pieChart.setTitle(seriesTitle); 496 pieChart.setBackgroundPaint(chartDefinition.getChartBackgroundPaint()); 497 plot.setPieChart(pieChart); 498 499 506 512 return pieChart; 513 } 514 515 private static JFreeChart createLineChart(CategoryDatasetChartDefinition chartDefinition) { 516 String categoryAxisLabel = null; 518 String valueAxisLabel = null; 519 boolean tooltips = true; 520 boolean urls = true; 521 523 String title = chartDefinition.getTitle(); 524 boolean legend = chartDefinition.isLegendIncluded(); 525 526 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 527 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 528 LineAndShapeRenderer renderer = chartDefinition.isThreeD() ? new LineRenderer3D() : new LineAndShapeRenderer(true, false); 529 if (tooltips) { 530 renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator()); 531 } 532 if (urls) { 533 renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator()); 534 } 535 536 CategoryPlot plot = new CategoryPlot(chartDefinition, categoryAxis, valueAxis, renderer); 537 updatePlot(plot, chartDefinition); 538 539 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 540 541 return chart; 542 543 } 544 545 private static JFreeChart createBarChart(CategoryDatasetChartDefinition chartDefinition) { 546 String categoryAxisLabel = null; 548 String valueAxisLabel = null; 549 boolean tooltips = true; 550 boolean urls = true; 551 553 String title = chartDefinition.getTitle(); 554 boolean legend = chartDefinition.isLegendIncluded(); 555 PlotOrientation orientation = chartDefinition.getOrientation(); 556 557 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 558 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 559 BarRenderer renderer = null; 560 if (chartDefinition.isStacked() || chartDefinition.isThreeD()) { 562 if (chartDefinition.isStacked() && chartDefinition.isThreeD()) { 563 renderer = new StackedBarRenderer3D(); 564 } else if (chartDefinition.isStacked()) { 565 renderer = new StackedBarRenderer(); 566 } else { 567 renderer = new BarRenderer3D(); 568 } 569 } else { 570 renderer = new BarRenderer(); 571 } 572 if (orientation == PlotOrientation.HORIZONTAL) { 573 ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT); 574 renderer.setPositiveItemLabelPosition(position1); 575 ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT); 576 renderer.setNegativeItemLabelPosition(position2); 577 } else if (orientation == PlotOrientation.VERTICAL) { 578 ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER); 579 renderer.setPositiveItemLabelPosition(position1); 580 ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER); 581 renderer.setNegativeItemLabelPosition(position2); 582 } 583 if (tooltips) { 584 renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator()); 585 } 586 if (urls) { 587 renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator()); 588 } 589 590 CategoryPlot plot = new CategoryPlot(chartDefinition, categoryAxis, valueAxis, renderer); 591 updatePlot(plot, chartDefinition); 592 593 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 594 595 return chart; 596 597 } 598 599 private static JFreeChart createAreaChart(CategoryDatasetChartDefinition chartDefinition) { 600 String categoryAxisLabel = null; 602 String valueAxisLabel = null; 603 boolean tooltips = true; 604 boolean urls = true; 605 607 String title = chartDefinition.getTitle(); 608 boolean legend = chartDefinition.isLegendIncluded(); 609 610 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 611 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 612 613 AreaRenderer renderer = chartDefinition.isStacked() ? new StackedAreaRenderer() : new AreaRenderer(); 614 if (tooltips) { 615 renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator()); 616 } 617 if (urls) { 618 renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator()); 619 } 620 621 CategoryPlot plot = new CategoryPlot(chartDefinition, categoryAxis, valueAxis, renderer); 622 updatePlot(plot, chartDefinition); 623 624 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 625 626 return chart; 627 } 628 629 644 private static void saveChartAsPNG(JFreeChart chart, String path, int width, int height, PrintWriter writer, ChartRenderingInfo info) throws IOException { 645 File file = new File (path + ".png"); ChartUtilities.saveChartAsPNG(file, chart, width, height, info); 647 648 650 652 if (writer != null) { 654 ChartUtilities.writeImageMap(writer, "map-name", info, true); } 656 } 657 658 671 private static void saveChartAsSVG(JFreeChart chart, String path, int width, int height, ChartRenderingInfo info) throws IOException { 672 org.w3c.dom.DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); 675 Document document = domImpl.createDocument(null, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); 679 svgGenerator.getGeneratorContext().setPrecision(6); 681 chart.draw(svgGenerator, new Rectangle2D.Double (0, 0, width, height), info); 683 boolean useCSS = true; 686 Writer out = new OutputStreamWriter (new FileOutputStream (new File (path + ".svg")), LocaleHelper.getSystemEncoding()); svgGenerator.stream(out, useCSS); 688 } 689 690 706 private static void saveChart(JFreeChart chart, String path, int width, int height, int outputType, PrintWriter writer, ChartRenderingInfo info) throws IOException { 707 if (outputType == OUTPUT_PNG) { 708 saveChartAsPNG(chart, path, width, height, writer, info); 709 } else if (outputType == OUTPUT_SVG) { 710 saveChartAsSVG(chart, path, width, height, info); 711 } 712 } 714 715 739 public static void saveChart(Dataset dataset, String title, String units, String fileName, int width, int height, int outputType, PrintWriter writer, ILogger logger) { 740 ChartRenderingInfo info = new ChartRenderingInfo(); 741 saveChart(dataset, title, units, fileName, width, height, outputType, writer, info, logger); 742 } 743 744 768 public static void saveChart(Dataset dataset, String title, String units, String fileName, int width, int height, int outputType, PrintWriter writer, ChartRenderingInfo info, ILogger logger) { 769 770 JFreeChart chart = null; 771 if (dataset instanceof DialWidgetDefinition) { 772 chart = createDial((DialWidgetDefinition) dataset, title, units); 773 } else if (dataset instanceof TimeSeriesCollectionChartDefinition) { 774 chart = createTimeSeriesCollectionChart((TimeSeriesCollectionChartDefinition) dataset); 775 } else if (dataset instanceof XYSeriesCollectionChartDefinition) { 776 chart = createXYSeriesCollectionChart((XYSeriesCollectionChartDefinition) dataset); 777 } else if (dataset instanceof CategoryDatasetChartDefinition) { 778 chart = createCategoryDatasetChart((CategoryDatasetChartDefinition) dataset); 779 } else if (dataset instanceof PieDatasetChartDefinition) { 780 chart = createPieDatasetChart((PieDatasetChartDefinition) dataset); 781 } 782 if (chart == null) { 783 logger.error(Messages.getString("ChartEngine.ERROR_0002_COULD_NOT_CREATE_CHART")); } else { 785 ChartDefinition chartDefinition = (ChartDefinition) dataset; 787 Iterator iter = chartDefinition.getSubtitles().iterator(); 788 while (iter.hasNext()) { 789 chart.addSubtitle(new TextTitle(iter.next().toString())); 790 } 791 chart.setBackgroundPaint(chartDefinition.getChartBackgroundPaint()); 792 chart.setBackgroundImage(chartDefinition.getChartBackgroundImage()); 793 chart.setBorderVisible(chartDefinition.isBorderVisible()); 794 chart.setBorderPaint(chartDefinition.getBorderPaint()); 795 chart.getTitle().setPosition(chartDefinition.getTitlePosition()); 796 chart.getTitle().setFont(chartDefinition.getTitleFont()); 797 798 } 801 try { 802 String filePath = PentahoSystem.getApplicationContext().getFileOutputPath(fileName); 803 saveChart(chart, filePath, width, height, outputType, writer, info); 804 } catch (IOException e) { 805 logger.error(Messages.getString("ChartEngine.ERROR_0001_COULD_NOT_CREATE_CHART"), e); } 807 } 808 809 816 public static Color getColor(String htmlColor) { 817 818 return new Color (Integer.parseInt(htmlColor.substring(1, 3), 16), Integer.parseInt(htmlColor.substring(3, 5), 16), Integer.parseInt(htmlColor.substring(5), 16)); 819 } 820 821 public static Color getTransparent( ) { 822 return new Color ( 0, 0, 0, 0); 823 } 824 825 837 public static Paint getPaint(Node node, int width, int height, IPentahoSession session) { 838 if (node == null) { 839 return null; 840 } 841 if (null != node.selectSingleNode("gradient")) { return getGradientPaint((node.selectSingleNode("gradient")), width, height); } else if (null != node.selectSingleNode("texture")) { return getTexturePaint((node.selectSingleNode("texture")), width, height, session); } else { 847 String htmlColor = node.getText(); 848 return getPaint(htmlColor); 849 } 850 851 } 852 853 875 922 public static Font getFont(Node fontNode) { 923 Font font = null; 924 if (fontNode != null) { 925 String fontFamily = TextTitle.DEFAULT_FONT.getFamily(); 926 int fontStyle = Font.PLAIN; 927 int fontSize = TextTitle.DEFAULT_FONT.getSize(); 928 929 Node fontFamilyNode = fontNode.selectSingleNode(FONT_FAMILY_NODE_NAME); 930 if (fontFamilyNode != null && fontFamilyNode.getText().length() > 0) { 931 fontFamily = fontFamilyNode.getText(); 932 } 933 Node isBoldNode = fontNode.selectSingleNode(IS_BOLD_NODE_NAME); 934 if (isBoldNode != null) { 935 boolean bold = Boolean.valueOf(isBoldNode.getText()).booleanValue(); 936 if (bold) { 937 fontStyle += Font.BOLD; 938 } 939 } 940 Node isItalicNode = fontNode.selectSingleNode(IS_ITALIC_NODE_NAME); 941 if (isItalicNode != null) { 942 boolean italic = Boolean.valueOf(isItalicNode.getText()).booleanValue(); 943 if (italic) { 944 fontStyle += Font.ITALIC; 945 } 946 } 947 Node sizeNode = fontNode.selectSingleNode(SIZE_NODE_NAME); 948 if (sizeNode != null && sizeNode.getText().length() > 0) { 949 fontSize = Integer.parseInt(sizeNode.getText()); 950 } 951 font = new Font (fontFamily, fontStyle, fontSize); 952 } 953 return font; 954 } 955 956 964 public static Paint getPaint(Node node) { 965 if (node == null) { 966 return null; 967 } 968 String htmlColor = node.getText(); 970 return getPaint(htmlColor); 971 } 972 973 980 public static Paint getPaint(String htmlColor) { 981 982 Color color = getColor(htmlColor); 983 984 return color; 985 } 986 987 992 public static Paint getGradientPaint(Node gradientNode, int width, int height) { 993 994 if (gradientNode == null) { 995 return null; 996 } 997 float x1 = 0; 998 float y1 = 0; 999 float x2 = width; 1000 float y2 = height; 1001 1002 if (gradientNode.selectSingleNode(X1_NODE_NAME) != null) { 1003 x1 = Float.parseFloat(gradientNode.selectSingleNode(X1_NODE_NAME).getText()); 1004 } 1005 if (gradientNode.selectSingleNode(Y1_NODE_NAME) != null) { 1006 y1 = Float.parseFloat(gradientNode.selectSingleNode(Y1_NODE_NAME).getText()); 1007 } 1008 if (gradientNode.selectSingleNode(X2_NODE_NAME) != null) { 1009 x2 = Float.parseFloat(gradientNode.selectSingleNode(X2_NODE_NAME).getText()); 1010 } 1011 if (gradientNode.selectSingleNode(Y2_NODE_NAME) != null) { 1012 y2 = Float.parseFloat(gradientNode.selectSingleNode(Y2_NODE_NAME).getText()); 1013 } 1014 Color color1 = getColor(gradientNode.selectSingleNode(COLOR1_NODE_NAME).getText()); 1015 Color color2 = getColor(gradientNode.selectSingleNode(COLOR2_NODE_NAME).getText()); 1016 boolean cyclic = false; 1017 if (gradientNode.selectSingleNode(CYCLIC_NODE_NAME) != null) { 1018 cyclic = Boolean.valueOf(gradientNode.selectSingleNode(CYCLIC_NODE_NAME).getText()).booleanValue(); 1019 } 1020 1021 Paint paint = new GradientPaint (x1, y1, color1, x2, y2, color2, cyclic); 1022 return paint; 1023 } 1024 1025 1026 1031 public static Paint getTexturePaint(Node textureNode, int width, int height, IPentahoSession session) { 1032 1033 if (textureNode == null) { 1034 return null; 1035 } 1036 double x1 = 0.0; 1037 double y1 = 0.0; 1038 double x2 = width; 1039 double y2 = height; 1040 BufferedImage texture = (BufferedImage ) getImage(textureNode.selectSingleNode(TEXTURE_IMAGE_NODE_NAME), session); 1041 if (textureNode.selectSingleNode(X1_NODE_NAME) != null) { 1042 x1 = Double.parseDouble(textureNode.selectSingleNode(X1_NODE_NAME).getText()); 1043 } 1044 if (textureNode.selectSingleNode(Y1_NODE_NAME) != null) { 1045 y1 = Double.parseDouble(textureNode.selectSingleNode(Y1_NODE_NAME).getText()); 1046 } 1047 if (textureNode.selectSingleNode(X2_NODE_NAME) != null) { 1048 x2 = Double.parseDouble(textureNode.selectSingleNode(X2_NODE_NAME).getText()); 1049 } 1050 if (textureNode.selectSingleNode(Y2_NODE_NAME) != null) { 1051 y2 = Double.parseDouble(textureNode.selectSingleNode(Y2_NODE_NAME).getText()); 1052 } 1053 Rectangle2D anchor = new Rectangle2D.Double (x1, y1, x2, y2); 1054 1055 Paint paint = new TexturePaint (texture, anchor); 1056 return paint; 1057 } 1058 1059 1064 public static Image getImage(Node chartBackgroundImageNode, IPentahoSession session) { 1065 Image image = null; 1066 if (chartBackgroundImageNode != null && chartBackgroundImageNode.getText().length() > 0) { 1067 return getImage(chartBackgroundImageNode.getText(), session); 1068 } 1069 return image; 1070 } 1071 1072 1077 public static Image getImage(String imageName, IPentahoSession session) { 1078 Image image = null; 1079 ISolutionRepository repository = PentahoSystem.getSolutionRepository(session); 1080 InputStream is = repository.getResourceInputStream(imageName); 1081 try { 1082 image = ImageIO.read(is); 1083 } catch (IOException e) { 1084 e.printStackTrace(); 1086 } 1087 return image; 1088 } 1089 1090} 1091 | Popular Tags |