| 1 108 109 package org.jfree.chart; 110 111 import java.awt.Color ; 112 import java.awt.Font ; 113 import java.text.DateFormat ; 114 import java.text.NumberFormat ; 115 import java.util.Iterator ; 116 import java.util.List ; 117 118 import org.jfree.chart.axis.CategoryAxis; 119 import org.jfree.chart.axis.CategoryAxis3D; 120 import org.jfree.chart.axis.DateAxis; 121 import org.jfree.chart.axis.NumberAxis; 122 import org.jfree.chart.axis.NumberAxis3D; 123 import org.jfree.chart.axis.Timeline; 124 import org.jfree.chart.axis.ValueAxis; 125 import org.jfree.chart.labels.HighLowItemLabelGenerator; 126 import org.jfree.chart.labels.IntervalCategoryToolTipGenerator; 127 import org.jfree.chart.labels.ItemLabelAnchor; 128 import org.jfree.chart.labels.ItemLabelPosition; 129 import org.jfree.chart.labels.PieToolTipGenerator; 130 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 131 import org.jfree.chart.labels.StandardPieItemLabelGenerator; 132 import org.jfree.chart.labels.StandardXYToolTipGenerator; 133 import org.jfree.chart.labels.StandardXYZToolTipGenerator; 134 import org.jfree.chart.labels.XYToolTipGenerator; 135 import org.jfree.chart.plot.CategoryPlot; 136 import org.jfree.chart.plot.Marker; 137 import org.jfree.chart.plot.MultiplePiePlot; 138 import org.jfree.chart.plot.PiePlot; 139 import org.jfree.chart.plot.PiePlot3D; 140 import org.jfree.chart.plot.PlotOrientation; 141 import org.jfree.chart.plot.PolarPlot; 142 import org.jfree.chart.plot.RingPlot; 143 import org.jfree.chart.plot.ValueMarker; 144 import org.jfree.chart.plot.WaferMapPlot; 145 import org.jfree.chart.plot.XYPlot; 146 import org.jfree.chart.renderer.DefaultPolarItemRenderer; 147 import org.jfree.chart.renderer.WaferMapRenderer; 148 import org.jfree.chart.renderer.category.AreaRenderer; 149 import org.jfree.chart.renderer.category.BarRenderer; 150 import org.jfree.chart.renderer.category.BarRenderer3D; 151 import org.jfree.chart.renderer.category.CategoryItemRenderer; 152 import org.jfree.chart.renderer.category.GanttRenderer; 153 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 154 import org.jfree.chart.renderer.category.LineRenderer3D; 155 import org.jfree.chart.renderer.category.StackedAreaRenderer; 156 import org.jfree.chart.renderer.category.StackedBarRenderer; 157 import org.jfree.chart.renderer.category.StackedBarRenderer3D; 158 import org.jfree.chart.renderer.category.WaterfallBarRenderer; 159 import org.jfree.chart.renderer.xy.CandlestickRenderer; 160 import org.jfree.chart.renderer.xy.HighLowRenderer; 161 import org.jfree.chart.renderer.xy.SignalRenderer; 162 import org.jfree.chart.renderer.xy.StackedXYAreaRenderer; 163 import org.jfree.chart.renderer.xy.StandardXYItemRenderer; 164 import org.jfree.chart.renderer.xy.WindItemRenderer; 165 import org.jfree.chart.renderer.xy.XYAreaRenderer; 166 import org.jfree.chart.renderer.xy.XYBarRenderer; 167 import org.jfree.chart.renderer.xy.XYBoxAndWhiskerRenderer; 168 import org.jfree.chart.renderer.xy.XYBubbleRenderer; 169 import org.jfree.chart.renderer.xy.XYItemRenderer; 170 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 171 import org.jfree.chart.renderer.xy.XYStepAreaRenderer; 172 import org.jfree.chart.renderer.xy.XYStepRenderer; 173 import org.jfree.chart.title.TextTitle; 174 import org.jfree.chart.urls.PieURLGenerator; 175 import org.jfree.chart.urls.StandardCategoryURLGenerator; 176 import org.jfree.chart.urls.StandardPieURLGenerator; 177 import org.jfree.chart.urls.StandardXYURLGenerator; 178 import org.jfree.chart.urls.StandardXYZURLGenerator; 179 import org.jfree.chart.urls.XYURLGenerator; 180 import org.jfree.data.category.CategoryDataset; 181 import org.jfree.data.category.IntervalCategoryDataset; 182 import org.jfree.data.general.DefaultPieDataset; 183 import org.jfree.data.general.PieDataset; 184 import org.jfree.data.general.WaferMapDataset; 185 import org.jfree.data.statistics.BoxAndWhiskerXYDataset; 186 import org.jfree.data.xy.IntervalXYDataset; 187 import org.jfree.data.xy.OHLCDataset; 188 import org.jfree.data.xy.SignalsDataset; 189 import org.jfree.data.xy.TableXYDataset; 190 import org.jfree.data.xy.WindDataset; 191 import org.jfree.data.xy.XYDataset; 192 import org.jfree.data.xy.XYZDataset; 193 import org.jfree.ui.Layer; 194 import org.jfree.ui.RectangleEdge; 195 import org.jfree.ui.RectangleInsets; 196 import org.jfree.ui.TextAnchor; 197 import org.jfree.util.SortOrder; 198 import org.jfree.util.TableOrder; 199 200 204 public abstract class ChartFactory { 205 206 220 public static JFreeChart createPieChart(String title, 221 PieDataset dataset, 222 boolean legend, 223 boolean tooltips, 224 boolean urls) { 225 226 PiePlot plot = new PiePlot(dataset); 227 plot.setLabelGenerator(new StandardPieItemLabelGenerator()); 228 plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); 229 if (tooltips) { 230 plot.setToolTipGenerator( 231 new StandardPieItemLabelGenerator( 232 StandardPieItemLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT 233 ) 234 ); 235 } 236 if (urls) { 237 plot.setURLGenerator(new StandardPieURLGenerator()); 238 } 239 return new JFreeChart( 240 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 241 ); 242 243 } 244 245 283 public static JFreeChart createPieChart(String title, 284 PieDataset dataset, 285 PieDataset previousDataset, 286 int percentDiffForMaxScale, 287 boolean greenForIncrease, 288 boolean legend, 289 boolean tooltips, 290 boolean urls, 291 boolean subTitle, 292 boolean showDifference) { 293 294 PiePlot plot = new PiePlot(dataset); 295 plot.setLabelGenerator(new StandardPieItemLabelGenerator()); 296 plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); 297 298 if (tooltips) { 299 plot.setToolTipGenerator( 300 new StandardPieItemLabelGenerator( 301 StandardPieItemLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT 302 ) 303 ); 304 } 305 if (urls) { 306 plot.setURLGenerator(new StandardPieURLGenerator()); 307 } 308 309 List keys = dataset.getKeys(); 310 DefaultPieDataset series = null; 311 if (showDifference) { 312 series = new DefaultPieDataset(); 313 } 314 315 double colorPerPercent = 255.0 / percentDiffForMaxScale; 316 for (Iterator it = keys.iterator(); it.hasNext();) { 317 Comparable key = (Comparable ) it.next(); 318 Number newValue = dataset.getValue(key); 319 Number oldValue = previousDataset.getValue(key); 320 int section = dataset.getIndex(key); 321 322 if (oldValue == null) { 323 if (greenForIncrease) { 324 plot.setSectionPaint(section, Color.green); 325 } 326 else { 327 plot.setSectionPaint(section, Color.red); 328 } 329 if (showDifference) { 330 series.setValue(key + " (+100%)", newValue); 331 } 332 } 333 else { 334 double percentChange = (newValue.doubleValue() 335 / oldValue.doubleValue() - 1.0) * 100.0; 336 double shade 337 = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255 338 : Math.abs(percentChange) * colorPerPercent); 339 if (greenForIncrease 340 && newValue.doubleValue() > oldValue.doubleValue() 341 || !greenForIncrease && newValue.doubleValue() 342 < oldValue.doubleValue()) { 343 plot.setSectionPaint(section, new Color (0, (int) shade, 0)); 344 } 345 else { 346 plot.setSectionPaint(section, new Color ((int) shade, 0, 0)); 347 } 348 if (showDifference) { 349 series.setValue( 350 key + " (" + (percentChange >= 0 ? "+" : "") 351 + NumberFormat.getPercentInstance().format( 352 percentChange / 100.0) + ")", newValue 353 ); 354 } 355 } 356 } 357 358 if (showDifference) { 359 plot.setDataset(series); 360 } 361 362 JFreeChart chart = new JFreeChart( 363 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 364 ); 365 366 if (subTitle) { 367 TextTitle subtitle = null; 368 subtitle = new TextTitle( 369 "Bright " + (greenForIncrease ? "red" : "green") 370 + "=change >=-" + percentDiffForMaxScale + "%, Bright " 371 + (!greenForIncrease ? "red" : "green") + "=change >=+" 372 + percentDiffForMaxScale + "%", 373 new Font ("SansSerif", Font.PLAIN, 10) 374 ); 375 chart.addSubtitle(subtitle); 376 } 377 378 return chart; 379 } 380 381 395 public static JFreeChart createRingChart(String title, 396 PieDataset dataset, 397 boolean legend, 398 boolean tooltips, 399 boolean urls) { 400 401 RingPlot plot = new RingPlot(dataset); 402 plot.setLabelGenerator(new StandardPieItemLabelGenerator()); 403 plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); 404 if (tooltips) { 405 plot.setToolTipGenerator( 406 new StandardPieItemLabelGenerator( 407 StandardPieItemLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT 408 ) 409 ); 410 } 411 if (urls) { 412 plot.setURLGenerator(new StandardPieURLGenerator()); 413 } 414 return new JFreeChart( 415 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 416 ); 417 418 } 419 420 435 public static JFreeChart createMultiplePieChart(String title, 436 CategoryDataset dataset, 437 TableOrder order, 438 boolean legend, 439 boolean tooltips, 440 boolean urls) { 441 442 if (order == null) { 443 throw new IllegalArgumentException ("Null 'order' argument."); 444 } 445 MultiplePiePlot plot = new MultiplePiePlot(dataset); 446 plot.setDataExtractOrder(order); 447 plot.setBackgroundPaint(null); 448 plot.setOutlineStroke(null); 449 450 if (tooltips) { 451 PieToolTipGenerator tooltipGenerator 452 = new StandardPieItemLabelGenerator(); 453 PiePlot pp = (PiePlot) plot.getPieChart().getPlot(); 454 pp.setToolTipGenerator(tooltipGenerator); 455 } 456 457 if (urls) { 458 PieURLGenerator urlGenerator = new StandardPieURLGenerator(); 459 PiePlot pp = (PiePlot) plot.getPieChart().getPlot(); 460 pp.setURLGenerator(urlGenerator); 461 } 462 463 JFreeChart chart = new JFreeChart( 464 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 465 ); 466 467 return chart; 468 469 } 470 471 484 public static JFreeChart createPieChart3D(String title, 485 PieDataset dataset, 486 boolean legend, 487 boolean tooltips, 488 boolean urls) { 489 490 PiePlot3D plot = new PiePlot3D(dataset); 491 plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); 492 if (tooltips) { 493 plot.setToolTipGenerator(new StandardPieItemLabelGenerator()); 494 } 495 if (urls) { 496 plot.setURLGenerator(new StandardPieURLGenerator()); 497 } 498 return new JFreeChart( 499 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 500 ); 501 502 } 503 504 519 public static JFreeChart createMultiplePieChart3D(String title, 520 CategoryDataset dataset, 521 TableOrder order, 522 boolean legend, 523 boolean tooltips, 524 boolean urls) { 525 526 if (order == null) { 527 throw new IllegalArgumentException ("Null 'order' argument."); 528 } 529 MultiplePiePlot plot = new MultiplePiePlot(dataset); 530 plot.setDataExtractOrder(order); 531 plot.setBackgroundPaint(null); 532 plot.setOutlineStroke(null); 533 534 JFreeChart pieChart = new JFreeChart(new PiePlot3D(null)); 535 TextTitle seriesTitle = new TextTitle( 536 "Series Title", new Font ("SansSerif", Font.BOLD, 12) 537 ); 538 seriesTitle.setPosition(RectangleEdge.BOTTOM); 539 pieChart.setTitle(seriesTitle); 540 pieChart.removeLegend(); 541 pieChart.setBackgroundPaint(null); 542 plot.setPieChart(pieChart); 543 544 if (tooltips) { 545 PieToolTipGenerator tooltipGenerator 546 = new StandardPieItemLabelGenerator(); 547 PiePlot pp = (PiePlot) plot.getPieChart().getPlot(); 548 pp.setToolTipGenerator(tooltipGenerator); 549 } 550 551 if (urls) { 552 PieURLGenerator urlGenerator = new StandardPieURLGenerator(); 553 PiePlot pp = (PiePlot) plot.getPieChart().getPlot(); 554 pp.setURLGenerator(urlGenerator); 555 } 556 557 JFreeChart chart = new JFreeChart( 558 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 559 ); 560 561 return chart; 562 563 } 564 565 585 public static JFreeChart createBarChart(String title, 586 String categoryAxisLabel, 587 String valueAxisLabel, 588 CategoryDataset dataset, 589 PlotOrientation orientation, 590 boolean legend, 591 boolean tooltips, 592 boolean urls) { 593 594 if (orientation == null) { 595 throw new IllegalArgumentException ("Null 'orientation' argument."); 596 } 597 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 598 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 599 600 BarRenderer renderer = new BarRenderer(); 601 if (orientation == PlotOrientation.HORIZONTAL) { 602 ItemLabelPosition position1 = new ItemLabelPosition( 603 ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT 604 ); 605 renderer.setPositiveItemLabelPosition(position1); 606 ItemLabelPosition position2 = new ItemLabelPosition( 607 ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT 608 ); 609 renderer.setNegativeItemLabelPosition(position2); 610 } 611 else if (orientation == PlotOrientation.VERTICAL) { 612 ItemLabelPosition position1 = new ItemLabelPosition( 613 ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER 614 ); 615 renderer.setPositiveItemLabelPosition(position1); 616 ItemLabelPosition position2 = new ItemLabelPosition( 617 ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER 618 ); 619 renderer.setNegativeItemLabelPosition(position2); 620 } 621 if (tooltips) { 622 renderer.setBaseToolTipGenerator( 623 new StandardCategoryToolTipGenerator() 624 ); 625 } 626 if (urls) { 627 renderer.setBaseItemURLGenerator( 628 new StandardCategoryURLGenerator() 629 ); 630 } 631 632 CategoryPlot plot = new CategoryPlot( 633 dataset, categoryAxis, valueAxis, renderer 634 ); 635 plot.setOrientation(orientation); 636 JFreeChart chart = new JFreeChart( 637 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 638 ); 639 640 return chart; 641 642 } 643 644 665 public static JFreeChart createStackedBarChart(String title, 666 String domainAxisLabel, 667 String rangeAxisLabel, 668 CategoryDataset dataset, 669 PlotOrientation orientation, 670 boolean legend, 671 boolean tooltips, 672 boolean urls) { 673 674 if (orientation == null) { 675 throw new IllegalArgumentException ("Null 'orientation' argument."); 676 } 677 678 CategoryAxis categoryAxis = new CategoryAxis(domainAxisLabel); 679 ValueAxis valueAxis = new NumberAxis(rangeAxisLabel); 680 681 StackedBarRenderer renderer = new StackedBarRenderer(); 682 if (tooltips) { 683 renderer.setBaseToolTipGenerator( 684 new StandardCategoryToolTipGenerator() 685 ); 686 } 687 if (urls) { 688 renderer.setBaseItemURLGenerator( 689 new StandardCategoryURLGenerator() 690 ); 691 } 692 693 CategoryPlot plot = new CategoryPlot( 694 dataset, categoryAxis, valueAxis, renderer 695 ); 696 plot.setOrientation(orientation); 697 JFreeChart chart = new JFreeChart( 698 title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend 699 ); 700 701 return chart; 702 703 } 704 705 725 public static JFreeChart createBarChart3D(String title, 726 String categoryAxisLabel, 727 String valueAxisLabel, 728 CategoryDataset dataset, 729 PlotOrientation orientation, 730 boolean legend, 731 boolean tooltips, 732 boolean urls) { 733 734 if (orientation == null) { 735 throw new IllegalArgumentException ("Null 'orientation' argument."); 736 } 737 CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel); 738 ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel); 739 740 BarRenderer3D renderer = new BarRenderer3D(); 741 if (too
|