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