001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * --------------- 028 * JFreeChart.java 029 * --------------- 030 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Andrzej Porebski; 034 * David Li; 035 * Wolfgang Irler; 036 * Christian W. Zuckschwerdt; 037 * Klaus Rheinwald; 038 * Nicolas Brodu; 039 * 040 * NOTE: The above list of contributors lists only the people that have 041 * contributed to this source file (JFreeChart.java) - for a list of ALL 042 * contributors to the project, please see the README.txt file. 043 * 044 * Changes (from 20-Jun-2001) 045 * -------------------------- 046 * 20-Jun-2001 : Modifications submitted by Andrzej Porebski for legend 047 * placement; 048 * 21-Jun-2001 : Removed JFreeChart parameter from Plot constructors (DG); 049 * 22-Jun-2001 : Multiple titles added (original code by David Berry, with 050 * reworkings by DG); 051 * 18-Sep-2001 : Updated header (DG); 052 * 15-Oct-2001 : Moved data source classes into new package 053 * com.jrefinery.data.* (DG); 054 * 18-Oct-2001 : New factory method for creating VerticalXYBarChart (DG); 055 * 19-Oct-2001 : Moved series paint and stroke methods to the Plot class (DG); 056 * Moved static chart creation methods to new ChartFactory 057 * class (DG); 058 * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG); 059 * Fixed bug where chart isn't registered with the dataset (DG); 060 * 07-Nov-2001 : Fixed bug where null title in constructor causes 061 * exception (DG); 062 * Tidied up event notification code (DG); 063 * 17-Nov-2001 : Added getLegendItemCount() method (DG); 064 * 21-Nov-2001 : Set clipping in draw method to ensure that nothing gets drawn 065 * outside the chart area (DG); 066 * 11-Dec-2001 : Added the createBufferedImage() method, taken from the 067 * JFreeChartServletDemo class (DG); 068 * 13-Dec-2001 : Added tooltips (DG); 069 * 16-Jan-2002 : Added handleClick() method (DG); 070 * 22-Jan-2002 : Fixed bug correlating legend labels with pie data (DG); 071 * 05-Feb-2002 : Removed redundant tooltips code (DG); 072 * 19-Feb-2002 : Added accessor methods for the backgroundImage and 073 * backgroundImageAlpha attributes (DG); 074 * 21-Feb-2002 : Added static fields for INFO, COPYRIGHT, LICENCE, CONTRIBUTORS 075 * and LIBRARIES. These can be used to display information about 076 * JFreeChart (DG); 077 * 06-Mar-2002 : Moved constants to JFreeChartConstants interface (DG); 078 * 18-Apr-2002 : PieDataset is no longer sorted (oldman); 079 * 23-Apr-2002 : Moved dataset to the Plot class (DG); 080 * 13-Jun-2002 : Added an extra draw() method (DG); 081 * 25-Jun-2002 : Implemented the Drawable interface and removed redundant 082 * imports (DG); 083 * 26-Jun-2002 : Added another createBufferedImage() method (DG); 084 * 18-Sep-2002 : Fixed issues reported by Checkstyle (DG); 085 * 23-Sep-2002 : Added new contributor (DG); 086 * 28-Oct-2002 : Created main title and subtitle list to replace existing title 087 * list (DG); 088 * 08-Jan-2003 : Added contributor (DG); 089 * 17-Jan-2003 : Added new constructor (DG); 090 * 22-Jan-2003 : Added ChartColor class by Cameron Riley, and background image 091 * alignment code by Christian W. Zuckschwerdt (DG); 092 * 11-Feb-2003 : Added flag to allow suppression of chart change events, based 093 * on a suggestion by Klaus Rheinwald (DG); 094 * 04-Mar-2003 : Added small fix for suppressed chart change events (see bug id 095 * 690865) (DG); 096 * 10-Mar-2003 : Added Benoit Xhenseval to contributors (DG); 097 * 26-Mar-2003 : Implemented Serializable (DG); 098 * 15-Jul-2003 : Added an optional border for the chart (DG); 099 * 11-Sep-2003 : Took care of listeners while cloning (NB); 100 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 101 * 22-Sep-2003 : Added nullpointer checks. 102 * 25-Sep-2003 : Added nullpointer checks too (NB). 103 * 03-Dec-2003 : Legends are now registered by this class instead of using the 104 * old constructor way (TM); 105 * 03-Dec-2003 : Added anchorPoint to draw() method (DG); 106 * 08-Jan-2004 : Reworked title code, introducing line wrapping (DG); 107 * 09-Feb-2004 : Created additional createBufferedImage() method (DG); 108 * 05-Apr-2004 : Added new createBufferedImage() method (DG); 109 * 27-May-2004 : Moved constants from JFreeChartConstants.java back to this 110 * class (DG); 111 * 25-Nov-2004 : Updates for changes to Title class (DG); 112 * 06-Jan-2005 : Change lookup for default background color (DG); 113 * 31-Jan-2005 : Added Don Elliott to contributors (DG); 114 * 02-Feb-2005 : Added clearSubtitles() method (DG); 115 * 03-Feb-2005 : Added Mofeed Shahin to contributors (DG); 116 * 08-Feb-2005 : Updated for RectangleConstraint changes (DG); 117 * 28-Mar-2005 : Renamed Legend --> OldLegend (DG); 118 * 12-Apr-2005 : Added methods to access legend(s) in subtitle list (DG); 119 * 13-Apr-2005 : Added removeLegend() and removeSubtitle() methods (DG); 120 * 20-Apr-2005 : Modified to collect chart entities from titles and 121 * subtitles (DG); 122 * 26-Apr-2005 : Removed LOGGER (DG); 123 * 06-Jun-2005 : Added addLegend() method and padding attribute, fixed equals() 124 * method (DG); 125 * 24-Nov-2005 : Removed OldLegend and related code - don't want to support 126 * this in 1.0.0 final (DG); 127 * ------------- JFREECHART 1.0.x --------------------------------------------- 128 * 27-Jan-2006 : Updated version number (DG); 129 * 07-Dec-2006 : Added some missing credits (DG); 130 * 17-Jan-2007 : Added Darren Jung to contributor list (DG); 131 * 05-Mar-2007 : Added Sergei Ivanov to the contributor list (DG); 132 * 16-Mar-2007 : Modified initial legend border (DG); 133 * 22-Mar-2007 : New methods for text anti-aliasing (DG); 134 * 16-May-2007 : Fixed argument check in getSubtitle(), copy list in 135 * get/setSubtitles(), and added new addSubtitle(int, Title) 136 * method (DG); 137 * 05-Jun-2007 : Add change listener to default legend (DG); 138 * 04-Dec-2007 : In createBufferedImage() methods, make the default image type 139 * BufferedImage.TYPE_INT_ARGB (thanks to Klaus Rheinwald) (DG); 140 * 05-Dec-2007 : Fixed bug 1749124 (not registering as listener with 141 * TextTitle) (DG); 142 * 23-Apr-2008 : Added new contributor (Diego Pierangeli) (DG); 143 * 16-May-2008 : Added new contributor (Michael Siemer) (DG); 144 * 19-Sep-2008 : Check for title visibility (DG); 145 * 146 */ 147 148 package org.jfree.chart; 149 150 import java.awt.AlphaComposite; 151 import java.awt.BasicStroke; 152 import java.awt.Color; 153 import java.awt.Composite; 154 import java.awt.Font; 155 import java.awt.Graphics2D; 156 import java.awt.Image; 157 import java.awt.Paint; 158 import java.awt.RenderingHints; 159 import java.awt.Shape; 160 import java.awt.Stroke; 161 import java.awt.geom.AffineTransform; 162 import java.awt.geom.Point2D; 163 import java.awt.geom.Rectangle2D; 164 import java.awt.image.BufferedImage; 165 import java.io.IOException; 166 import java.io.ObjectInputStream; 167 import java.io.ObjectOutputStream; 168 import java.io.Serializable; 169 import java.net.URL; 170 import java.util.ArrayList; 171 import java.util.Arrays; 172 import java.util.Iterator; 173 import java.util.List; 174 import java.util.ResourceBundle; 175 176 import javax.swing.ImageIcon; 177 import javax.swing.UIManager; 178 import javax.swing.event.EventListenerList; 179 180 import org.jfree.JCommon; 181 import org.jfree.chart.block.BlockParams; 182 import org.jfree.chart.block.EntityBlockResult; 183 import org.jfree.chart.block.LengthConstraintType; 184 import org.jfree.chart.block.LineBorder; 185 import org.jfree.chart.block.RectangleConstraint; 186 import org.jfree.chart.entity.EntityCollection; 187 import org.jfree.chart.event.ChartChangeEvent; 188 import org.jfree.chart.event.ChartChangeListener; 189 import org.jfree.chart.event.ChartProgressEvent; 190 import org.jfree.chart.event.ChartProgressListener; 191 import org.jfree.chart.event.PlotChangeEvent; 192 import org.jfree.chart.event.PlotChangeListener; 193 import org.jfree.chart.event.TitleChangeEvent; 194 import org.jfree.chart.event.TitleChangeListener; 195 import org.jfree.chart.plot.CategoryPlot; 196 import org.jfree.chart.plot.Plot; 197 import org.jfree.chart.plot.PlotRenderingInfo; 198 import org.jfree.chart.plot.XYPlot; 199 import org.jfree.chart.title.LegendTitle; 200 import org.jfree.chart.title.TextTitle; 201 import org.jfree.chart.title.Title; 202 import org.jfree.data.Range; 203 import org.jfree.io.SerialUtilities; 204 import org.jfree.ui.Align; 205 import org.jfree.ui.Drawable; 206 import org.jfree.ui.HorizontalAlignment; 207 import org.jfree.ui.RectangleEdge; 208 import org.jfree.ui.RectangleInsets; 209 import org.jfree.ui.Size2D; 210 import org.jfree.ui.VerticalAlignment; 211 import org.jfree.ui.about.Contributor; 212 import org.jfree.ui.about.Licences; 213 import org.jfree.ui.about.ProjectInfo; 214 import org.jfree.util.ObjectUtilities; 215 import org.jfree.util.PaintUtilities; 216 217 /** 218 * A chart class implemented using the Java 2D APIs. The current version 219 * supports bar charts, line charts, pie charts and xy plots (including time 220 * series data). 221 * <P> 222 * JFreeChart coordinates several objects to achieve its aim of being able to 223 * draw a chart on a Java 2D graphics device: a list of {@link Title} objects 224 * (which often includes the chart's legend), a {@link Plot} and a 225 * {@link org.jfree.data.general.Dataset} (the plot in turn manages a 226 * domain axis and a range axis). 227 * <P> 228 * You should use a {@link ChartPanel} to display a chart in a GUI. 229 * <P> 230 * The {@link ChartFactory} class contains static methods for creating 231 * 'ready-made' charts. 232 * 233 * @see ChartPanel 234 * @see ChartFactory 235 * @see Title 236 * @see Plot 237 */ 238 public class JFreeChart implements Drawable, 239 TitleChangeListener, 240 PlotChangeListener, 241 Serializable, 242 Cloneable { 243 244 /** For serialization. */ 245 private static final long serialVersionUID = -3470703747817429120L; 246 247 /** Information about the project. */ 248 public static final ProjectInfo INFO = new JFreeChartInfo(); 249 250 /** The default font for titles. */ 251 public static final Font DEFAULT_TITLE_FONT 252 = new Font("SansSerif", Font.BOLD, 18); 253 254 /** The default background color. */ 255 public static final Paint DEFAULT_BACKGROUND_PAINT 256 = UIManager.getColor("Panel.background"); 257 258 /** The default background image. */ 259 public static final Image DEFAULT_BACKGROUND_IMAGE = null; 260 261 /** The default background image alignment. */ 262 public static final int DEFAULT_BACKGROUND_IMAGE_ALIGNMENT = Align.FIT; 263 264 /** The default background image alpha. */ 265 public static final float DEFAULT_BACKGROUND_IMAGE_ALPHA = 0.5f; 266 267 /** 268 * Rendering hints that will be used for chart drawing. This should never 269 * be <code>null</code>. 270 */ 271 private transient RenderingHints renderingHints; 272 273 /** A flag that controls whether or not the chart border is drawn. */ 274 private boolean borderVisible; 275 276 /** The stroke used to draw the chart border (if visible). */ 277 private transient Stroke borderStroke; 278 279 /** The paint used to draw the chart border (if visible). */ 280 private transient Paint borderPaint; 281 282 /** The padding between the chart border and the chart drawing area. */ 283 private RectangleInsets padding; 284 285 /** The chart title (optional). */ 286 private TextTitle title; 287 288 /** 289 * The chart subtitles (zero, one or many). This field should never be 290 * <code>null</code>. 291 */ 292 private List subtitles; 293 294 /** Draws the visual representation of the data. */ 295 private Plot plot; 296 297 /** Paint used to draw the background of the chart. */ 298 private transient Paint backgroundPaint; 299 300 /** An optional background image for the chart. */ 301 private transient Image backgroundImage; // todo: not serialized yet 302 303 /** The alignment for the background image. */ 304 private int backgroundImageAlignment = Align.FIT; 305 306 /** The alpha transparency for the background image. */ 307 private float backgroundImageAlpha = 0.5f; 308 309 /** Storage for registered change listeners. */ 310 private transient EventListenerList changeListeners; 311 312 /** Storage for registered progress listeners. */ 313 private transient EventListenerList progressListeners; 314 315 /** 316 * A flag that can be used to enable/disable notification of chart change 317 * events. 318 */ 319 private boolean notify; 320 321 /** 322 * Creates a new chart based on the supplied plot. The chart will have 323 * a legend added automatically, but no title (although you can easily add 324 * one later). 325 * <br><br> 326 * Note that the {@link ChartFactory} class contains a range 327 * of static methods that will return ready-made charts, and often this 328 * is a more convenient way to create charts than using this constructor. 329 * 330 * @param plot the plot (<code>null</code> not permitted). 331 */ 332 public JFreeChart(Plot plot) { 333 this(null, null, plot, true); 334 } 335 336 /** 337 * Creates a new chart with the given title and plot. A default font 338 * ({@link #DEFAULT_TITLE_FONT}) is used for the title, and the chart will 339 * have a legend added automatically. 340 * <br><br> 341 * Note that the {@link ChartFactory} class contains a range 342 * of static methods that will return ready-made charts, and often this 343 * is a more convenient way to create charts than using this constructor. 344 * 345 * @param title the chart title (<code>null</code> permitted). 346 * @param plot the plot (<code>null</code> not permitted). 347 */ 348 public JFreeChart(String title, Plot plot) { 349 this(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); 350 } 351 352 /** 353 * Creates a new chart with the given title and plot. The 354 * <code>createLegend</code> argument specifies whether or not a legend 355 * should be added to the chart. 356 * <br><br> 357 * Note that the {@link ChartFactory} class contains a range 358 * of static methods that will return ready-made charts, and often this 359 * is a more convenient way to create charts than using this constructor. 360 * 361 * @param title the chart title (<code>null</code> permitted). 362 * @param titleFont the font for displaying the chart title 363 * (<code>null</code> permitted). 364 * @param plot controller of the visual representation of the data 365 * (<code>null</code> not permitted). 366 * @param createLegend a flag indicating whether or not a legend should 367 * be created for the chart. 368 */ 369 public JFreeChart(String title, Font titleFont, Plot plot, 370 boolean createLegend) { 371 372 if (plot == null) { 373 throw new NullPointerException("Null 'plot' argument."); 374 } 375 376 // create storage for listeners... 377 this.progressListeners = new EventListenerList(); 378 this.changeListeners = new EventListenerList(); 379 this.notify = true; // default is to notify listeners when the 380 // chart changes 381 382 this.renderingHints = new RenderingHints( 383 RenderingHints.KEY_ANTIALIASING, 384 RenderingHints.VALUE_ANTIALIAS_ON); 385 386 this.borderVisible = false; 387 this.borderStroke = new BasicStroke(1.0f); 388 this.borderPaint = Color.black; 389 390 this.padding = RectangleInsets.ZERO_INSETS; 391 392 this.plot = plot; 393 plot.addChangeListener(this); 394 395 this.subtitles = new ArrayList(); 396 397 // create a legend, if requested... 398 if (createLegend) { 399 LegendTitle legend = new LegendTitle(this.plot); 400 legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0)); 401 legend.setFrame(new LineBorder()); 402 legend.setBackgroundPaint(Color.white); 403 legend.setPosition(RectangleEdge.BOTTOM); 404 this.subtitles.add(legend); 405 legend.addChangeListener(this); 406 } 407 408 // add the chart title, if one has been specified... 409 if (title != null) { 410 if (titleFont == null) { 411 titleFont = DEFAULT_TITLE_FONT; 412 } 413 this.title = new TextTitle(title, titleFont); 414 this.title.addChangeListener(this); 415 } 416 417 this.backgroundPaint = DEFAULT_BACKGROUND_PAINT; 418 419 this.backgroundImage = DEFAULT_BACKGROUND_IMAGE; 420 this.backgroundImageAlignment = DEFAULT_BACKGROUND_IMAGE_ALIGNMENT; 421 this.backgroundImageAlpha = DEFAULT_BACKGROUND_IMAGE_ALPHA; 422 423 } 424 425 /** 426 * Returns the collection of rendering hints for the chart. 427 * 428 * @return The rendering hints for the chart (never <code>null</code>). 429 * 430 * @see #setRenderingHints(RenderingHints) 431 */ 432 public RenderingHints getRenderingHints() { 433 return this.renderingHints; 434 } 435 436 /** 437 * Sets the rendering hints for the chart. These will be added (using the 438 * Graphics2D.addRenderingHints() method) near the start of the 439 * JFreeChart.draw() method. 440 * 441 * @param renderingHints the rendering hints (<code>null</code> not 442 * permitted). 443 * 444 * @see #getRenderingHints() 445 */ 446 public void setRenderingHints(RenderingHints renderingHints) { 447 if (renderingHints == null) { 448 throw new NullPointerException("RenderingHints given are null"); 449 } 450 this.renderingHints = renderingHints; 451 fireChartChanged(); 452 } 453 454 /** 455 * Returns a flag that controls whether or not a border is drawn around the 456 * outside of the chart. 457 * 458 * @return A boolean. 459 * 460 * @see #setBorderVisible(boolean) 461 */ 462 public boolean isBorderVisible() { 463 return this.borderVisible; 464 } 465 466 /** 467 * Sets a flag that controls whether or not a border is drawn around the 468 * outside of the chart. 469 * 470 * @param visible the flag. 471 * 472 * @see #isBorderVisible() 473 */ 474 public void setBorderVisible(boolean visible) { 475 this.borderVisible = visible; 476 fireChartChanged(); 477 } 478 479 /** 480 * Returns the stroke used to draw the chart border (if visible). 481 * 482 * @return The border stroke. 483 * 484 * @see #setBorderStroke(Stroke) 485 */ 486 public Stroke getBorderStroke() { 487 return this.borderStroke; 488 } 489 490 /** 491 * Sets the stroke used to draw the chart border (if visible). 492 * 493 * @param stroke the stroke. 494 * 495 * @see #getBorderStroke() 496 */ 497 public void setBorderStroke(Stroke stroke) { 498 this.borderStroke = stroke; 499 fireChartChanged(); 500 } 501 502 /** 503 * Returns the paint used to draw the chart border (if visible). 504 * 505 * @return The border paint. 506 * 507 * @see #setBorderPaint(Paint) 508 */ 509 public Paint getBorderPaint() { 510 return this.borderPaint; 511 } 512 513 /** 514 * Sets the paint used to draw the chart border (if visible). 515 * 516 * @param paint the paint. 517 * 518 * @see #getBorderPaint() 519 */ 520 public void setBorderPaint(Paint paint) { 521 this.borderPaint = paint; 522 fireChartChanged(); 523 } 524 525 /** 526 * Returns the padding between the chart border and the chart drawing area. 527 * 528 * @return The padding (never <code>null</code>). 529 * 530 * @see #setPadding(RectangleInsets) 531 */ 532 public RectangleInsets getPadding() { 533 return this.padding; 534 } 535 536 /** 537 * Sets the padding between the chart border and the chart drawing area, 538 * and sends a {@link ChartChangeEvent} to all registered listeners. 539 * 540 * @param padding the padding (<code>null</code> not permitted). 541 * 542 * @see #getPadding() 543 */ 544 public void setPadding(RectangleInsets padding) { 545 if (padding == null) { 546 throw new IllegalArgumentException("Null 'padding' argument."); 547 } 548 this.padding = padding; 549 notifyListeners(new ChartChangeEvent(this)); 550 } 551 552 /** 553 * Returns the main chart title. Very often a chart will have just one 554 * title, so we make this case simple by providing accessor methods for 555 * the main title. However, multiple titles are supported - see the 556 * {@link #addSubtitle(Title)} method. 557 * 558 * @return The chart title (possibly <code>null</code>). 559 * 560 * @see #setTitle(TextTitle) 561 */ 562 public TextTitle getTitle() { 563 return this.title; 564 } 565 566 /** 567 * Sets the main title for the chart and sends a {@link ChartChangeEvent} 568 * to all registered listeners. If you do not want a title for the 569 * chart, set it to <code>null</code>. If you want more than one title on 570 * a chart, use the {@link #addSubtitle(Title)} method. 571 * 572 * @param title the title (<code>null</code> permitted). 573 * 574 * @see #getTitle() 575 */ 576 public void setTitle(TextTitle title) { 577 if (this.title != null) { 578 this.title.removeChangeListener(this); 579 } 580 this.title = title; 581 if (title != null) { 582 title.addChangeListener(this); 583 } 584 fireChartChanged(); 585 } 586 587 /** 588 * Sets the chart title and sends a {@link ChartChangeEvent} to all 589 * registered listeners. This is a convenience method that ends up calling 590 * the {@link #setTitle(TextTitle)} method. If there is an existing title, 591 * its text is updated, otherwise a new title using the default font is 592 * added to the chart. If <code>text</code> is <code>null</code> the chart 593 * title is set to <code>null</code>. 594 * 595 * @param text the title text (<code>null</code> permitted). 596 * 597 * @see #getTitle() 598 */ 599 public void setTitle(String text) { 600 if (text != null) { 601 if (this.title == null) { 602 setTitle(new TextTitle(text, JFreeChart.DEFAULT_TITLE_FONT)); 603 } 604 else { 605 this.title.setText(text); 606 } 607 } 608 else { 609 setTitle((TextTitle) null); 610 } 611 } 612 613 /** 614 * Adds a legend to the plot and sends a {@link ChartChangeEvent} to all 615 * registered listeners. 616 * 617 * @param legend the legend (<code>null</code> not permitted). 618 * 619 * @see #removeLegend() 620 */ 621 public void addLegend(LegendTitle legend) { 622 addSubtitle(legend); 623 } 624 625 /** 626 * Returns the legend for the chart, if there is one. Note that a chart 627 * can have more than one legend - this method returns the first. 628 * 629 * @return The legend (possibly <code>null</code>). 630 * 631 * @see #getLegend(int) 632 */ 633 public LegendTitle getLegend() { 634 return getLegend(0); 635 } 636 637 /** 638 * Returns the nth legend for a chart, or <code>null</code>. 639 * 640 * @param index the legend index (zero-based). 641 * 642 * @return The legend (possibly <code>null</code>). 643 * 644 * @see #addLegend(LegendTitle) 645 */ 646 public LegendTitle getLegend(int index) { 647 int seen = 0; 648 Iterator iterator = this.subtitles.iterator(); 649 while (iterator.hasNext()) { 650 Title subtitle = (Title) iterator.next(); 651 if (subtitle instanceof LegendTitle) { 652 if (seen == index) { 653 return (LegendTitle) subtitle; 654 } 655 else { 656 seen++; 657 } 658 } 659 } 660 return null; 661 } 662 663 /** 664 * Removes the first legend in the chart and sends a 665 * {@link ChartChangeEvent} to all registered listeners. 666 * 667 * @see #getLegend() 668 */ 669 public void removeLegend() { 670 removeSubtitle(getLegend()); 671 } 672 673 /** 674 * Returns the list of subtitles for the chart. 675 * 676 * @return The subtitle list (possibly empty, but never <code>null</code>). 677 * 678 * @see #setSubtitles(List) 679 */ 680 public List getSubtitles() { 681 return new ArrayList(this.subtitles); 682 } 683 684 /** 685 * Sets the title list for the chart (completely replaces any existing 686 * titles) and sends a {@link ChartChangeEvent} to all registered 687 * listeners. 688 * 689 * @param subtitles the new list of subtitles (<code>null</code> not 690 * permitted). 691 * 692 * @see #getSubtitles() 693 */ 694 public void setSubtitles(List subtitles) { 695 if (subtitles == null) { 696 throw new NullPointerException("Null 'subtitles' argument."); 697 } 698 setNotify(false); 699 clearSubtitles(); 700 Iterator iterator = subtitles.iterator(); 701 while (iterator.hasNext()) { 702 Title t = (Title) iterator.next(); 703 if (t != null) { 704 addSubtitle(t); 705 } 706 } 707 setNotify(true); // this fires a ChartChangeEvent 708 } 709 710 /** 711 * Returns the number of titles for the chart. 712 * 713 * @return The number of titles for the chart. 714 * 715 * @see #getSubtitles() 716 */ 717 public int getSubtitleCount() { 718 return this.subtitles.size(); 719 } 720 721 /** 722 * Returns a chart subtitle. 723 * 724 * @param index the index of the chart subtitle (zero based). 725 * 726 * @return A chart subtitle. 727 * 728 * @see #addSubtitle(Title) 729 */ 730 public Title getSubtitle(int index) { 731 if ((index < 0) || (index >= getSubtitleCount())) { 732 throw new IllegalArgumentException("Index out of range."); 733 } 734 return (Title) this.subtitles.get(index); 735 } 736 737 /** 738 * Adds a chart subtitle, and notifies registered listeners that the chart 739 * has been modified. 740 * 741 * @param subtitle the subtitle (<code>null</code> not permitted). 742 * 743 * @see #getSubtitle(int) 744 */ 745 public void addSubtitle(Title subtitle) { 746 if (subtitle == null) { 747 throw new IllegalArgumentException("Null 'subtitle' argument."); 748 } 749 this.subtitles.add(subtitle); 750 subtitle.addChangeListener(this); 751 fireChartChanged(); 752 } 753 754 /** 755 * Adds a subtitle at a particular position in the subtitle list, and sends 756 * a {@link ChartChangeEvent} to all registered listeners. 757 * 758 * @param index the index (in the range 0 to {@link #getSubtitleCount()}). 759 * @param subtitle the subtitle to add (<code>null</code> not permitted). 760 * 761 * @since 1.0.6 762 */ 763 public void addSubtitle(int index, Title subtitle) { 764 if (index < 0 || index > getSubtitleCount()) { 765 throw new IllegalArgumentException( 766 "The 'index' argument is out of range."); 767 } 768 if (subtitle == null) { 769 throw new IllegalArgumentException("Null 'subtitle' argument."); 770 } 771 this.subtitles.add(index, subtitle); 772 subtitle.addChangeListener(this); 773 fireChartChanged(); 774 } 775 776 /** 777 * Clears all subtitles from the chart and sends a {@link ChartChangeEvent} 778 * to all registered listeners. 779 * 780 * @see #addSubtitle(Title) 781 */ 782 public void clearSubtitles() { 783 Iterator iterator = this.subtitles.iterator(); 784 while (iterator.hasNext()) { 785 Title t = (Title) iterator.next(); 786 t.removeChangeListener(this); 787 } 788 this.subtitles.clear(); 789 fireChartChanged(); 790 } 791 792 /** 793 * Removes the specified subtitle and sends a {@link ChartChangeEvent} to 794 * all registered listeners. 795 * 796 * @param title the title. 797 * 798 * @see #addSubtitle(Title) 799 */ 800 public void removeSubtitle(Title title) { 801 this.subtitles.remove(title); 802 fireChartChanged(); 803 } 804 805 /** 806 * Returns the plot for the chart. The plot is a class responsible for 807 * coordinating the visual representation of the data, including the axes 808 * (if any). 809 * 810 * @return The plot. 811 */ 812 public Plot getPlot() { 813 return this.plot; 814 } 815 816 /** 817 * Returns the plot cast as a {@link CategoryPlot}. 818 * <p> 819 * NOTE: if the plot is not an instance of {@link CategoryPlot}, then a 820 * <code>ClassCastException</code> is thrown. 821 * 822 * @return The plot. 823 * 824 * @see #getPlot() 825 */ 826 public CategoryPlot getCategoryPlot() { 827 return (CategoryPlot) this.plot; 828 } 829 830 /** 831 * Returns the plot cast as an {@link XYPlot}. 832 * <p> 833 * NOTE: if the plot is not an instance of {@link XYPlot}, then a 834 * <code>ClassCastException</code> is thrown. 835 * 836 * @return The plot. 837 * 838 * @see #getPlot() 839 */ 840 public XYPlot getXYPlot() { 841 return (XYPlot) this.plot; 842 } 843 844 /** 845 * Returns a flag that indicates whether or not anti-aliasing is used when 846 * the chart is drawn. 847 * 848 * @return The flag. 849 * 850 * @see #setAntiAlias(boolean) 851 */ 852 public boolean getAntiAlias() { 853 Object val = this.renderingHints.get(RenderingHints.KEY_ANTIALIASING); 854 return RenderingHints.VALUE_ANTIALIAS_ON.equals(val); 855 } 856 857 /** 858 * Sets a flag that indicates whether or not anti-aliasing is used when the 859 * chart is drawn. 860 * <P> 861 * Anti-aliasing usually improves the appearance of charts, but is slower. 862 * 863 * @param flag the new value of the flag. 864 * 865 * @see #getAntiAlias() 866 */ 867 public void setAntiAlias(boolean flag) { 868 869 Object val = this.renderingHints.get(RenderingHints.KEY_ANTIALIASING); 870 if (val == null) { 871 val = RenderingHints.VALUE_ANTIALIAS_DEFAULT; 872 } 873 if (!flag && RenderingHints.VALUE_ANTIALIAS_OFF.equals(val) 874 || flag && RenderingHints.VALUE_ANTIALIAS_ON.equals(val)) { 875 // no change, do nothing 876 return; 877 } 878 if (flag) { 879 this.renderingHints.put(RenderingHints.KEY_ANTIALIASING, 880 RenderingHints.VALUE_ANTIALIAS_ON); 881 } 882 else { 883 this.renderingHints.put(RenderingHints.KEY_ANTIALIASING, 884 RenderingHints.VALUE_ANTIALIAS_OFF); 885 } 886 fireChartChanged(); 887 888 } 889 890 /** 891 * Returns the current value stored in the rendering hints table for 892 * {@link RenderingHints#KEY_TEXT_ANTIALIASING}. 893 * 894 * @return The hint value (possibly <code>null</code>). 895 * 896 * @since 1.0.5 897 * 898 * @see #setTextAntiAlias(Object) 899 */ 900 public Object getTextAntiAlias() { 901 return this.renderingHints.get(RenderingHints.KEY_TEXT_ANTIALIASING); 902 } 903 904 /** 905 * Sets the value in the rendering hints table for 906 * {@link RenderingHints#KEY_TEXT_ANTIALIASING} to either 907 * {@link RenderingHints#VALUE_TEXT_ANTIALIAS_ON} or 908 * {@link RenderingHints#VALUE_TEXT_ANTIALIAS_OFF}, then sends a 909 * {@link ChartChangeEvent} to all registered listeners. 910 * 911 * @param flag the new value of the flag. 912 * 913 * @since 1.0.5 914 * 915 * @see #getTextAntiAlias() 916 * @see #setTextAntiAlias(Object) 917 */ 918 public void setTextAntiAlias(boolean flag) { 919 if (flag) { 920 setTextAntiAlias(RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 921 } 922 else { 923 setTextAntiAlias(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 924 } 925 } 926 927 /** 928 * Sets the value in the rendering hints table for 929 * {@link RenderingHints#KEY_TEXT_ANTIALIASING} and sends a 930 * {@link ChartChangeEvent} to all registered listeners. 931 * 932 * @param val the new value (<code>null</code> permitted). 933 * 934 * @since 1.0.5 935 * 936 * @see #getTextAntiAlias() 937 * @see #setTextAntiAlias(boolean) 938 */ 939 public void setTextAntiAlias(Object val) { 940 this.renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, val); 941 notifyListeners(new ChartChangeEvent(this)); 942 } 943 944 /** 945 * Returns the paint used for the chart background. 946 * 947 * @return The paint (possibly <code>null</code>). 948 * 949 * @see #setBackgroundPaint(Paint) 950 */ 951 public Paint getBackgroundPaint() { 952 return this.backgroundPaint; 953 } 954 955 /** 956 * Sets the paint used to fill the chart background and sends a 957 * {@link ChartChangeEvent} to all registered listeners. 958 * 959 * @param paint the paint (<code>null</code> permitted). 960 * 961 * @see #getBackgroundPaint() 962 */ 963 public void setBackgroundPaint(Paint paint) { 964 965 if (this.backgroundPaint != null) { 966 if (!this.backgroundPaint.equals(paint)) { 967 this.backgroundPaint = paint; 968 fireChartChanged(); 969 } 970 } 971 else { 972 if (paint != null) { 973 this.backgroundPaint = paint; 974 fireChartChanged(); 975 } 976 } 977 978 } 979 980 /** 981 * Returns the background image for the chart, or <code>null</code> if 982 * there is no image. 983 * 984 * @return The image (possibly <code>null</code>). 985 * 986 * @see #setBackgroundImage(Image) 987 */ 988 public Image getBackgroundImage() { 989 return this.backgroundImage; 990 } 991 992 /** 993 * Sets the background image for the chart and sends a 994 * {@link ChartChangeEvent} to all registered listeners. 995 * 996 * @param image the image (<code>null</code> permitted). 997 * 998 * @see #getBackgroundImage() 999 */ 1000 public void setBackgroundImage(Image image) { 1001 1002 if (this.backgroundImage != null) { 1003 if (!this.backgroundImage.equals(image)) { 1004 this.backgroundImage = image; 1005 fireChartChanged(); 1006 } 1007 } 1008 else { 1009 if (image != null) { 1010 this.backgroundImage = image; 1011 fireChartChanged(); 1012 } 1013 } 1014 1015 } 1016 1017 /** 1018 * Returns the background image alignment. Alignment constants are defined 1019 * in the <code>org.jfree.ui.Align</code> class in the JCommon class 1020 * library. 1021 * 1022 * @return The alignment. 1023 * 1024 * @see #setBackgroundImageAlignment(int) 1025 */ 1026 public int getBackgroundImageAlignment() { 1027 return this.backgroundImageAlignment; 1028 } 1029 1030 /** 1031 * Sets the background alignment. Alignment options are defined by the 1032 * {@link org.jfree.ui.Align} class. 1033 * 1034 * @param alignment the alignment. 1035 * 1036 * @see #getBackgroundImageAlignment() 1037 */ 1038 public void setBackgroundImageAlignment(int alignment) { 1039 if (this.backgroundImageAlignment != alignment) { 1040 this.backgroundImageAlignment = alignment; 1041 fireChartChanged(); 1042 } 1043 } 1044 1045 /** 1046 * Returns the alpha-transparency for the chart's background image. 1047 * 1048 * @return The alpha-transparency. 1049 * 1050 * @see #setBackgroundImageAlpha(float) 1051 */ 1052 public float getBackgroundImageAlpha() { 1053 return this.backgroundImageAlpha; 1054 } 1055 1056 /** 1057 * Sets the alpha-transparency for the chart's background image. 1058 * Registered listeners are notified that the chart has been changed. 1059 * 1060 * @param alpha the alpha value. 1061 * 1062 * @see #getBackgroundImageAlpha() 1063 */ 1064 public void setBackgroundImageAlpha(float alpha) { 1065 1066 if (this.backgroundImageAlpha != alpha) { 1067 this.backgroundImageAlpha = alpha; 1068 fireChartChanged(); 1069 } 1070 1071 } 1072 1073 /** 1074 * Returns a flag that controls whether or not change events are sent to 1075 * registered listeners. 1076 * 1077 * @return A boolean. 1078 * 1079 * @see #setNotify(boolean) 1080 */ 1081 public boolean isNotify() { 1082 return this.notify; 1083 } 1084 1085 /** 1086 * Sets a flag that controls whether or not listeners receive 1087 * {@link ChartChangeEvent} notifications. 1088 * 1089 * @param notify a boolean. 1090 * 1091 * @see #isNotify() 1092 */ 1093 public void setNotify(boolean notify) { 1094 this.notify = notify; 1095 // if the flag is being set to true, there may be queued up changes... 1096 if (notify) { 1097 notifyListeners(new ChartChangeEvent(this)); 1098 } 1099 } 1100 1101 /** 1102 * Draws the chart on a Java 2D graphics device (such as the screen or a 1103 * printer). 1104 * <P> 1105 * This method is the focus of the entire JFreeChart library. 1106 * 1107 * @param g2 the graphics device. 1108 * @param area the area within which the chart should be drawn. 1109 */ 1110 public void draw(Graphics2D g2, Rectangle2D area) { 1111 draw(g2, area, null, null); 1112 } 1113 1114 /** 1115 * Draws the chart on a Java 2D graphics device (such as the screen or a 1116 * printer). This method is the focus of the entire JFreeChart library. 1117 * 1118 * @param g2 the graphics device. 1119 * @param area the area within which the chart should be drawn. 1120 * @param info records info about the drawing (null means collect no info). 1121 */ 1122 public void draw(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info) { 1123 draw(g2, area, null, info); 1124 } 1125 1126 /** 1127 * Draws the chart on a Java 2D graphics device (such as the screen or a 1128 * printer). 1129 * <P> 1130 * This method is the focus of the entire JFreeChart library. 1131 * 1132 * @param g2 the graphics device. 1133 * @param chartArea the area within which the chart should be drawn. 1134 * @param anchor the anchor point (in Java2D space) for the chart 1135 * (<code>null</code> permitted). 1136 * @param info records info about the drawing (null means collect no info). 1137 */ 1138 public void draw(Graphics2D g2, 1139 Rectangle2D chartArea, Point2D anchor, 1140 ChartRenderingInfo info) { 1141 1142 notifyListeners(new ChartProgressEvent(this, this, 1143 ChartProgressEvent.DRAWING_STARTED, 0)); 1144 1145 // record the chart area, if info is requested... 1146 if (info != null) { 1147 info.clear(); 1148 info.setChartArea(chartArea); 1149 } 1150 1151 // ensure no drawing occurs outside chart area... 1152 Shape savedClip = g2.getClip(); 1153 g2.clip(chartArea); 1154 1155 g2.addRenderingHints(this.renderingHints); 1156 1157 // draw the chart background... 1158 if (this.backgroundPaint != null) { 1159 g2.setPaint(this.backgroundPaint); 1160 g2.fill(chartArea); 1161 } 1162 1163 if (this.backgroundImage != null) { 1164 Composite originalComposite = g2.getComposite(); 1165 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1166 this.backgroundImageAlpha)); 1167 Rectangle2D dest = new Rectangle2D.Double(0.0, 0.0, 1168 this.backgroundImage.getWidth(null), 1169 this.backgroundImage.getHeight(null)); 1170 Align.align(dest, chartArea, this.backgroundImageAlignment); 1171 g2.drawImage(this.backgroundImage, (int) dest.getX(), 1172 (int) dest.getY(), (int) dest.getWidth(), 1173 (int) dest.getHeight(), null); 1174 g2.setComposite(originalComposite); 1175 } 1176 1177 if (isBorderVisible()) { 1178 Paint paint = getBorderPaint(); 1179 Stroke stroke = getBorderStroke(); 1180 if (paint != null && stroke != null) { 1181 Rectangle2D borderArea = new Rectangle2D.Double( 1182 chartArea.getX(), chartArea.getY(), 1183 chartArea.getWidth() - 1.0, chartArea.getHeight() 1184 - 1.0); 1185 g2.setPaint(paint); 1186 g2.setStroke(stroke); 1187 g2.draw(borderArea); 1188 } 1189 } 1190 1191 // draw the title and subtitles... 1192 Rectangle2D nonTitleArea = new Rectangle2D.Double(); 1193 nonTitleArea.setRect(chartArea); 1194 this.padding.trim(nonTitleArea); 1195 1196 EntityCollection entities = null; 1197 if (info != null) { 1198 entities = info.getEntityCollection(); 1199 } 1200 if (this.title != null) { 1201 EntityCollection e = drawTitle(this.title, g2, nonTitleArea, 1202 (entities != null)); 1203 if (e != null) { 1204 entities.addAll(e); 1205 } 1206 } 1207 1208 Iterator iterator = this.subtitles.iterator(); 1209 while (iterator.hasNext()) { 1210 Title currentTitle = (Title) iterator.next(); 1211 if (currentTitle.isVisible()) { 1212 EntityCollection e = drawTitle(currentTitle, g2, nonTitleArea, 1213 (entities != null)); 1214 if (e != null) { 1215 entities.addAll(e); 1216 } 1217 } 1218 } 1219 1220 Rectangle2D plotArea = nonTitleArea; 1221 1222 // draw the plot (axes and data visualisation) 1223 PlotRenderingInfo plotInfo = null; 1224 if (info != null) { 1225 plotInfo = info.getPlotInfo(); 1226 } 1227 this.plot.draw(g2, plotArea, anchor, null, plotInfo); 1228 1229 g2.setClip(savedClip); 1230 1231 notifyListeners(new ChartProgressEvent(this, this, 1232 ChartProgressEvent.DRAWING_FINISHED, 100)); 1233 } 1234 1235 /** 1236 * Creates a rectangle that is aligned to the frame. 1237 * 1238 * @param dimensions the dimensions for the rectangle. 1239 * @param frame the frame to align to. 1240 * @param hAlign the horizontal alignment. 1241 * @param vAlign the vertical alignment. 1242 * 1243 * @return A rectangle. 1244 */ 1245 private Rectangle2D createAlignedRectangle2D(Size2D dimensions, 1246 Rectangle2D frame, HorizontalAlignment hAlign, 1247 VerticalAlignment vAlign) { 1248 double x = Double.NaN; 1249 double y = Double.NaN; 1250 if (hAlign == HorizontalAlignment.LEFT) { 1251 x = frame.getX(); 1252 } 1253 else if (hAlign == HorizontalAlignment.CENTER) { 1254 x = frame.getCenterX() - (dimensions.width / 2.0); 1255 } 1256 else if (hAlign == HorizontalAlignment.RIGHT) { 1257 x = frame.getMaxX() - dimensions.width; 1258 } 1259 if (vAlign == VerticalAlignment.TOP) { 1260 y = frame.getY(); 1261 } 1262 else if (vAlign == VerticalAlignment.CENTER) { 1263 y = frame.getCenterY() - (dimensions.height / 2.0); 1264 } 1265 else if (vAlign == VerticalAlignment.BOTTOM) { 1266 y = frame.getMaxY() - dimensions.height; 1267 } 1268 1269 return new Rectangle2D.Double(x, y, dimensions.width, 1270 dimensions.height); 1271 } 1272 1273 /** 1274 * Draws a title. The title should be drawn at the top, bottom, left or 1275 * right of the specified area, and the area should be updated to reflect 1276 * the amount of space used by the title. 1277 * 1278 * @param t the title (<code>null</code> not permitted). 1279 * @param g2 the graphics device (<code>null</code> not permitted). 1280 * @param area the chart area, excluding any existing titles 1281 * (<code>null</code> not permitted). 1282 * @param entities a flag that controls whether or not an entity 1283 * collection is returned for the title. 1284 * 1285 * @return An entity collection for the title (possibly <code>null</code>). 1286 */ 1287 protected EntityCollection drawTitle(Title t, Graphics2D g2, 1288 Rectangle2D area, boolean entities) { 1289 1290 if (t == null) { 1291 throw new IllegalArgumentException("Null 't' argument."); 1292 } 1293 if (area == null) { 1294 throw new IllegalArgumentException("Null 'area' argument."); 1295 } 1296 Rectangle2D titleArea = new Rectangle2D.Double(); 1297 RectangleEdge position = t.getPosition(); 1298 double ww = area.getWidth(); 1299 if (ww <= 0.0) { 1300 return null; 1301 } 1302 double hh = area.getHeight(); 1303 if (hh <= 0.0) { 1304 return null; 1305 } 1306 RectangleConstraint constraint = new RectangleConstraint(ww, 1307 new Range(0.0, ww), LengthConstraintType.RANGE, hh, 1308 new Range(0.0, hh), LengthConstraintType.RANGE); 1309 Object retValue = null; 1310 BlockParams p = new BlockParams(); 1311 p.setGenerateEntities(entities); 1312 if (position == RectangleEdge.TOP) { 1313 Size2D size = t.arrange(g2, constraint); 1314 titleArea = createAlignedRectangle2D(size, area, 1315 t.getHorizontalAlignment(), VerticalAlignment.TOP); 1316 retValue = t.draw(g2, titleArea, p); 1317 area.setRect(area.getX(), Math.min(area.getY() + size.height, 1318 area.getMaxY()), area.getWidth(), Math.max(area.getHeight() 1319 - size.height, 0)); 1320 } 1321 else if (position == RectangleEdge.BOTTOM) { 1322 Size2D size = t.arrange(g2, constraint); 1323 titleArea = createAlignedRectangle2D(size, area, 1324 t.getHorizontalAlignment(), VerticalAlignment.BOTTOM); 1325 retValue = t.draw(g2, titleArea, p); 1326 area.setRect(area.getX(), area.getY(), area.getWidth(), 1327 area.getHeight() - size.height); 1328 } 1329 else if (position == RectangleEdge.RIGHT) { 1330 Size2D size = t.arrange(g2, constraint); 1331 titleArea = createAlignedRectangle2D(size, area, 1332 HorizontalAlignment.RIGHT, t.getVerticalAlignment()); 1333 retValue = t.draw(g2, titleArea, p); 1334 area.setRect(area.getX(), area.getY(), area.getWidth() 1335 - size.width, area.getHeight()); 1336 } 1337 1338 else if (position == RectangleEdge.LEFT) { 1339 Size2D size = t.arrange(g2, constraint); 1340 titleArea = createAlignedRectangle2D(size, area, 1341 HorizontalAlignment.LEFT, t.getVerticalAlignment()); 1342 retValue = t.draw(g2, titleArea, p); 1343 area.setRect(area.getX() + size.width, area.getY(), area.getWidth() 1344 - size.width, area.getHeight()); 1345 } 1346 else { 1347 throw new RuntimeException("Unrecognised title position."); 1348 } 1349 EntityCollection result = null; 1350 if (retValue instanceof EntityBlockResult) { 1351 EntityBlockResult ebr = (EntityBlockResult) retValue; 1352 result = ebr.getEntityCollection(); 1353 } 1354 return result; 1355 } 1356 1357 /** 1358 * Creates and returns a buffered image into which the chart has been drawn. 1359 * 1360 * @param width the width. 1361 * @param height the height. 1362 * 1363 * @return A buffered image. 1364 */ 1365 public BufferedImage createBufferedImage(int width, int height) { 1366 return createBufferedImage(width, height, null); 1367 } 1368 1369 /** 1370 * Creates and returns a buffered image into which the chart has been drawn. 1371 * 1372 * @param width the width. 1373 * @param height the height. 1374 * @param info carries back chart state information (<code>null</code> 1375 * permitted). 1376 * 1377 * @return A buffered image. 1378 */ 1379 public BufferedImage createBufferedImage(int width, int height, 1380 ChartRenderingInfo info) { 1381 return createBufferedImage(width, height, BufferedImage.TYPE_INT_ARGB, 1382 info); 1383 } 1384 1385 /** 1386 * Creates and returns a buffered image into which the chart has been drawn. 1387 * 1388 * @param width the width. 1389 * @param height the height. 1390 * @param imageType the image type. 1391 * @param info carries back chart state information (<code>null</code> 1392 * permitted). 1393 * 1394 * @return A buffered image. 1395 */ 1396 public BufferedImage createBufferedImage(int width, int height, 1397 int imageType, 1398 ChartRenderingInfo info) { 1399 BufferedImage image = new BufferedImage(width, height, imageType); 1400 Graphics2D g2 = image.createGraphics(); 1401 draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info); 1402 g2.dispose(); 1403 return image; 1404 } 1405 1406 /** 1407 * Creates and returns a buffered image into which the chart has been drawn. 1408 * 1409 * @param imageWidth the image width. 1410 * @param imageHeight the image height. 1411 * @param drawWidth the width for drawing the chart (will be scaled to 1412 * fit image). 1413 * @param drawHeight the height for drawing the chart (will be scaled to 1414 * fit image). 1415 * @param info optional object for collection chart dimension and entity 1416 * information. 1417 * 1418 * @return A buffered image. 1419 */ 1420 public BufferedImage createBufferedImage(int imageWidth, 1421 int imageHeight, 1422 double drawWidth, 1423 double drawHeight, 1424 ChartRenderingInfo info) { 1425 1426 BufferedImage image = new BufferedImage(imageWidth, imageHeight, 1427 BufferedImage.TYPE_INT_ARGB); 1428 Graphics2D g2 = image.createGraphics(); 1429 double scaleX = imageWidth / drawWidth; 1430 double scaleY = imageHeight / drawHeight; 1431 AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY); 1432 g2.transform(st); 1433 draw(g2, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), null, 1434 info); 1435 g2.dispose(); 1436 return image; 1437 1438 } 1439 1440 /** 1441 * Handles a 'click' on the chart. JFreeChart is not a UI component, so 1442 * some other object (for example, {@link ChartPanel}) needs to capture 1443 * the click event and pass it onto the JFreeChart object. 1444 * If you are not using JFreeChart in a client application, then this 1445 * method is not required. 1446 * 1447 * @param x x-coordinate of the click (in Java2D space). 1448 * @param y y-coordinate of the click (in Java2D space). 1449 * @param info contains chart dimension and entity information 1450 * (<code>null</code> not permitted). 1451 */ 1452 public void handleClick(int x, int y, ChartRenderingInfo info) { 1453 1454 // pass the click on to the plot... 1455 // rely on the plot to post a plot change event and redraw the chart... 1456 this.plot.handleClick(x, y, info.getPlotInfo()); 1457 1458 } 1459 1460 /** 1461 * Registers an object for notification of changes to the chart. 1462 * 1463 * @param listener the listener (<code>null</code> not permitted). 1464 * 1465 * @see #removeChangeListener(ChartChangeListener) 1466 */ 1467 public void addChangeListener(ChartChangeListener listener) { 1468 if (listener == null) { 1469 throw new IllegalArgumentException("Null 'listener' argument."); 1470 } 1471 this.changeListeners.add(ChartChangeListener.class, listener); 1472 } 1473 1474 /** 1475 * Deregisters an object for notification of changes to the chart. 1476 * 1477 * @param listener the listener (<code>null</code> not permitted) 1478 * 1479 * @see #addChangeListener(ChartChangeListener) 1480 */ 1481 public void removeChangeListener(ChartChangeListener listener) { 1482 if (listener == null) { 1483 throw new IllegalArgumentException("Null 'listener' argument."); 1484 } 1485 this.changeListeners.remove(ChartChangeListener.class, listener); 1486 } 1487 1488 /** 1489 * Sends a default {@link ChartChangeEvent} to all registered listeners. 1490 * <P> 1491 * This method is for convenience only. 1492 */ 1493 public void fireChartChanged() { 1494 ChartChangeEvent event = new ChartChangeEvent(this); 1495 notifyListeners(event); 1496 } 1497 1498 /** 1499 * Sends a {@link ChartChangeEvent} to all registered listeners. 1500 * 1501 * @param event information about the event that triggered the 1502 * notification. 1503 */ 1504 protected void notifyListeners(ChartChangeEvent event) { 1505 if (this.notify) { 1506 Object[] listeners = this.changeListeners.getListenerList(); 1507 for (int i = listeners.length - 2; i >= 0; i -= 2) { 1508 if (listeners[i] == ChartChangeListener.class) { 1509 ((ChartChangeListener) listeners[i + 1]).chartChanged( 1510 event); 1511 } 1512 } 1513 } 1514 } 1515 1516 /** 1517 * Registers an object for notification of progress events relating to the 1518 * chart. 1519 * 1520 * @param listener the object being registered. 1521 * 1522 * @see #removeProgressListener(ChartProgressListener) 1523 */ 1524 public void addProgressListener(ChartProgressListener listener) { 1525 this.progressListeners.add(ChartProgressListener.class, listener); 1526 } 1527 1528 /** 1529 * Deregisters an object for notification of changes to the chart. 1530 * 1531 * @param listener the object being deregistered. 1532 * 1533 * @see #addProgressListener(ChartProgressListener) 1534 */ 1535 public void removeProgressListener(ChartProgressListener listener) { 1536 this.progressListeners.remove(ChartProgressListener.class, listener); 1537 } 1538 1539 /** 1540 * Sends a {@link ChartProgressEvent} to all registered listeners. 1541 * 1542 * @param event information about the event that triggered the 1543 * notification. 1544 */ 1545 protected void notifyListeners(ChartProgressEvent event) { 1546 1547 Object[] listeners = this.progressListeners.getListenerList(); 1548 for (int i = listeners.length - 2; i >= 0; i -= 2) { 1549 if (listeners[i] == ChartProgressListener.class) { 1550 ((ChartProgressListener) listeners[i + 1]).chartProgress(event); 1551 } 1552 } 1553 1554 } 1555 1556 /** 1557 * Receives notification that a chart title has changed, and passes this 1558 * on to registered listeners. 1559 * 1560 * @param event information about the chart title change. 1561 */ 1562 public void titleChanged(TitleChangeEvent event) { 1563 event.setChart(this); 1564 notifyListeners(event); 1565 } 1566 1567 /** 1568 * Receives notification that the plot has changed, and passes this on to 1569 * registered listeners. 1570 * 1571 * @param event information about the plot change. 1572 */ 1573 public void plotChanged(PlotChangeEvent event) { 1574 event.setChart(this); 1575 notifyListeners(event); 1576 } 1577 1578 /** 1579 * Tests this chart for equality with another object. 1580 * 1581 * @param obj the object (<code>null</code> permitted). 1582 * 1583 * @return A boolean. 1584 */ 1585 public boolean equals(Object obj) { 1586 if (obj == this) { 1587 return true; 1588 } 1589 if (!(obj instanceof JFreeChart)) { 1590 return false; 1591 } 1592 JFreeChart that = (JFreeChart) obj; 1593 if (!this.renderingHints.equals(that.renderingHints)) { 1594 return false; 1595 } 1596 if (this.borderVisible != that.borderVisible) { 1597 return false; 1598 } 1599 if (!ObjectUtilities.equal(this.borderStroke, that.borderStroke)) { 1600 return false; 1601 } 1602 if (!PaintUtilities.equal(this.borderPaint, that.borderPaint)) { 1603 return false; 1604 } 1605 if (!this.padding.equals(that.padding)) { 1606 return false; 1607 } 1608 if (!ObjectUtilities.equal(this.title, that.title)) { 1609 return false; 1610 } 1611 if (!ObjectUtilities.equal(this.subtitles, that.subtitles)) { 1612 return false; 1613 } 1614 if (!ObjectUtilities.equal(this.plot, that.plot)) { 1615 return false; 1616 } 1617 if (!PaintUtilities.equal( 1618 this.backgroundPaint, that.backgroundPaint 1619 )) { 1620 return false; 1621 } 1622 if (!ObjectUtilities.equal(this.backgroundImage, 1623 that.backgroundImage)) { 1624 return false; 1625 } 1626 if (this.backgroundImageAlignment != that.backgroundImageAlignment) { 1627 return false; 1628 } 1629 if (this.backgroundImageAlpha != that.backgroundImageAlpha) { 1630 return false; 1631 } 1632 if (this.notify != that.notify) { 1633 return false; 1634 } 1635 return true; 1636 } 1637 1638 /** 1639 * Provides serialization support. 1640 * 1641 * @param stream the output stream. 1642 * 1643 * @throws IOException if there is an I/O error. 1644 */ 1645 private void writeObject(ObjectOutputStream stream) throws IOException { 1646 stream.defaultWriteObject(); 1647 SerialUtilities.writeStroke(this.borderStroke, stream); 1648 SerialUtilities.writePaint(this.borderPaint, stream); 1649 SerialUtilities.writePaint(this.backgroundPaint, stream); 1650 } 1651 1652 /** 1653 * Provides serialization support. 1654 * 1655 * @param stream the input stream. 1656 * 1657 * @throws IOException if there is an I/O error. 1658 * @throws ClassNotFoundException if there is a classpath problem. 1659 */ 1660 private void readObject(ObjectInputStream stream) 1661 throws IOException, ClassNotFoundException { 1662 stream.defaultReadObject(); 1663 this.borderStroke = SerialUtilities.readStroke(stream); 1664 this.borderPaint = SerialUtilities.readPaint(stream); 1665 this.backgroundPaint = SerialUtilities.readPaint(stream); 1666 this.progressListeners = new EventListenerList(); 1667 this.changeListeners = new EventListenerList(); 1668 this.renderingHints = new RenderingHints( 1669 RenderingHints.KEY_ANTIALIASING, 1670 RenderingHints.VALUE_ANTIALIAS_ON); 1671 1672 // register as a listener with sub-components... 1673 if (this.title != null) { 1674 this.title.addChangeListener(this); 1675 } 1676 1677 for (int i = 0; i < getSubtitleCount(); i++) { 1678 getSubtitle(i).addChangeListener(this); 1679 } 1680 this.plot.addChangeListener(this); 1681 } 1682 1683 /** 1684 * Prints information about JFreeChart to standard output. 1685 * 1686 * @param args no arguments are honored. 1687 */ 1688 public static void main(String[] args) { 1689 System.out.println(JFreeChart.INFO.toString()); 1690 } 1691 1692 /** 1693 * Clones the object, and takes care of listeners. 1694 * Note: caller shall register its own listeners on cloned graph. 1695 * 1696 * @return A clone. 1697 * 1698 * @throws CloneNotSupportedException if the chart is not cloneable. 1699 */ 1700 public Object clone() throws CloneNotSupportedException { 1701 JFreeChart chart = (JFreeChart) super.clone(); 1702 1703 chart.renderingHints = (RenderingHints) this.renderingHints.clone(); 1704 // private boolean borderVisible; 1705 // private transient Stroke borderStroke; 1706 // private transient Paint borderPaint; 1707 1708 if (this.title != null) { 1709 chart.title = (TextTitle) this.title.clone(); 1710 chart.title.addChangeListener(chart); 1711 } 1712 1713 chart.subtitles = new ArrayList(); 1714 for (int i = 0; i < getSubtitleCount(); i++) { 1715 Title subtitle = (Title) getSubtitle(i).clone(); 1716 chart.subtitles.add(subtitle); 1717 subtitle.addChangeListener(chart); 1718 } 1719 1720 if (this.plot != null) { 1721 chart.plot = (Plot) this.plot.clone(); 1722 chart.plot.addChangeListener(chart); 1723 } 1724 1725 chart.progressListeners = new EventListenerList(); 1726 chart.changeListeners = new EventListenerList(); 1727 return chart; 1728 } 1729 1730 } 1731 1732 /** 1733 * Information about the JFreeChart project. One instance of this class is 1734 * assigned to <code>JFreeChart.INFO<code>. 1735 */ 1736 class JFreeChartInfo extends ProjectInfo { 1737 1738 /** 1739 * Default constructor. 1740 */ 1741 public JFreeChartInfo() { 1742 1743 // get a locale-specific resource bundle... 1744 String baseResourceClass 1745 = "org.jfree.chart.resources.JFreeChartResources"; 1746 ResourceBundle resources = ResourceBundle.getBundle(baseResourceClass); 1747 1748 setName(resources.getString("project.name")); 1749 setVersion(resources.getString("project.version")); 1750 setInfo(resources.getString("project.info")); 1751 setCopyright(resources.getString("project.copyright")); 1752 setLogo(null); // load only when required 1753 setLicenceName("LGPL"); 1754 setLicenceText(Licences.getInstance().getLGPL()); 1755 1756 setContributors(Arrays.asList( 1757 new Contributor[]{ 1758 new Contributor("Eric Alexander", "-"), 1759 new Contributor("Richard Atkinson", 1760 "richard_c_atkinson@ntlworld.com"), 1761 new Contributor("David Basten", "-"), 1762 new Contributor("David Berry", "-"), 1763 new Contributor("Chris Boek", "-"), 1764 new Contributor("Zoheb Borbora", "-"), 1765 new Contributor("Anthony Boulestreau", "-"), 1766 new Contributor("Jeremy Bowman", "-"), 1767 new Contributor("Nicolas Brodu", "-"), 1768 new Contributor("Jody Brownell", "-"), 1769 new Contributor("David Browning", "-"), 1770 new Contributor("Soren Caspersen", "-"), 1771 new Contributor("Chuanhao Chiu", "-"), 1772 new Contributor("Brian Cole", "-"), 1773 new Contributor("Pascal Collet", "-"), 1774 new Contributor("Martin Cordova", "-"), 1775 new Contributor("Paolo Cova", "-"), 1776 new Contributor("Greg Darke", "-"), 1777 new Contributor("Mike Duffy", "-"), 1778 new Contributor("Don Elliott", "-"), 1779 new Contributor("David Forslund", "-"), 1780 new Contributor("Jonathan Gabbai", "-"), 1781 new Contributor("David Gilbert", 1782 "david.gilbert@object-refinery.com"), 1783 new Contributor("Serge V. Grachov", "-"), 1784 new Contributor("Daniel Gredler", "-"), 1785 new Contributor("Hans-Jurgen Greiner", "-"), 1786 new Contributor("Joao Guilherme Del Valle", "-"), 1787 new Contributor("Aiman Han", "-"), 1788 new Contributor("Cameron Hayne", "-"), 1789 new Contributor("Martin Hoeller", "-"), 1790 new Contributor("Jon Iles", "-"), 1791 new Contributor("Wolfgang Irler", "-"), 1792 new Contributor("Sergei Ivanov", "-"), 1793 new Contributor("Adriaan Joubert", "-"), 1794 new Contributor("Darren Jung", "-"), 1795 new Contributor("Xun Kang", "-"), 1796 new Contributor("Bill Kelemen", "-"), 1797 new Contributor("Norbert Kiesel", "-"), 1798 new Contributor("Gideon Krause", "-"), 1799 new Contributor("Pierre-Marie Le Biot", "-"), 1800 new Contributor("Arnaud Lelievre", "-"), 1801 new Contributor("Wolfgang Lenhard", "-"), 1802 new Contributor("David Li", "-"), 1803 new Contributor("Yan Liu", "-"), 1804 new Contributor("Tin Luu", "-"), 1805 new Contributor("Craig MacFarlane", "-"), 1806 new Contributor("Achilleus Mantzios", "-"), 1807 new Contributor("Thomas Meier", "-"), 1808 new Contributor("Jim Moore", "-"), 1809 new Contributor("Jonathan Nash", "-"), 1810 new Contributor("Barak Naveh", "-"), 1811 new Contributor("David M. O'Donnell", "-"), 1812 new Contributor("Krzysztof Paz", "-"), 1813 new Contributor("Eric Penfold", "-"), 1814 new Contributor("Tomer Peretz", "-"), 1815 new Contributor("Diego Pierangeli", "-"), 1816 new Contributor("Xavier Poinsard", "-"), 1817 new Contributor("Andrzej Porebski", "-"), 1818 new Contributor("Viktor Rajewski", "-"), 1819 new Contributor("Eduardo Ramalho", "-"), 1820 new Contributor("Michael Rauch", "-"), 1821 new Contributor("Cameron Riley", "-"), 1822 new Contributor("Klaus Rheinwald", "-"), 1823 new Contributor("Dan Rivett", "d.rivett@ukonline.co.uk"), 1824 new Contributor("Scott Sams", "-"), 1825 new Contributor("Michel Santos", "-"), 1826 new Contributor("Thierry Saura", "-"), 1827 new Contributor("Andreas Schneider", "-"), 1828 new Contributor("Jean-Luc SCHWAB", "-"), 1829 new Contributor("Bryan Scott", "-"), 1830 new Contributor("Tobias Selb", "-"), 1831 new Contributor("Mofeed Shahin", "-"), 1832 new Contributor("Michael Siemer", "-"), 1833 new Contributor("Pady Srinivasan", "-"), 1834 new Contributor("Greg Steckman", "-"), 1835 new Contributor("Gerald Struck", "-"), 1836 new Contributor("Roger Studner", "-"), 1837 new Contributor("Irv Thomae", "-"), 1838 new Contributor("Eric Thomas", "-"), 1839 new Contributor("Rich Unger", "-"), 1840 new Contributor("Daniel van Enckevort", "-"), 1841 new Contributor("Laurence Vanhelsuwe", "-"), 1842 new Contributor("Sylvain Vieujot", "-"), 1843 new Contributor("Ulrich Voigt", "-"), 1844 new Contributor("Jelai Wang", "-"), 1845 new Contributor("Mark Watson", "www.markwatson.com"), 1846 new Contributor("Alex Weber", "-"), 1847 new Contributor("Matthew Wright", "-"), 1848 new Contributor("Benoit Xhenseval", "-"), 1849 new Contributor("Christian W. Zuckschwerdt", 1850 "Christian.Zuckschwerdt@Informatik.Uni-Oldenburg.de"), 1851 new Contributor("Hari", "-"), 1852 new Contributor("Sam (oldman)", "-"), 1853 } 1854 )); 1855 1856 addLibrary(JCommon.INFO); 1857 1858 } 1859 1860 /** 1861 * Returns the JFreeChart logo (a picture of a gorilla). 1862 * 1863 * @return The JFreeChart logo. 1864 */ 1865 public Image getLogo() { 1866 1867 Image logo = super.getLogo(); 1868 if (logo == null) { 1869 URL imageURL = this.getClass().getClassLoader().getResource( 1870 "org/jfree/chart/gorilla.jpg"); 1871 if (imageURL != null) { 1872 ImageIcon temp = new ImageIcon(imageURL); 1873 // use ImageIcon because it waits for the image to load... 1874 logo = temp.getImage(); 1875 setLogo(logo); 1876 } 1877 } 1878 return logo; 1879 1880 } 1881 1882 }