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 * AbstractCategoryItemRenderer.java
029 * ---------------------------------
030 * (C) Copyright 2002-2008, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Richard Atkinson;
034 *
035 * Changes:
036 * --------
037 * 29-May-2002 : Version 1 (DG);
038 * 06-Jun-2002 : Added accessor methods for the tool tip generator (DG);
039 * 11-Jun-2002 : Made constructors protected (DG);
040 * 26-Jun-2002 : Added axis to initialise method (DG);
041 * 05-Aug-2002 : Added urlGenerator member variable plus accessors (RA);
042 * 22-Aug-2002 : Added categoriesPaint attribute, based on code submitted by
043 * Janet Banks. This can be used when there is only one series,
044 * and you want each category item to have a different color (DG);
045 * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
046 * 29-Oct-2002 : Fixed bug where background image for plot was not being
047 * drawn (DG);
048 * 05-Nov-2002 : Replaced references to CategoryDataset with TableDataset (DG);
049 * 26-Nov 2002 : Replaced the isStacked() method with getRangeType() (DG);
050 * 09-Jan-2003 : Renamed grid-line methods (DG);
051 * 17-Jan-2003 : Moved plot classes into separate package (DG);
052 * 25-Mar-2003 : Implemented Serializable (DG);
053 * 12-May-2003 : Modified to take into account the plot orientation (DG);
054 * 12-Aug-2003 : Very minor javadoc corrections (DB)
055 * 13-Aug-2003 : Implemented Cloneable (DG);
056 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
057 * 05-Nov-2003 : Fixed marker rendering bug (833623) (DG);
058 * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
059 * 11-Feb-2004 : Modified labelling for markers (DG);
060 * 12-Feb-2004 : Updated clone() method (DG);
061 * 15-Apr-2004 : Created a new CategoryToolTipGenerator interface (DG);
062 * 05-May-2004 : Fixed bug (948310) where interval markers extend outside axis
063 * range (DG);
064 * 14-Jun-2004 : Fixed bug in drawRangeMarker() method - now uses 'paint' and
065 * 'stroke' rather than 'outlinePaint' and 'outlineStroke' (DG);
066 * 15-Jun-2004 : Interval markers can now use GradientPaint (DG);
067 * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities
068 * --> TextUtilities (DG);
069 * 01-Oct-2004 : Fixed bug 1029697, problem with label alignment in
070 * drawRangeMarker() method (DG);
071 * 07-Jan-2005 : Renamed getRangeExtent() --> findRangeBounds() (DG);
072 * 21-Jan-2005 : Modified return type of calculateRangeMarkerTextAnchorPoint()
073 * method (DG);
074 * 08-Mar-2005 : Fixed positioning of marker labels (DG);
075 * 20-Apr-2005 : Added legend label, tooltip and URL generators (DG);
076 * 01-Jun-2005 : Handle one dimension of the marker label adjustment
077 * automatically (DG);
078 * 09-Jun-2005 : Added utility method for adding an item entity (DG);
079 * ------------- JFREECHART 1.0.x ---------------------------------------------
080 * 01-Mar-2006 : Updated getLegendItems() to check seriesVisibleInLegend
081 * flags (DG);
082 * 20-Jul-2006 : Set dataset and series indices in LegendItem (DG);
083 * 23-Oct-2006 : Draw outlines for interval markers (DG);
084 * 24-Oct-2006 : Respect alpha setting in markers, as suggested by Sergei
085 * Ivanov in patch 1567843 (DG);
086 * 30-Nov-2006 : Added a check for series visibility in the getLegendItem()
087 * method (DG);
088 * 07-Dec-2006 : Fix for equals() method (DG);
089 * 22-Feb-2007 : Added createState() method (DG);
090 * 01-Mar-2007 : Fixed interval marker drawing (patch 1670686 thanks to
091 * Sergei Ivanov) (DG);
092 * 20-Apr-2007 : Updated getLegendItem() for renderer change, and deprecated
093 * itemLabelGenerator, toolTipGenerator and itemURLGenerator
094 * override fields (DG);
095 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
096 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG);
097 * 26-Jun-2008 : Added crosshair support (DG);
098 * 25-Nov-2008 : Fixed bug in findRangeBounds() method (DG);
099 *
100 */
101
102 package org.jfree.chart.renderer.category;
103
104 import java.awt.AlphaComposite;
105 import java.awt.Composite;
106 import java.awt.Font;
107 import java.awt.GradientPaint;
108 import java.awt.Graphics2D;
109 import java.awt.Paint;
110 import java.awt.Shape;
111 import java.awt.Stroke;
112 import java.awt.geom.Line2D;
113 import java.awt.geom.Point2D;
114 import java.awt.geom.Rectangle2D;
115 import java.io.Serializable;
116
117 import org.jfree.chart.LegendItem;
118 import org.jfree.chart.LegendItemCollection;
119 import org.jfree.chart.axis.CategoryAxis;
120 import org.jfree.chart.axis.ValueAxis;
121 import org.jfree.chart.entity.CategoryItemEntity;
122 import org.jfree.chart.entity.EntityCollection;
123 import org.jfree.chart.event.RendererChangeEvent;
124 import org.jfree.chart.labels.CategoryItemLabelGenerator;
125 import org.jfree.chart.labels.CategorySeriesLabelGenerator;
126 import org.jfree.chart.labels.CategoryToolTipGenerator;
127 import org.jfree.chart.labels.ItemLabelPosition;
128 import org.jfree.chart.labels.StandardCategorySeriesLabelGenerator;
129 import org.jfree.chart.plot.CategoryCrosshairState;
130 import org.jfree.chart.plot.CategoryMarker;
131 import org.jfree.chart.plot.CategoryPlot;
132 import org.jfree.chart.plot.DrawingSupplier;
133 import org.jfree.chart.plot.IntervalMarker;
134 import org.jfree.chart.plot.Marker;
135 import org.jfree.chart.plot.PlotOrientation;
136 import org.jfree.chart.plot.PlotRenderingInfo;
137 import org.jfree.chart.plot.ValueMarker;
138 import org.jfree.chart.renderer.AbstractRenderer;
139 import org.jfree.chart.urls.CategoryURLGenerator;
140 import org.jfree.data.Range;
141 import org.jfree.data.category.CategoryDataset;
142 import org.jfree.data.general.DatasetUtilities;
143 import org.jfree.text.TextUtilities;
144 import org.jfree.ui.GradientPaintTransformer;
145 import org.jfree.ui.LengthAdjustmentType;
146 import org.jfree.ui.RectangleAnchor;
147 import org.jfree.ui.RectangleEdge;
148 import org.jfree.ui.RectangleInsets;
149 import org.jfree.util.ObjectList;
150 import org.jfree.util.ObjectUtilities;
151 import org.jfree.util.PublicCloneable;
152
153 /**
154 * An abstract base class that you can use to implement a new
155 * {@link CategoryItemRenderer}. When you create a new
156 * {@link CategoryItemRenderer} you are not required to extend this class,
157 * but it makes the job easier.
158 */
159 public abstract class AbstractCategoryItemRenderer extends AbstractRenderer
160 implements CategoryItemRenderer, Cloneable, PublicCloneable,
161 Serializable {
162
163 /** For serialization. */
164 private static final long serialVersionUID = 1247553218442497391L;
165
166 /** The plot that the renderer is assigned to. */
167 private CategoryPlot plot;
168
169 /**
170 * The item label generator for ALL series.
171 *
172 * @deprecated This field is redundant and deprecated as of version 1.0.6.
173 */
174 private CategoryItemLabelGenerator itemLabelGenerator;
175
176 /** A list of item label generators (one per series). */
177 private ObjectList itemLabelGeneratorList;
178
179 /** The base item label generator. */
180 private CategoryItemLabelGenerator baseItemLabelGenerator;
181
182 /**
183 * The tool tip generator for ALL series.
184 *
185 * @deprecated This field is redundant and deprecated as of version 1.0.6.
186 */
187 private CategoryToolTipGenerator toolTipGenerator;
188
189 /** A list of tool tip generators (one per series). */
190 private ObjectList toolTipGeneratorList;
191
192 /** The base tool tip generator. */
193 private CategoryToolTipGenerator baseToolTipGenerator;
194
195 /**
196 * The URL generator.
197 *
198 * @deprecated This field is redundant and deprecated as of version 1.0.6.
199 */
200 private CategoryURLGenerator itemURLGenerator;
201
202 /** A list of item label generators (one per series). */
203 private ObjectList itemURLGeneratorList;
204
205 /** The base item label generator. */
206 private CategoryURLGenerator baseItemURLGenerator;
207
208 /** The legend item label generator. */
209 private CategorySeriesLabelGenerator legendItemLabelGenerator;
210
211 /** The legend item tool tip generator. */
212 private CategorySeriesLabelGenerator legendItemToolTipGenerator;
213
214 /** The legend item URL generator. */
215 private CategorySeriesLabelGenerator legendItemURLGenerator;
216
217 /** The number of rows in the dataset (temporary record). */
218 private transient int rowCount;
219
220 /** The number of columns in the dataset (temporary record). */
221 private transient int columnCount;
222
223 /**
224 * Creates a new renderer with no tool tip generator and no URL generator.
225 * The defaults (no tool tip or URL generators) have been chosen to
226 * minimise the processing required to generate a default chart. If you
227 * require tool tips or URLs, then you can easily add the required
228 * generators.
229 */
230 protected AbstractCategoryItemRenderer() {
231 this.itemLabelGenerator = null;
232 this.itemLabelGeneratorList = new ObjectList();
233 this.toolTipGenerator = null;
234 this.toolTipGeneratorList = new ObjectList();
235 this.itemURLGenerator = null;
236 this.itemURLGeneratorList = new ObjectList();
237 this.legendItemLabelGenerator
238 = new StandardCategorySeriesLabelGenerator();
239 }
240
241 /**
242 * Returns the number of passes through the dataset required by the
243 * renderer. This method returns <code>1</code>, subclasses should
244 * override if they need more passes.
245 *
246 * @return The pass count.
247 */
248 public int getPassCount() {
249 return 1;
250 }
251
252 /**
253 * Returns the plot that the renderer has been assigned to (where
254 * <code>null</code> indicates that the renderer is not currently assigned
255 * to a plot).
256 *
257 * @return The plot (possibly <code>null</code>).
258 *
259 * @see #setPlot(CategoryPlot)
260 */
261 public CategoryPlot getPlot() {
262 return this.plot;
263 }
264
265 /**
266 * Sets the plot that the renderer has been assigned to. This method is
267 * usually called by the {@link CategoryPlot}, in normal usage you
268 * shouldn't need to call this method directly.
269 *
270 * @param plot the plot (<code>null</code> not permitted).
271 *
272 * @see #getPlot()
273 */
274 public void setPlot(CategoryPlot plot) {
275 if (plot == null) {
276 throw new IllegalArgumentException("Null 'plot' argument.");
277 }
278 this.plot = plot;
279 }
280
281 // ITEM LABEL GENERATOR
282
283 /**
284 * Returns the item label generator for a data item. This implementation
285 * simply passes control to the {@link #getSeriesItemLabelGenerator(int)}
286 * method. If, for some reason, you want a different generator for
287 * individual items, you can override this method.
288 *
289 * @param row the row index (zero based).
290 * @param column the column index (zero based).
291 *
292 * @return The generator (possibly <code>null</code>).
293 */
294 public CategoryItemLabelGenerator getItemLabelGenerator(int row,
295 int column) {
296 return getSeriesItemLabelGenerator(row);
297 }
298
299 /**
300 * Returns the item label generator for a series.
301 *
302 * @param series the series index (zero based).
303 *
304 * @return The generator (possibly <code>null</code>).
305 *
306 * @see #setSeriesItemLabelGenerator(int, CategoryItemLabelGenerator)
307 */
308 public CategoryItemLabelGenerator getSeriesItemLabelGenerator(int series) {
309
310 // return the generator for ALL series, if there is one...
311 if (this.itemLabelGenerator != null) {
312 return this.itemLabelGenerator;
313 }
314
315 // otherwise look up the generator table
316 CategoryItemLabelGenerator generator = (CategoryItemLabelGenerator)
317 this.itemLabelGeneratorList.get(series);
318 if (generator == null) {
319 generator = this.baseItemLabelGenerator;
320 }
321 return generator;
322
323 }
324
325 /**
326 * Sets the item label generator for ALL series and sends a
327 * {@link RendererChangeEvent} to all registered listeners.
328 *
329 * @param generator the generator (<code>null</code> permitted).
330 *
331 * @deprecated This method should no longer be used (as of version 1.0.6).
332 * It is sufficient to rely on {@link #setSeriesItemLabelGenerator(int,
333 * CategoryItemLabelGenerator)} and
334 * {@link #setBaseItemLabelGenerator(CategoryItemLabelGenerator)}.
335 */
336 public void setItemLabelGenerator(CategoryItemLabelGenerator generator) {
337 this.itemLabelGenerator = generator;
338 fireChangeEvent();
339 }
340
341 /**
342 * Sets the item label generator for a series and sends a
343 * {@link RendererChangeEvent} to all registered listeners.
344 *
345 * @param series the series index (zero based).
346 * @param generator the generator (<code>null</code> permitted).
347 *
348 * @see #getSeriesItemLabelGenerator(int)
349 */
350 public void setSeriesItemLabelGenerator(int series,
351 CategoryItemLabelGenerator generator) {
352 this.itemLabelGeneratorList.set(series, generator);
353 fireChangeEvent();
354 }
355
356 /**
357 * Returns the base item label generator.
358 *
359 * @return The generator (possibly <code>null</code>).
360 *
361 * @see #setBaseItemLabelGenerator(CategoryItemLabelGenerator)
362 */
363 public CategoryItemLabelGenerator getBaseItemLabelGenerator() {
364 return this.baseItemLabelGenerator;
365 }
366
367 /**
368 * Sets the base item label generator and sends a
369 * {@link RendererChangeEvent} to all registered listeners.
370 *
371 * @param generator the generator (<code>null</code> permitted).
372 *
373 * @see #getBaseItemLabelGenerator()
374 */
375 public void setBaseItemLabelGenerator(
376 CategoryItemLabelGenerator generator) {
377 this.baseItemLabelGenerator = generator;
378 fireChangeEvent();
379 }
380
381 // TOOL TIP GENERATOR
382
383 /**
384 * Returns the tool tip generator that should be used for the specified
385 * item. This method looks up the generator using the "three-layer"
386 * approach outlined in the general description of this interface. You
387 * can override this method if you want to return a different generator per
388 * item.
389 *
390 * @param row the row index (zero-based).
391 * @param column the column index (zero-based).
392 *
393 * @return The generator (possibly <code>null</code>).
394 */
395 public CategoryToolTipGenerator getToolTipGenerator(int row, int column) {
396
397 CategoryToolTipGenerator result = null;
398 if (this.toolTipGenerator != null) {
399 result = this.toolTipGenerator;
400 }
401 else {
402 result = getSeriesToolTipGenerator(row);
403 if (result == null) {
404 result = this.baseToolTipGenerator;
405 }
406 }
407 return result;
408 }
409
410 /**
411 * Returns the tool tip generator that will be used for ALL items in the
412 * dataset (the "layer 0" generator).
413 *
414 * @return A tool tip generator (possibly <code>null</code>).
415 *
416 * @see #setToolTipGenerator(CategoryToolTipGenerator)
417 *
418 * @deprecated This method should no longer be used (as of version 1.0.6).
419 * It is sufficient to rely on {@link #getSeriesToolTipGenerator(int)}
420 * and {@link #getBaseToolTipGenerator()}.
421 */
422 public CategoryToolTipGenerator getToolTipGenerator() {
423 return this.toolTipGenerator;
424 }
425
426 /**
427 * Sets the tool tip generator for ALL series and sends a
428 * {@link org.jfree.chart.event.RendererChangeEvent} to all registered
429 * listeners.
430 *
431 * @param generator the generator (<code>null</code> permitted).
432 *
433 * @see #getToolTipGenerator()
434 *
435 * @deprecated This method should no longer be used (as of version 1.0.6).
436 * It is sufficient to rely on {@link #setSeriesToolTipGenerator(int,
437 * CategoryToolTipGenerator)} and
438 * {@link #setBaseToolTipGenerator(CategoryToolTipGenerator)}.
439 */
440 public void setToolTipGenerator(CategoryToolTipGenerator generator) {
441 this.toolTipGenerator = generator;
442 fireChangeEvent();
443 }
444
445 /**
446 * Returns the tool tip generator for the specified series (a "layer 1"
447 * generator).
448 *
449 * @param series the series index (zero-based).
450 *
451 * @return The tool tip generator (possibly <code>null</code>).
452 *
453 * @see #setSeriesToolTipGenerator(int, CategoryToolTipGenerator)
454 */
455 public CategoryToolTipGenerator getSeriesToolTipGenerator(int series) {
456 return (CategoryToolTipGenerator) this.toolTipGeneratorList.get(series);
457 }
458
459 /**
460 * Sets the tool tip generator for a series and sends a
461 * {@link RendererChangeEvent} to all registered listeners.
462 *
463 * @param series the series index (zero-based).
464 * @param generator the generator (<code>null</code> permitted).
465 *
466 * @see #getSeriesToolTipGenerator(int)
467 */
468 public void setSeriesToolTipGenerator(int series,
469 CategoryToolTipGenerator generator) {
470 this.toolTipGeneratorList.set(series, generator);
471 fireChangeEvent();
472 }
473
474 /**
475 * Returns the base tool tip generator (the "layer 2" generator).
476 *
477 * @return The tool tip generator (possibly <code>null</code>).
478 *
479 * @see #setBaseToolTipGenerator(CategoryToolTipGenerator)
480 */
481 public CategoryToolTipGenerator getBaseToolTipGenerator() {
482 return this.baseToolTipGenerator;
483 }
484
485 /**
486 * Sets the base tool tip generator and sends a {@link RendererChangeEvent}
487 * to all registered listeners.
488 *
489 * @param generator the generator (<code>null</code> permitted).
490 *
491 * @see #getBaseToolTipGenerator()
492 */
493 public void setBaseToolTipGenerator(CategoryToolTipGenerator generator) {
494 this.baseToolTipGenerator = generator;
495 fireChangeEvent();
496 }
497
498 // URL GENERATOR
499
500 /**
501 * Returns the URL generator for a data item. This method just calls the
502 * getSeriesItemURLGenerator method, but you can override this behaviour if
503 * you want to.
504 *
505 * @param row the row index (zero based).
506 * @param column the column index (zero based).
507 *
508 * @return The URL generator.
509 */
510 public CategoryURLGenerator getItemURLGenerator(int row, int column) {
511 return getSeriesItemURLGenerator(row);
512 }
513
514 /**
515 * Returns the URL generator for a series.
516 *
517 * @param series the series index (zero based).
518 *
519 * @return The URL generator for the series.
520 *
521 * @see #setSeriesItemURLGenerator(int, CategoryURLGenerator)
522 */
523 public CategoryURLGenerator getSeriesItemURLGenerator(int series) {
524
525 // return the generator for ALL series, if there is one...
526 if (this.itemURLGenerator != null) {
527 return this.itemURLGenerator;
528 }
529
530 // otherwise look up the generator table
531 CategoryURLGenerator generator
532 = (CategoryURLGenerator) this.itemURLGeneratorList.get(series);
533 if (generator == null) {
534 generator = this.baseItemURLGenerator;
535 }
536 return generator;
537
538 }
539
540 /**
541 * Sets the item URL generator for ALL series and sends a
542 * {@link RendererChangeEvent} to all registered listeners.
543 *
544 * @param generator the generator.
545 *
546 * @deprecated This method should no longer be used (as of version 1.0.6).
547 * It is sufficient to rely on {@link #setSeriesItemURLGenerator(int,
548 * CategoryURLGenerator)} and
549 * {@link #setBaseItemURLGenerator(CategoryURLGenerator)}.
550 */
551 public void setItemURLGenerator(CategoryURLGenerator generator) {
552 this.itemURLGenerator = generator;
553 fireChangeEvent();
554 }
555
556 /**
557 * Sets the URL generator for a series and sends a
558 * {@link RendererChangeEvent} to all registered listeners.
559 *
560 * @param series the series index (zero based).
561 * @param generator the generator.
562 *
563 * @see #getSeriesItemURLGenerator(int)
564 */
565 public void setSeriesItemURLGenerator(int series,
566 CategoryURLGenerator generator) {
567 this.itemURLGeneratorList.set(series, generator);
568 fireChangeEvent();
569 }
570
571 /**
572 * Returns the base item URL generator.
573 *
574 * @return The item URL generator.
575 *
576 * @see #setBaseItemURLGenerator(CategoryURLGenerator)
577 */
578 public CategoryURLGenerator getBaseItemURLGenerator() {
579 return this.baseItemURLGenerator;
580 }
581
582 /**
583 * Sets the base item URL generator and sends a
584 * {@link RendererChangeEvent} to all registered listeners.
585 *
586 * @param generator the item URL generator (<code>null</code> permitted).
587 *
588 * @see #getBaseItemURLGenerator()
589 */
590 public void setBaseItemURLGenerator(CategoryURLGenerator generator) {
591 this.baseItemURLGenerator = generator;
592 fireChangeEvent();
593 }
594
595 /**
596 * Returns the number of rows in the dataset. This value is updated in the
597 * {@link AbstractCategoryItemRenderer#initialise} method.
598 *
599 * @return The row count.
600 */
601 public int getRowCount() {
602 return this.rowCount;
603 }
604
605 /**
606 * Returns the number of columns in the dataset. This value is updated in
607 * the {@link AbstractCategoryItemRenderer#initialise} method.
608 *
609 * @return The column count.
610 */
611 public int getColumnCount() {
612 return this.columnCount;
613 }
614
615 /**
616 * Creates a new state instance---this method is called from the
617 * {@link #initialise(Graphics2D, Rectangle2D, CategoryPlot, int,
618 * PlotRenderingInfo)} method. Subclasses can override this method if
619 * they need to use a subclass of {@link CategoryItemRendererState}.
620 *
621 * @param info collects plot rendering info (<code>null</code> permitted).
622 *
623 * @return The new state instance (never <code>null</code>).
624 *
625 * @since 1.0.5
626 */
627 protected CategoryItemRendererState createState(PlotRenderingInfo info) {
628 return new CategoryItemRendererState(info);
629 }
630
631 /**
632 * Initialises the renderer and returns a state object that will be used
633 * for the remainder of the drawing process for a single chart. The state
634 * object allows for the fact that the renderer may be used simultaneously
635 * by multiple threads (each thread will work with a separate state object).
636 *
637 * @param g2 the graphics device.
638 * @param dataArea the data area.
639 * @param plot the plot.
640 * @param rendererIndex the renderer index.
641 * @param info an object for returning information about the structure of
642 * the plot (<code>null</code> permitted).
643 *
644 * @return The renderer state.
645 */
646 public CategoryItemRendererState initialise(Graphics2D g2,
647 Rectangle2D dataArea,
648 CategoryPlot plot,
649 int rendererIndex,
650 PlotRenderingInfo info) {
651
652 setPlot(plot);
653 CategoryDataset data = plot.getDataset(rendererIndex);
654 if (data != null) {
655 this.rowCount = data.getRowCount();
656 this.columnCount = data.getColumnCount();
657 }
658 else {
659 this.rowCount = 0;
660 this.columnCount = 0;
661 }
662 return createState(info);
663
664 }
665
666 /**
667 * Returns the range of values the renderer requires to display all the
668 * items from the specified dataset.
669 *
670 * @param dataset the dataset (<code>null</code> permitted).
671 *
672 * @return The range (or <code>null</code> if the dataset is
673 * <code>null</code> or empty).
674 */
675 public Range findRangeBounds(CategoryDataset dataset) {
676 if (dataset == null) {
677 return null;
678 }
679 return DatasetUtilities.findRangeBounds(dataset);
680 }
681
682 /**
683 * Returns the Java2D coordinate for the middle of the specified data item.
684 *
685 * @param rowKey the row key.
686 * @param columnKey the column key.
687 * @param dataset the dataset.
688 * @param axis the axis.
689 * @param area the data area.
690 * @param edge the edge along which the axis lies.
691 *
692 * @return The Java2D coordinate for the middle of the item.
693 *
694 * @since 1.0.11
695 */
696 public double getItemMiddle(Comparable rowKey, Comparable columnKey,
697 CategoryDataset dataset, CategoryAxis axis, Rectangle2D area,
698 RectangleEdge edge) {
699 return axis.getCategoryMiddle(columnKey, dataset.getColumnKeys(), area,
700 edge);
701 }
702
703 /**
704 * Draws a background for the data area. The default implementation just
705 * gets the plot to draw the background, but some renderers will override
706 * this behaviour.
707 *
708 * @param g2 the graphics device.
709 * @param plot the plot.
710 * @param dataArea the data area.
711 */
712 public void drawBackground(Graphics2D g2,
713 CategoryPlot plot,
714 Rectangle2D dataArea) {
715
716 plot.drawBackground(g2, dataArea);
717
718 }
719
720 /**
721 * Draws an outline for the data area. The default implementation just
722 * gets the plot to draw the outline, but some renderers will override this
723 * behaviour.
724 *
725 * @param g2 the graphics device.
726 * @param plot the plot.
727 * @param dataArea the data area.
728 */
729 public void drawOutline(Graphics2D g2,
730 CategoryPlot plot,
731 Rectangle2D dataArea) {
732
733 plot.drawOutline(g2, dataArea);
734
735 }
736
737 /**
738 * Draws a grid line against the domain axis.
739 * <P>
740 * Note that this default implementation assumes that the horizontal axis
741 * is the domain axis. If this is not the case, you will need to override
742 * this method.
743 *
744 * @param g2 the graphics device.
745 * @param plot the plot.
746 * @param dataArea the area for plotting data (not yet adjusted for any
747 * 3D effect).
748 * @param value the Java2D value at which the grid line should be drawn.
749 *
750 * @see #drawRangeGridline(Graphics2D, CategoryPlot, ValueAxis,
751 * Rectangle2D, double)
752 */
753 public void drawDomainGridline(Graphics2D g2,
754 CategoryPlot plot,
755 Rectangle2D dataArea,
756 double value) {
757
758 Line2D line = null;
759 PlotOrientation orientation = plot.getOrientation();
760
761 if (orientation == PlotOrientation.HORIZONTAL) {
762 line = new Line2D.Double(dataArea.getMinX(), value,
763 dataArea.getMaxX(), value);
764 }
765 else if (orientation == PlotOrientation.VERTICAL) {
766 line = new Line2D.Double(value, dataArea.getMinY(), value,
767 dataArea.getMaxY());
768 }
769
770 Paint paint = plot.getDomainGridlinePaint();
771 if (paint == null) {
772 paint = CategoryPlot.DEFAULT_GRIDLINE_PAINT;
773 }
774 g2.setPaint(paint);
775
776 Stroke stroke = plot.getDomainGridlineStroke();
777 if (stroke == null) {
778 stroke = CategoryPlot.DEFAULT_GRIDLINE_STROKE;
779 }
780 g2.setStroke(stroke);
781
782 g2.draw(line);
783
784 }
785
786 /**
787 * Draws a grid line against the range axis.
788 *
789 * @param g2 the graphics device.
790 * @param plot the plot.
791 * @param axis the value axis.
792 * @param dataArea the area for plotting data (not yet adjusted for any
793 * 3D effect).
794 * @param value the value at which the grid line should be drawn.
795 *
796 * @see #drawDomainGridline(Graphics2D, CategoryPlot, Rectangle2D, double)
797 *
798 */
799 public void drawRangeGridline(Graphics2D g2,
800 CategoryPlot plot,
801 ValueAxis axis,
802 Rectangle2D dataArea,
803 double value) {
804
805 Range range = axis.getRange();
806 if (!range.contains(value)) {
807 return;
808 }
809
810 PlotOrientation orientation = plot.getOrientation();
811 double v = axis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge());
812 Line2D line = null;
813 if (orientation == PlotOrientation.HORIZONTAL) {
814 line = new Line2D.Double(v, dataArea.getMinY(), v,
815 dataArea.getMaxY());
816 }
817 else if (orientation == PlotOrientation.VERTICAL) {
818 line = new Line2D.Double(dataArea.getMinX(), v,
819 dataArea.getMaxX(), v);
820 }
821
822 Paint paint = plot.getRangeGridlinePaint();
823 if (paint == null) {
824 paint = CategoryPlot.DEFAULT_GRIDLINE_PAINT;
825 }
826 g2.setPaint(paint);
827
828 Stroke stroke = plot.getRangeGridlineStroke();
829 if (stroke == null) {
830 stroke = CategoryPlot.DEFAULT_GRIDLINE_STROKE;
831 }
832 g2.setStroke(stroke);
833
834 g2.draw(line);
835
836 }
837
838 /**
839 * Draws a marker for the domain axis.
840 *
841 * @param g2 the graphics device (not <code>null</code>).
842 * @param plot the plot (not <code>null</code>).
843 * @param axis the range axis (not <code>null</code>).
844 * @param marker the marker to be drawn (not <code>null</code>).
845 * @param dataArea the area inside the axes (not <code>null</code>).
846 *
847 * @see #drawRangeMarker(Graphics2D, CategoryPlot, ValueAxis, Marker,
848 * Rectangle2D)
849 */
850 public void drawDomainMarker(Graphics2D g2,
851 CategoryPlot plot,
852 CategoryAxis axis,
853 CategoryMarker marker,
854 Rectangle2D dataArea) {
855
856 Comparable category = marker.getKey();
857 CategoryDataset dataset = plot.getDataset(plot.getIndexOf(this));
858 int columnIndex = dataset.getColumnIndex(category);
859 if (columnIndex < 0) {
860 return;
861 }
862
863 final Composite savedComposite = g2.getComposite();
864 g2.setComposite(AlphaComposite.getInstance(
865 AlphaComposite.SRC_OVER, marker.getAlpha()));
866
867 PlotOrientation orientation = plot.getOrientation();
868 Rectangle2D bounds = null;
869 if (marker.getDrawAsLine()) {
870 double v = axis.getCategoryMiddle(columnIndex,
871 dataset.getColumnCount(), dataArea,
872 plot.getDomainAxisEdge());
873 Line2D line = null;
874 if (orientation == PlotOrientation.HORIZONTAL) {
875 line = new Line2D.Double(dataArea.getMinX(), v,
876 dataArea.getMaxX(), v);
877 }
878 else if (orientation == PlotOrientation.VERTICAL) {
879 line = new Line2D.Double(v, dataArea.getMinY(), v,
880 dataArea.getMaxY());
881 }
882 g2.setPaint(marker.getPaint());
883 g2.setStroke(marker.getStroke());
884 g2.draw(line);
885 bounds = line.getBounds2D();
886 }
887 else {
888 double v0 = axis.getCategoryStart(columnIndex,
889 dataset.getColumnCount(), dataArea,
890 plot.getDomainAxisEdge());
891 double v1 = axis.getCategoryEnd(columnIndex,
892 dataset.getColumnCount(), dataArea,
893 plot.getDomainAxisEdge());
894 Rectangle2D area = null;
895 if (orientation == PlotOrientation.HORIZONTAL) {
896 area = new Rectangle2D.Double(dataArea.getMinX(), v0,
897 dataArea.getWidth(), (v1 - v0));
898 }
899 else if (orientation == PlotOrientation.VERTICAL) {
900 area = new Rectangle2D.Double(v0, dataArea.getMinY(),
901 (v1 - v0), dataArea.getHeight());
902 }
903 g2.setPaint(marker.getPaint());
904 g2.fill(area);
905 bounds = area;
906 }
907
908 String label = marker.getLabel();
909 RectangleAnchor anchor = marker.getLabelAnchor();
910 if (label != null) {
911 Font labelFont = marker.getLabelFont();
912 g2.setFont(labelFont);
913 g2.setPaint(marker.getLabelPaint());
914 Point2D coordinates = calculateDomainMarkerTextAnchorPoint(
915 g2, orientation, dataArea, bounds, marker.getLabelOffset(),
916 marker.getLabelOffsetType(), anchor);
917 TextUtilities.drawAlignedString(label, g2,
918 (float) coordinates.getX(), (float) coordinates.getY(),
919 marker.getLabelTextAnchor());
920 }
921 g2.setComposite(savedComposite);
922 }
923
924 /**
925 * Draws a marker for the range axis.
926 *
927 * @param g2 the graphics device (not <code>null</code>).
928 * @param plot the plot (not <code>null</code>).
929 * @param axis the range axis (not <code>null</code>).
930 * @param marker the marker to be drawn (not <code>null</code>).
931 * @param dataArea the area inside the axes (not <code>null</code>).
932 *
933 * @see #drawDomainMarker(Graphics2D, CategoryPlot, CategoryAxis,
934 * CategoryMarker, Rectangle2D)
935 */
936 public void drawRangeMarker(Graphics2D g2,
937 CategoryPlot plot,
938 ValueAxis axis,
939 Marker marker,
940 Rectangle2D dataArea) {
941
942 if (marker instanceof ValueMarker) {
943 ValueMarker vm = (ValueMarker) marker;
944 double value = vm.getValue();
945 Range range = axis.getRange();
946
947 if (!range.contains(value)) {
948 return;
949 }
950
951 final Composite savedComposite = g2.getComposite();
952 g2.setComposite(AlphaComposite.getInstance(
953 AlphaComposite.SRC_OVER, marker.getAlpha()));
954
955 PlotOrientation orientation = plot.getOrientation();
956 double v = axis.valueToJava2D(value, dataArea,
957 plot.getRangeAxisEdge());
958 Line2D line = null;
959 if (orientation == PlotOrientation.HORIZONTAL) {
960 line = new Line2D.Double(v, dataArea.getMinY(), v,
961 dataArea.getMaxY());
962 }
963 else if (orientation == PlotOrientation.VERTICAL) {
964 line = new Line2D.Double(dataArea.getMinX(), v,
965 dataArea.getMaxX(), v);
966 }
967
968 g2.setPaint(marker.getPaint());
969 g2.setStroke(marker.getStroke());
970 g2.draw(line);
971
972 String label = marker.getLabel();
973 RectangleAnchor anchor = marker.getLabelAnchor();
974 if (label != null) {
975 Font labelFont = marker.getLabelFont();
976 g2.setFont(labelFont);
977 g2.setPaint(marker.getLabelPaint());
978 Point2D coordinates = calculateRangeMarkerTextAnchorPoint(
979 g2, orientation, dataArea, line.getBounds2D(),
980 marker.getLabelOffset(), LengthAdjustmentType.EXPAND,
981 anchor);
982 TextUtilities.drawAlignedString(label, g2,
983 (float) coordinates.getX(), (float) coordinates.getY(),
984 marker.getLabelTextAnchor());
985 }
986 g2.setComposite(savedComposite);
987 }
988 else if (marker instanceof IntervalMarker) {
989 IntervalMarker im = (IntervalMarker) marker;
990 double start = im.getStartValue();
991 double end = im.getEndValue();
992 Range range = axis.getRange();
993 if (!(range.intersects(start, end))) {
994 return;
995 }
996
997 final Composite savedComposite = g2.getComposite();
998 g2.setComposite(AlphaComposite.getInstance(
999 AlphaComposite.SRC_OVER, marker.getAlpha()));
1000
1001 double start2d = axis.valueToJava2D(start, dataArea,
1002 plot.getRangeAxisEdge());
1003 double end2d = axis.valueToJava2D(end, dataArea,
1004 plot.getRangeAxisEdge());
1005 double low = Math.min(start2d, end2d);
1006 double high = Math.max(start2d, end2d);
1007
1008 PlotOrientation orientation = plot.getOrientation();
1009 Rectangle2D rect = null;
1010 if (orientation == PlotOrientation.HORIZONTAL) {
1011 // clip left and right bounds to data area
1012 low = Math.max(low, dataArea.getMinX());
1013 high = Math.min(high, dataArea.getMaxX());
1014 rect = new Rectangle2D.Double(low,
1015 dataArea.getMinY(), high - low,
1016 dataArea.getHeight());
1017 }
1018 else if (orientation == PlotOrientation.VERTICAL) {
1019 // clip top and bottom bounds to data area
1020 low = Math.max(low, dataArea.getMinY());
1021 high = Math.min(high, dataArea.getMaxY());
1022 rect = new Rectangle2D.Double(dataArea.getMinX(),
1023 low, dataArea.getWidth(),
1024 high - low);
1025 }
1026 Paint p = marker.getPaint();
1027 if (p instanceof GradientPaint) {
1028 GradientPaint gp = (GradientPaint) p;
1029 GradientPaintTransformer t = im.getGradientPaintTransformer();
1030 if (t != null) {
1031 gp = t.transform(gp, rect);
1032 }
1033 g2.setPaint(gp);
1034 }
1035 else {
1036 g2.setPaint(p);
1037 }
1038 g2.fill(rect);
1039
1040 // now draw the outlines, if visible...
1041 if (im.getOutlinePaint() != null && im.getOutlineStroke() != null) {
1042 if (orientation == PlotOrientation.VERTICAL) {
1043 Line2D line = new Line2D.Double();
1044 double x0 = dataArea.getMinX();
1045 double x1 = dataArea.getMaxX();
1046 g2.setPaint(im.getOutlinePaint());
1047 g2.setStroke(im.getOutlineStroke());
1048 if (range.contains(start)) {
1049 line.setLine(x0, start2d, x1, start2d);
1050 g2.draw(line);
1051 }
1052 if (range.contains(end)) {
1053 line.setLine(x0, end2d, x1, end2d);
1054 g2.draw(line);
1055 }
1056 }
1057 else { // PlotOrientation.HORIZONTAL
1058 Line2D line = new Line2D.Double();
1059 double y0 = dataArea.getMinY();
1060 double y1 = dataArea.getMaxY();
1061 g2.setPaint(im.getOutlinePaint());
1062 g2.setStroke(im.getOutlineStroke());
1063 if (range.contains(start)) {
1064 line.setLine(start2d, y0, start2d, y1);
1065 g2.draw(line);
1066 }
1067 if (range.contains(end)) {
1068 line.setLine(end2d, y0, end2d, y1);
1069 g2.draw(line);
1070 }
1071 }
1072 }
1073
1074 String label = marker.getLabel();
1075 RectangleAnchor anchor = marker.getLabelAnchor();
1076 if (label != null) {
1077 Font labelFont = marker.getLabelFont();
1078 g2.setFont(labelFont);
1079 g2.setPaint(marker.getLabelPaint());
1080 Point2D coordinates = calculateRangeMarkerTextAnchorPoint(
1081 g2, orientation, dataArea, rect,
1082 marker.getLabelOffset(), marker.getLabelOffsetType(),
1083 anchor);
1084 TextUtilities.drawAlignedString(label, g2,
1085 (float) coordinates.getX(), (float) coordinates.getY(),
1086 marker.getLabelTextAnchor());
1087 }
1088 g2.setComposite(savedComposite);
1089 }
1090 }
1091
1092 /**
1093 * Calculates the (x, y) coordinates for drawing the label for a marker on
1094 * the range axis.
1095 *
1096 * @param g2 the graphics device.
1097 * @param orientation the plot orientation.
1098 * @param dataArea the data area.
1099 * @param markerArea the rectangle surrounding the marker.
1100 * @param markerOffset the marker offset.
1101 * @param labelOffsetType the label offset type.
1102 * @param anchor the label anchor.
1103 *
1104 * @return The coordinates for drawing the marker label.
1105 */
1106 protected Point2D calculateDomainMarkerTextAnchorPoint(Graphics2D g2,
1107 PlotOrientation orientation,
1108 Rectangle2D dataArea,
1109 Rectangle2D markerArea,
1110 RectangleInsets markerOffset,
1111 LengthAdjustmentType labelOffsetType,
1112 RectangleAnchor anchor) {
1113
1114 Rectangle2D anchorRect = null;
1115 if (orientation == PlotOrientation.HORIZONTAL) {
1116 anchorRect = markerOffset.createAdjustedRectangle(markerArea,
1117 LengthAdjustmentType.CONTRACT, labelOffsetType);
1118 }
1119 else if (orientation == PlotOrientation.VERTICAL) {
1120 anchorRect = markerOffset.createAdjustedRectangle(markerArea,
1121 labelOffsetType, LengthAdjustmentType.CONTRACT);
1122 }
1123 return RectangleAnchor.coordinates(anchorRect, anchor);
1124
1125 }
1126
1127 /**
1128 * Calculates the (x, y) coordinates for drawing a marker label.
1129 *
1130 * @param g2 the graphics device.
1131 * @param orientation the plot orientation.
1132 * @param dataArea the data area.
1133 * @param markerArea the rectangle surrounding the marker.
1134 * @param markerOffset the marker offset.
1135 * @param labelOffsetType the label offset type.
1136 * @param anchor the label anchor.
1137 *
1138 * @return The coordinates for drawing the marker label.
1139 */
1140 protected Point2D calculateRangeMarkerTextAnchorPoint(Graphics2D g2,
1141 PlotOrientation orientation,
1142 Rectangle2D dataArea,
1143 Rectangle2D markerArea,
1144 RectangleInsets markerOffset,
1145 LengthAdjustmentType labelOffsetType,
1146 RectangleAnchor anchor) {
1147
1148 Rectangle2D anchorRect = null;
1149 if (orientation == PlotOrientation.HORIZONTAL) {
1150 anchorRect = markerOffset.createAdjustedRectangle(markerArea,
1151 labelOffsetType, LengthAdjustmentType.CONTRACT);
1152 }
1153 else if (orientation == PlotOrientation.VERTICAL) {
1154 anchorRect = markerOffset.createAdjustedRectangle(markerArea,
1155 LengthAdjustmentType.CONTRACT, labelOffsetType);
1156 }
1157 return RectangleAnchor.coordinates(anchorRect, anchor);
1158
1159 }
1160
1161 /**
1162 * Returns a legend item for a series. This default implementation will
1163 * return <code>null</code> if {@link #isSeriesVisible(int)} or
1164 * {@link #isSeriesVisibleInLegend(int)} returns <code>false</code>.
1165 *
1166 * @param datasetIndex the dataset index (zero-based).
1167 * @param series the series index (zero-based).
1168 *
1169 * @return The legend item (possibly <code>null</code>).
1170 *
1171 * @see #getLegendItems()
1172 */
1173 public LegendItem getLegendItem(int datasetIndex, int series) {
1174
1175 CategoryPlot p = getPlot();
1176 if (p == null) {
1177 return null;
1178 }
1179
1180 // check that a legend item needs to be displayed...
1181 if (!isSeriesVisible(series) || !isSeriesVisibleInLegend(series)) {
1182 return null;
1183 }
1184
1185 CategoryDataset dataset = p.getDataset(datasetIndex);
1186 String label = this.legendItemLabelGenerator.generateLabel(dataset,
1187 series);
1188 String description = label;
1189 String toolTipText = null;
1190 if (this.legendItemToolTipGenerator != null) {
1191 toolTipText = this.legendItemToolTipGenerator.generateLabel(
1192 dataset, series);
1193 }
1194 String urlText = null;
1195 if (this.legendItemURLGenerator != null) {
1196 urlText = this.legendItemURLGenerator.generateLabel(dataset,
1197 series);
1198 }
1199 Shape shape = lookupLegendShape(series);
1200 Paint paint = lookupSeriesPaint(series);
1201 Paint outlinePaint = lookupSeriesOutlinePaint(series);
1202 Stroke outlineStroke = lookupSeriesOutlineStroke(series);
1203
1204 LegendItem item = new LegendItem(label, description, toolTipText,
1205 urlText, shape, paint, outlineStroke, outlinePaint);
1206 item.setLabelFont(lookupLegendTextFont(series));
1207 Paint labelPaint = lookupLegendTextPaint(series);
1208 if (labelPaint != null) {
1209 item.setLabelPaint(labelPaint);
1210 }
1211 item.setSeriesKey(dataset.getRowKey(series));
1212 item.setSeriesIndex(series);
1213 item.setDataset(dataset);
1214 item.setDatasetIndex(datasetIndex);
1215 return item;
1216 }
1217
1218 /**
1219 * Tests this renderer for equality with another object.
1220 *
1221 * @param obj the object.
1222 *
1223 * @return <code>true</code> or <code>false</code>.
1224 */
1225 public boolean equals(Object obj) {
1226
1227 if (obj == this) {
1228 return true;
1229 }
1230 if (!(obj instanceof AbstractCategoryItemRenderer)) {
1231 return false;
1232 }
1233 AbstractCategoryItemRenderer that = (AbstractCategoryItemRenderer) obj;
1234
1235 if (!ObjectUtilities.equal(this.itemLabelGenerator,
1236 that.itemLabelGenerator)) {
1237 return false;
1238 }
1239 if (!ObjectUtilities.equal(this.itemLabelGeneratorList,
1240 that.itemLabelGeneratorList)) {
1241 return false;
1242 }
1243 if (!ObjectUtilities.equal(this.baseItemLabelGenerator,
1244 that.baseItemLabelGenerator)) {
1245 return false;
1246 }
1247 if (!ObjectUtilities.equal(this.toolTipGenerator,
1248 that.toolTipGenerator)) {
1249 return false;
1250 }
1251 if (!ObjectUtilities.equal(this.toolTipGeneratorList,
1252 that.toolTipGeneratorList)) {
1253 return false;
1254 }
1255 if (!ObjectUtilities.equal(this.baseToolTipGenerator,
1256 that.baseToolTipGenerator)) {
1257 return false;
1258 }
1259 if (!ObjectUtilities.equal(this.itemURLGenerator,
1260 that.itemURLGenerator)) {
1261 return false;
1262 }
1263 if (!ObjectUtilities.equal(this.itemURLGeneratorList,
1264 that.itemURLGeneratorList)) {
1265 return false;
1266 }
1267 if (!ObjectUtilities.equal(this.baseItemURLGenerator,
1268 that.baseItemURLGenerator)) {
1269 return false;
1270 }
1271 if (!ObjectUtilities.equal(this.legendItemLabelGenerator,
1272 that.legendItemLabelGenerator)) {
1273 return false;
1274 }
1275 if (!ObjectUtilities.equal(this.legendItemToolTipGenerator,
1276 that.legendItemToolTipGenerator)) {
1277 return false;
1278 }
1279 if (!ObjectUtilities.equal(this.legendItemURLGenerator,
1280 that.legendItemURLGenerator)) {
1281 return false;
1282 }
1283 return super.equals(obj);
1284 }
1285
1286 /**
1287 * Returns a hash code for the renderer.
1288 *
1289 * @return The hash code.
1290 */
1291 public int hashCode() {
1292 int result = super.hashCode();
1293 return result;
1294 }
1295
1296 /**
1297 * Returns the drawing supplier from the plot.
1298 *
1299 * @return The drawing supplier (possibly <code>null</code>).
1300 */
1301 public DrawingSupplier getDrawingSupplier() {
1302 DrawingSupplier result = null;
1303 CategoryPlot cp = getPlot();
1304 if (cp != null) {
1305 result = cp.getDrawingSupplier();
1306 }
1307 return result;
1308 }
1309
1310 /**
1311 * Considers the current (x, y) coordinate and updates the crosshair point
1312 * if it meets the criteria (usually means the (x, y) coordinate is the
1313 * closest to the anchor point so far).
1314 *
1315 * @param crosshairState the crosshair state (<code>null</code> permitted,
1316 * but the method does nothing in that case).
1317 * @param rowKey the row key.
1318 * @param columnKey the column key.
1319 * @param value the data value.
1320 * @param datasetIndex the dataset index.
1321 * @param transX the x-value translated to Java2D space.
1322 * @param transY the y-value translated to Java2D space.
1323 * @param orientation the plot orientation (<code>null</code> not
1324 * permitted).
1325 *
1326 * @since 1.0.11
1327 */
1328 protected void updateCrosshairValues(CategoryCrosshairState crosshairState,
1329 Comparable rowKey, Comparable columnKey, double value,
1330 int datasetIndex,
1331 double transX, double transY, PlotOrientation orientation) {
1332
1333 if (orientation == null) {
1334 throw new IllegalArgumentException("Null 'orientation' argument.");
1335 }
1336
1337 if (crosshairState != null) {
1338 if (this.plot.isRangeCrosshairLockedOnData()) {
1339 // both axes
1340 crosshairState.updateCrosshairPoint(rowKey, columnKey, value,
1341 datasetIndex, transX, transY, orientation);
1342 }
1343 else {
1344 crosshairState.updateCrosshairX(rowKey, columnKey,
1345 datasetIndex, transX, orientation);
1346 }
1347 }
1348 }
1349
1350 /**
1351 * Draws an item label.
1352 *
1353 * @param g2 the graphics device.
1354 * @param orientation the orientation.
1355 * @param dataset the dataset.
1356 * @param row the row.
1357 * @param column the column.
1358 * @param x the x coordinate (in Java2D space).
1359 * @param y the y coordinate (in Java2D space).
1360 * @param negative indicates a negative value (which affects the item
1361 * label position).
1362 */
1363 protected void drawItemLabel(Graphics2D g2,
1364 PlotOrientation orientation,
1365 CategoryDataset dataset,
1366 int row, int column,
1367 double x, double y,
1368 boolean negative) {
1369
1370 CategoryItemLabelGenerator generator
1371 = getItemLabelGenerator(row, column);
1372 if (generator != null) {
1373 Font labelFont = getItemLabelFont(row, column);
1374 Paint paint = getItemLabelPaint(row, column);
1375 g2.setFont(labelFont);
1376 g2.setPaint(paint);
1377 String label = generator.generateLabel(dataset, row, column);
1378 ItemLabelPosition position = null;
1379 if (!negative) {
1380 position = getPositiveItemLabelPosition(row, column);
1381 }
1382 else {
1383 position = getNegativeItemLabelPosition(row, column);
1384 }
1385 Point2D anchorPoint = calculateLabelAnchorPoint(
1386 position.getItemLabelAnchor(), x, y, orientation);
1387 TextUtilities.drawRotatedString(label, g2,
1388 (float) anchorPoint.getX(), (float) anchorPoint.getY(),
1389 position.getTextAnchor(),
1390 position.getAngle(), position.getRotationAnchor());
1391 }
1392
1393 }
1394
1395 /**
1396 * Returns an independent copy of the renderer. The <code>plot</code>
1397 * reference is shallow copied.
1398 *
1399 * @return A clone.
1400 *
1401 * @throws CloneNotSupportedException can be thrown if one of the objects
1402 * belonging to the renderer does not support cloning (for example,
1403 * an item label generator).
1404 */
1405 public Object clone() throws CloneNotSupportedException {
1406
1407 AbstractCategoryItemRenderer clone
1408 = (AbstractCategoryItemRenderer) super.clone();
1409
1410 if (this.itemLabelGenerator != null) {
1411 if (this.itemLabelGenerator instanceof PublicCloneable) {
1412 PublicCloneable pc = (PublicCloneable) this.itemLabelGenerator;
1413 clone.itemLabelGenerator
1414 = (CategoryItemLabelGenerator) pc.clone();
1415 }
1416 else {
1417 throw new CloneNotSupportedException(
1418 "ItemLabelGenerator not cloneable.");
1419 }
1420 }
1421
1422 if (this.itemLabelGeneratorList != null) {
1423 clone.itemLabelGeneratorList
1424 = (ObjectList) this.itemLabelGeneratorList.clone();
1425 }
1426
1427 if (this.baseItemLabelGenerator != null) {
1428 if (this.baseItemLabelGenerator instanceof PublicCloneable) {
1429 PublicCloneable pc
1430 = (PublicCloneable) this.baseItemLabelGenerator;
1431 clone.baseItemLabelGenerator
1432 = (CategoryItemLabelGenerator) pc.clone();
1433 }
1434 else {
1435 throw new CloneNotSupportedException(
1436 "ItemLabelGenerator not cloneable.");
1437 }
1438 }
1439
1440 if (this.toolTipGenerator != null) {
1441 if (this.toolTipGenerator instanceof PublicCloneable) {
1442 PublicCloneable pc = (PublicCloneable) this.toolTipGenerator;
1443 clone.toolTipGenerator = (CategoryToolTipGenerator) pc.clone();
1444 }
1445 else {
1446 throw new CloneNotSupportedException(
1447 "Tool tip generator not cloneable.");
1448 }
1449 }
1450
1451 if (this.toolTipGeneratorList != null) {
1452 clone.toolTipGeneratorList
1453 = (ObjectList) this.toolTipGeneratorList.clone();
1454 }
1455
1456 if (this.baseToolTipGenerator != null) {
1457 if (this.baseToolTipGenerator instanceof PublicCloneable) {
1458 PublicCloneable pc
1459 = (PublicCloneable) this.baseToolTipGenerator;
1460 clone.baseToolTipGenerator
1461 = (CategoryToolTipGenerator) pc.clone();
1462 }
1463 else {
1464 throw new CloneNotSupportedException(
1465 "Base tool tip generator not cloneable.");
1466 }
1467 }
1468
1469 if (this.itemURLGenerator != null) {
1470 if (this.itemURLGenerator instanceof PublicCloneable) {
1471 PublicCloneable pc = (PublicCloneable) this.itemURLGenerator;
1472 clone.itemURLGenerator = (CategoryURLGenerator) pc.clone();
1473 }
1474 else {
1475 throw new CloneNotSupportedException(
1476 "Item URL generator not cloneable.");
1477 }
1478 }
1479
1480 if (this.itemURLGeneratorList != null) {
1481 clone.itemURLGeneratorList
1482 = (ObjectList) this.itemURLGeneratorList.clone();
1483 }
1484
1485 if (this.baseItemURLGenerator != null) {
1486 if (this.baseItemURLGenerator instanceof PublicCloneable) {
1487 PublicCloneable pc
1488 = (PublicCloneable) this.baseItemURLGenerator;
1489 clone.baseItemURLGenerator = (CategoryURLGenerator) pc.clone();
1490 }
1491 else {
1492 throw new CloneNotSupportedException(
1493 "Base item URL generator not cloneable.");
1494 }
1495 }
1496
1497 if (this.legendItemLabelGenerator instanceof PublicCloneable) {
1498 clone.legendItemLabelGenerator = (CategorySeriesLabelGenerator)
1499 ObjectUtilities.clone(this.legendItemLabelGenerator);
1500 }
1501 if (this.legendItemToolTipGenerator instanceof PublicCloneable) {
1502 clone.legendItemToolTipGenerator = (CategorySeriesLabelGenerator)
1503 ObjectUtilities.clone(this.legendItemToolTipGenerator);
1504 }
1505 if (this.legendItemURLGenerator instanceof PublicCloneable) {
1506 clone.legendItemURLGenerator = (CategorySeriesLabelGenerator)
1507 ObjectUtilities.clone(this.legendItemURLGenerator);
1508 }
1509 return clone;
1510 }
1511
1512 /**
1513 * Returns a domain axis for a plot.
1514 *
1515 * @param plot the plot.
1516 * @param index the axis index.
1517 *
1518 * @return A domain axis.
1519 */
1520 protected CategoryAxis getDomainAxis(CategoryPlot plot, int index) {
1521 CategoryAxis result = plot.getDomainAxis(index);
1522 if (result == null) {
1523 result = plot.getDomainAxis();
1524 }
1525 return result;
1526 }
1527
1528 /**
1529 * Returns a range axis for a plot.
1530 *
1531 * @param plot the plot.
1532 * @param index the axis index.
1533 *
1534 * @return A range axis.
1535 */
1536 protected ValueAxis getRangeAxis(CategoryPlot plot, int index) {
1537 ValueAxis result = plot.getRangeAxis(index);
1538 if (result == null) {
1539 result = plot.getRangeAxis();
1540 }
1541 return result;
1542 }
1543
1544 /**
1545 * Returns a (possibly empty) collection of legend items for the series
1546 * that this renderer is responsible for drawing.
1547 *
1548 * @return The legend item collection (never <code>null</code>).
1549 *
1550 * @see #getLegendItem(int, int)
1551 */
1552 public LegendItemCollection getLegendItems() {
1553 if (this.plot == null) {
1554 return new LegendItemCollection();
1555 }
1556 LegendItemCollection result = new LegendItemCollection();
1557 int index = this.plot.getIndexOf(this);
1558 CategoryDataset dataset = this.plot.getDataset(index);
1559 if (dataset != null) {
1560 int seriesCount = dataset.getRowCount();
1561 for (int i = 0; i < seriesCount; i++) {
1562 if (isSeriesVisibleInLegend(i)) {
1563 LegendItem item = getLegendItem(index, i);
1564 if (item != null) {
1565 result.add(item);
1566 }
1567 }
1568 }
1569
1570 }
1571 return result;
1572 }
1573
1574 /**
1575 * Returns the legend item label generator.
1576 *
1577 * @return The label generator (never <code>null</code>).
1578 *
1579 * @see #setLegendItemLabelGenerator(CategorySeriesLabelGenerator)
1580 */
1581 public CategorySeriesLabelGenerator getLegendItemLabelGenerator() {
1582 return this.legendItemLabelGenerator;
1583 }
1584
1585 /**
1586 * Sets the legend item label generator and sends a
1587 * {@link RendererChangeEvent} to all registered listeners.
1588 *
1589 * @param generator the generator (<code>null</code> not permitted).
1590 *
1591 * @see #getLegendItemLabelGenerator()
1592 */
1593 public void setLegendItemLabelGenerator(
1594 CategorySeriesLabelGenerator generator) {
1595 if (generator == null) {
1596 throw new IllegalArgumentException("Null 'generator' argument.");
1597 }
1598 this.legendItemLabelGenerator = generator;
1599 fireChangeEvent();
1600 }
1601
1602 /**
1603 * Returns the legend item tool tip generator.
1604 *
1605 * @return The tool tip generator (possibly <code>null</code>).
1606 *
1607 * @see #setLegendItemToolTipGenerator(CategorySeriesLabelGenerator)
1608 */
1609 public CategorySeriesLabelGenerator getLegendItemToolTipGenerator() {
1610 return this.legendItemToolTipGenerator;
1611 }
1612
1613 /**
1614 * Sets the legend item tool tip generator and sends a
1615 * {@link RendererChangeEvent} to all registered listeners.
1616 *
1617 * @param generator the generator (<code>null</code> permitted).
1618 *
1619 * @see #setLegendItemToolTipGenerator(CategorySeriesLabelGenerator)
1620 */
1621 public void setLegendItemToolTipGenerator(
1622 CategorySeriesLabelGenerator generator) {
1623 this.legendItemToolTipGenerator = generator;
1624 fireChangeEvent();
1625 }
1626
1627 /**
1628 * Returns the legend item URL generator.
1629 *
1630 * @return The URL generator (possibly <code>null</code>).
1631 *
1632 * @see #setLegendItemURLGenerator(CategorySeriesLabelGenerator)
1633 */
1634 public CategorySeriesLabelGenerator getLegendItemURLGenerator() {
1635 return this.legendItemURLGenerator;
1636 }
1637
1638 /**
1639 * Sets the legend item URL generator and sends a
1640 * {@link RendererChangeEvent} to all registered listeners.
1641 *
1642 * @param generator the generator (<code>null</code> permitted).
1643 *
1644 * @see #getLegendItemURLGenerator()
1645 */
1646 public void setLegendItemURLGenerator(
1647 CategorySeriesLabelGenerator generator) {
1648 this.legendItemURLGenerator = generator;
1649 fireChangeEvent();
1650 }
1651
1652 /**
1653 * Adds an entity with the specified hotspot.
1654 *
1655 * @param entities the entity collection.
1656 * @param dataset the dataset.
1657 * @param row the row index.
1658 * @param column the column index.
1659 * @param hotspot the hotspot.
1660 */
1661 protected void addItemEntity(EntityCollection entities,
1662 CategoryDataset dataset, int row, int column,
1663 Shape hotspot) {
1664
1665 String tip = null;
1666 CategoryToolTipGenerator tipster = getToolTipGenerator(row, column);
1667 if (tipster != null) {
1668 tip = tipster.generateToolTip(dataset, row, column);
1669 }
1670 String url = null;
1671 CategoryURLGenerator urlster = getItemURLGenerator(row, column);
1672 if (urlster != null) {
1673 url = urlster.generateURL(dataset, row, column);
1674 }
1675 CategoryItemEntity entity = new CategoryItemEntity(hotspot, tip, url,
1676 dataset, dataset.getRowKey(row), dataset.getColumnKey(column));
1677 entities.add(entity);
1678
1679 }
1680
1681 }