KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > AbstractXYItemRenderer


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------------------
23  * AbstractXYItemRenderer.java
24  * ---------------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Richard Atkinson;
29  * Focus Computer Services Limited;
30  *
31  * $Id: AbstractXYItemRenderer.java,v 1.26 2003/11/17 09:30:06 mungady Exp $
32  *
33  * Changes:
34  * --------
35  * 15-Mar-2002 : Version 1 (DG);
36  * 09-Apr-2002 : Added a getToolTipGenerator() method reflecting the change in the XYItemRenderer
37  * interface (DG);
38  * 05-Aug-2002 : Added a urlGenerator member variable to support HTML image maps (RA);
39  * 20-Aug-2002 : Added property change events for the tooltip and URL generators (DG);
40  * 22-Aug-2002 : Moved property change support into AbstractRenderer class (DG);
41  * 23-Sep-2002 : Fixed errors reported by Checkstyle tool (DG);
42  * 18-Nov-2002 : Added methods for drawing grid lines (DG);
43  * 17-Jan-2003 : Moved plot classes into a separate package (DG);
44  * 25-Mar-2003 : Implemented Serializable (DG);
45  * 01-May-2003 : Modified initialise(...) return type and drawItem(...) method signature (DG);
46  * 15-May-2003 : Modified to take into account the plot orientation (DG);
47  * 21-May-2003 : Added labels to markers (DG);
48  * 05-Jun-2003 : Added domain and range grid bands (sponsored by Focus Computer Services Ltd) (DG);
49  * 27-Jul-2003 : Added getRangeType() to support stacked XY area charts (RA);
50  * 31-Jul-2003 : Deprecated all but the default constructor (DG);
51  * 13-Aug-2003 : Implemented Cloneable (DG);
52  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
53  * 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
54  * 05-Nov-2003 : Fixed marker rendering bug (833623) (DG);
55  *
56  */

57
58 package org.jfree.chart.renderer;
59
60 import java.awt.Font JavaDoc;
61 import java.awt.FontMetrics JavaDoc;
62 import java.awt.Graphics2D JavaDoc;
63 import java.awt.Paint JavaDoc;
64 import java.awt.Shape JavaDoc;
65 import java.awt.Stroke JavaDoc;
66 import java.awt.font.FontRenderContext JavaDoc;
67 import java.awt.font.LineMetrics JavaDoc;
68 import java.awt.geom.Line2D JavaDoc;
69 import java.awt.geom.Rectangle2D JavaDoc;
70 import java.io.Serializable JavaDoc;
71
72 import org.jfree.chart.LegendItem;
73 import org.jfree.chart.Marker;
74 import org.jfree.chart.MarkerLabelPosition;
75 import org.jfree.chart.axis.ValueAxis;
76 import org.jfree.chart.labels.XYToolTipGenerator;
77 import org.jfree.chart.plot.DrawingSupplier;
78 import org.jfree.chart.plot.Plot;
79 import org.jfree.chart.plot.PlotOrientation;
80 import org.jfree.chart.plot.PlotRenderingInfo;
81 import org.jfree.chart.plot.XYPlot;
82 import org.jfree.chart.urls.XYURLGenerator;
83 import org.jfree.data.Range;
84 import org.jfree.data.XYDataset;
85 import org.jfree.util.ObjectUtils;
86 import org.jfree.util.PublicCloneable;
87
88 /**
89  * A base class that can be used to create new {@link XYItemRenderer} implementations.
90  *
91  * @author David Gilbert
92  */

93 public abstract class AbstractXYItemRenderer extends AbstractRenderer implements XYItemRenderer,
94                                                                                  Cloneable JavaDoc,
95                                                                                  Serializable JavaDoc {
96     
97     /** The plot. */
98     private XYPlot plot;
99     
100     /** The tool tip generator. */
101     private XYToolTipGenerator toolTipGenerator;
102
103     /** The URL text generator. */
104     private XYURLGenerator urlGenerator;
105
106     /**
107      * Default constructor.
108      */

109     protected AbstractXYItemRenderer() {
110         this.toolTipGenerator = null;
111         this.urlGenerator = null;
112     }
113
114     /**
115      * Creates a renderer with the specified tooltip generator.
116      * <P>
117      * Storage is allocated for property change listeners.
118      *
119      * @param toolTipGenerator the tooltip generator (<code>null</code> permitted).
120      *
121      * @deprecated Use default constructor then set tooltip generator.
122      */

123     protected AbstractXYItemRenderer(XYToolTipGenerator toolTipGenerator) {
124         this(toolTipGenerator, null);
125     }
126
127     /**
128      * Creates a renderer with the specified tooltip generator.
129      * <P>
130      * Storage is allocated for property change listeners.
131      *
132      * @param urlGenerator the URL generator (<code>null</code> permitted).
133      *
134      * @deprecated Use default constructor then set URL generator.
135      */

136     protected AbstractXYItemRenderer(XYURLGenerator urlGenerator) {
137         this(null, urlGenerator);
138     }
139
140     /**
141      * Creates a renderer with the specified tooltip generator and URL generator.
142      * <P>
143      * Storage is allocated for property change listeners.
144      *
145      * @param toolTipGenerator the tooltip generator (<code>null</code> permitted).
146      * @param urlGenerator the URL generator (<code>null</code> permitted).
147      *
148      * @deprecated Use default constructor then set URL generator.
149      */

150     protected AbstractXYItemRenderer(XYToolTipGenerator toolTipGenerator,
151                                      XYURLGenerator urlGenerator) {
152
153         this.toolTipGenerator = toolTipGenerator;
154         this.urlGenerator = urlGenerator;
155
156     }
157
158     /**
159      * Returns the number of passes through the data that the renderer requires in order to
160      * draw the chart. Most charts will require a single pass, but some require two passes.
161      *
162      * @return The pass count.
163      */

164     public int getPassCount() {
165         return 1;
166     }
167     
168     /**
169      * Returns the plot that the renderer is assigned to.
170      *
171      * @return The plot.
172      */

173     public XYPlot getPlot() {
174         return this.plot;
175     }
176     
177     /**
178      * Sets the plot that the renderer is assigned to.
179      *
180      * @param plot the plot.
181      */

182     public void setPlot(XYPlot plot) {
183         this.plot = plot;
184     }
185     
186     /**
187      * Initialises the renderer.
188      * <P>
189      * This method will be called before the first item is rendered, giving the
190      * renderer an opportunity to initialise any state information it wants to maintain.
191      * The renderer can do nothing if it chooses.
192      *
193      * @param g2 the graphics device.
194      * @param dataArea the area inside the axes.
195      * @param plot the plot.
196      * @param data the data.
197      * @param info an optional info collection object to return data back to the caller.
198      *
199      * @return The number of passes required by the renderer.
200      */

201     public XYItemRendererState initialise(Graphics2D JavaDoc g2,
202                                           Rectangle2D JavaDoc dataArea,
203                                           XYPlot plot,
204                                           XYDataset data,
205                                           PlotRenderingInfo info) {
206
207         XYItemRendererState state = new XYItemRendererState(info);
208         return state;
209
210     }
211
212     /**
213      * Returns the tool tip generator.
214      *
215      * @return the tool tip generator (possibly <code>null</code>).
216      */

217     public XYToolTipGenerator getToolTipGenerator() {
218         return this.toolTipGenerator;
219     }
220
221     /**
222      * Sets the tool tip generator.
223      *
224      * @param generator the tool tip generator (<code>null</code> permitted).
225      */

226     public void setToolTipGenerator(XYToolTipGenerator generator) {
227
228         Object JavaDoc oldValue = this.toolTipGenerator;
229         this.toolTipGenerator = generator;
230         firePropertyChanged("renderer.ToolTipGenerator", oldValue, generator);
231
232     }
233
234     /**
235      * Returns the URL generator for HTML image maps.
236      *
237      * @return the URL generator (possibly <code>null</code>).
238      */

239     public XYURLGenerator getURLGenerator() {
240         return this.urlGenerator;
241     }
242
243     /**
244      * Sets the URL generator for HTML image maps.
245      *
246      * @param urlGenerator the URL generator (<code>null</code> permitted).
247      */

248     public void setURLGenerator(XYURLGenerator urlGenerator) {
249
250         Object JavaDoc oldValue = this.urlGenerator;
251         this.urlGenerator = urlGenerator;
252         firePropertyChanged("renderer.URLGenerator", oldValue, urlGenerator);
253
254     }
255
256     /**
257      * Returns the range type for the renderer.
258      * <p>
259      * The default implementation returns <code>STANDARD</code>, subclasses may override this
260      * behaviour.
261      * <p>
262      * The {@link org.jfree.chart.plot.XYPlot} uses this information when auto-calculating
263      * the range for the axis.
264      *
265      * @return the range type.
266      */

267     public RangeType getRangeType() {
268         return RangeType.STANDARD;
269     }
270
271     /**
272      * Returns a legend item for a series.
273      *
274      * @param datasetIndex the dataset index (zero-based).
275      * @param series the series index (zero-based).
276      *
277      * @return a legend item for the series.
278      */

279     public LegendItem getLegendItem(int datasetIndex, int series) {
280
281         LegendItem result = null;
282
283         XYPlot plot = getPlot();
284         if (plot != null) {
285             XYDataset dataset;
286             if (datasetIndex == 0) {
287                 dataset = plot.getDataset();
288             }
289             else {
290                 dataset = plot.getSecondaryDataset(datasetIndex - 1);
291             }
292
293             if (dataset != null) {
294                 String JavaDoc label = dataset.getSeriesName(series);
295                 String JavaDoc description = label;
296                 Shape JavaDoc shape = getSeriesShape(series);
297                 Paint JavaDoc paint = getSeriesPaint(series);
298                 Paint JavaDoc outlinePaint = getSeriesOutlinePaint(series);
299                 Stroke JavaDoc stroke = getSeriesStroke(series);
300
301                 result = new LegendItem(label, description,
302                                         shape, paint, outlinePaint, stroke);
303             }
304
305         }
306
307         return result;
308
309     }
310
311     /**
312      * Fills a band between two values on the axis. This can be used to color bands between the
313      * grid lines.
314      *
315      * @param g2 the graphics device.
316      * @param plot the plot.
317      * @param axis the domain axis.
318      * @param dataArea the data area.
319      * @param start the start value.
320      * @param end the end value.
321      */

322     public void fillDomainGridBand(Graphics2D JavaDoc g2,
323                                    XYPlot plot,
324                                    ValueAxis axis,
325                                    Rectangle2D JavaDoc dataArea,
326                                    double start, double end) {
327
328         double x1 = axis.translateValueToJava2D(start, dataArea, plot.getDomainAxisEdge());
329         double x2 = axis.translateValueToJava2D(end, dataArea, plot.getDomainAxisEdge());
330         // need to change the next line to take account of plot orientation...
331
Rectangle2D JavaDoc band = new Rectangle2D.Double JavaDoc(x1, dataArea.getMinY(),
332                                                   x2 - x1, dataArea.getMaxY() - dataArea.getMinY());
333         Paint JavaDoc paint = plot.getDomainTickBandPaint();
334
335         if (paint != null) {
336             g2.setPaint(paint);
337             g2.fill(band);
338         }
339
340     }
341
342     /**
343      * Fills a band between two values on the range axis. This can be used to color bands between
344      * the grid lines.
345      *
346      * @param g2 the graphics device.
347      * @param plot the plot.
348      * @param axis the range axis.
349      * @param dataArea the data area.
350      * @param start the start value.
351      * @param end the end value.
352      */

353     public void fillRangeGridBand(Graphics2D JavaDoc g2,
354                                   XYPlot plot,
355                                   ValueAxis axis,
356                                   Rectangle2D JavaDoc dataArea,
357                                   double start, double end) {
358
359         double y1 = axis.translateValueToJava2D(start, dataArea, plot.getRangeAxisEdge());
360         double y2 = axis.translateValueToJava2D(end, dataArea, plot.getRangeAxisEdge());
361         // need to change the next line to take account of the plot orientation
362
Rectangle2D JavaDoc band = new Rectangle2D.Double JavaDoc(dataArea.getMinX(), y2,
363                                                   dataArea.getWidth(), y1 - y2);
364         Paint JavaDoc paint = plot.getRangeTickBandPaint();
365
366         if (paint != null) {
367             g2.setPaint(paint);
368             g2.fill(band);
369         }
370
371     }
372
373     /**
374      * Draws a grid line against the range axis.
375      *
376      * @param g2 the graphics device.
377      * @param plot the plot.
378      * @param axis the value axis.
379      * @param dataArea the area for plotting data (not yet adjusted for any 3D effect).
380      * @param value the value at which the grid line should be drawn.
381      *
382      */

383     public void drawDomainGridLine(Graphics2D JavaDoc g2,
384                                    XYPlot plot,
385                                    ValueAxis axis,
386                                    Rectangle2D JavaDoc dataArea,
387                                    double value) {
388
389         Range range = axis.getRange();
390         if (!range.contains(value)) {
391             return;
392         }
393
394         PlotOrientation orientation = plot.getOrientation();
395         double v = axis.translateValueToJava2D(value, dataArea, plot.getDomainAxisEdge());
396         Line2D JavaDoc line = null;
397         if (orientation == PlotOrientation.HORIZONTAL) {
398             line = new Line2D.Double JavaDoc(dataArea.getMinX(), v, dataArea.getMaxX(), v);
399         }
400         else if (orientation == PlotOrientation.VERTICAL) {
401             line = new Line2D.Double JavaDoc(v, dataArea.getMinY(), v, dataArea.getMaxY());
402         }
403
404         Paint JavaDoc paint = plot.getDomainGridlinePaint();
405         Stroke JavaDoc stroke = plot.getDomainGridlineStroke();
406         g2.setPaint(paint != null ? paint : Plot.DEFAULT_OUTLINE_PAINT);
407         g2.setStroke(stroke != null ? stroke : Plot.DEFAULT_OUTLINE_STROKE);
408         g2.draw(line);
409
410     }
411
412     /**
413      * Draws a grid line against the range axis.
414      *
415      * @param g2 the graphics device.
416      * @param plot the plot.
417      * @param axis the value axis.
418      * @param dataArea the area for plotting data (not yet adjusted for any 3D effect).
419      * @param value the value at which the grid line should be drawn.
420      *
421      */

422     public void drawRangeGridLine(Graphics2D JavaDoc g2,
423                                   XYPlot plot,
424                                   ValueAxis axis,
425                                   Rectangle2D JavaDoc dataArea,
426                                   double value) {
427
428         Range range = axis.getRange();
429         if (!range.contains(value)) {
430             return;
431         }
432
433         PlotOrientation orientation = plot.getOrientation();
434         Line2D JavaDoc line = null;
435         double v = axis.translateValueToJava2D(value, dataArea, plot.getRangeAxisEdge());
436         if (orientation == PlotOrientation.HORIZONTAL) {
437             line = new Line2D.Double JavaDoc(v, dataArea.getMinY(), v, dataArea.getMaxY());
438         }
439         else if (orientation == PlotOrientation.VERTICAL) {
440             line = new Line2D.Double JavaDoc(dataArea.getMinX(), v, dataArea.getMaxX(), v);
441         }
442
443         Paint JavaDoc paint = plot.getRangeGridlinePaint();
444         Stroke JavaDoc stroke = plot.getRangeGridlineStroke();
445         g2.setPaint(paint != null ? paint : Plot.DEFAULT_OUTLINE_PAINT);
446         g2.setStroke(stroke != null ? stroke : Plot.DEFAULT_OUTLINE_STROKE);
447         g2.draw(line);
448
449     }
450
451     /**
452      * Draws a vertical line on the chart to represent a 'range marker'.
453      *
454      * @param g2 the graphics device.
455      * @param plot the plot.
456      * @param domainAxis the domain axis.
457      * @param marker the marker line.
458      * @param dataArea the axis data area.
459      */

460     public void drawDomainMarker(Graphics2D JavaDoc g2,
461                                  XYPlot plot,
462                                  ValueAxis domainAxis,
463                                  Marker marker,
464                                  Rectangle2D JavaDoc dataArea) {
465
466         double value = marker.getValue();
467         Range range = domainAxis.getRange();
468         if (!range.contains(value)) {
469             return;
470         }
471
472         double v = domainAxis.translateValueToJava2D(marker.getValue(), dataArea,
473                                                      plot.getDomainAxisEdge());
474                                                      
475         PlotOrientation orientation = plot.getOrientation();
476         Line2D JavaDoc line = null;
477         if (orientation == PlotOrientation.HORIZONTAL) {
478             line = new Line2D.Double JavaDoc(dataArea.getMinX(), v, dataArea.getMaxX(), v);
479         }
480         else if (orientation == PlotOrientation.VERTICAL) {
481             line = new Line2D.Double JavaDoc(v, dataArea.getMinY(), v, dataArea.getMaxY());
482         }
483         Paint JavaDoc paint = marker.getOutlinePaint();
484         Stroke JavaDoc stroke = marker.getOutlineStroke();
485         g2.setPaint(paint != null ? paint : Plot.DEFAULT_OUTLINE_PAINT);
486         g2.setStroke(stroke != null ? stroke : Plot.DEFAULT_OUTLINE_STROKE);
487         g2.draw(line);
488
489         String JavaDoc label = marker.getLabel();
490         MarkerLabelPosition position = marker.getLabelPosition();
491         if (label != null) {
492             Font JavaDoc labelFont = marker.getLabelFont();
493             g2.setFont(labelFont);
494             g2.setPaint(marker.getLabelPaint());
495             double[] coordinates = calculateDomainMarkerTextPosition(g2, orientation, dataArea,
496                                                                      v, label, labelFont,
497                                                                position);
498             g2.drawString(label, (int) coordinates[0], (int) coordinates[1]);
499         }
500
501     }
502
503     /**
504      * Calculates the (x, y) coordinates for drawing a marker label.
505      *
506      * @param g2 the graphics device.
507      * @param orientation the plot orientation.
508      * @param dataArea the data area.
509      * @param coordinate the range value (converted to Java 2D space).
510      * @param label the label.
511      * @param font the font.
512      * @param position the label position.
513      *
514      * @return the coordinates for drawing the marker label.
515      */

516     private double[] calculateDomainMarkerTextPosition(Graphics2D JavaDoc g2,
517                                                        PlotOrientation orientation,
518                                                        Rectangle2D JavaDoc dataArea,
519                                                        double coordinate,
520                                                        String JavaDoc label,
521                                                        Font JavaDoc font,
522                                                        MarkerLabelPosition position) {
523                                                      
524         double[] result = new double[2];
525         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
526         FontMetrics JavaDoc fm = g2.getFontMetrics();
527         LineMetrics JavaDoc metrics = font.getLineMetrics(label, frc);
528         Rectangle2D JavaDoc bounds = fm.getStringBounds(label, g2);
529         if (orientation == PlotOrientation.HORIZONTAL) {
530             if (position == MarkerLabelPosition.TOP_LEFT) {
531                 result[0] = dataArea.getMinX() + 2.0;
532                 result[1] = coordinate - metrics.getDescent() - metrics.getLeading();
533             }
534             else if (position == MarkerLabelPosition.TOP_RIGHT) {
535                 result[0] = dataArea.getMaxX() - bounds.getWidth() - 2.0;
536                 result[1] = coordinate - metrics.getDescent() - metrics.getLeading();
537             }
538             else if (position == MarkerLabelPosition.BOTTOM_LEFT) {
539                 result[0] = dataArea.getMinX() + 2.0;
540                 result[1] = coordinate + bounds.getHeight();
541             }
542             else if (position == MarkerLabelPosition.BOTTOM_RIGHT) {
543                 result[0] = dataArea.getMaxX() - bounds.getWidth() - 2.0;
544                 result[1] = coordinate + bounds.getHeight();
545             }
546         }
547         else if (orientation == PlotOrientation.VERTICAL) {
548             if (position == MarkerLabelPosition.TOP_LEFT) {
549                 result[0] = coordinate - bounds.getWidth() - 2.0;
550                 result[1] = dataArea.getMinY() + bounds.getHeight();
551             }
552             else if (position == MarkerLabelPosition.TOP_RIGHT) {
553                 result[0] = coordinate + 2.0;
554                 result[1] = dataArea.getMinY() + bounds.getHeight();
555             }
556             else if (position == MarkerLabelPosition.BOTTOM_LEFT) {
557                 result[0] = coordinate - bounds.getWidth() - 2.0;
558                 result[1] = dataArea.getMaxY() - metrics.getDescent() - metrics.getLeading();
559             }
560             else if (position == MarkerLabelPosition.BOTTOM_RIGHT) {
561                 result[0] = coordinate + 2.0;
562                 result[1] = dataArea.getMaxY() - metrics.getDescent() - metrics.getLeading();
563             }
564         }
565         return result;
566         
567     }
568     
569     /**
570      * Draws a horizontal line across the chart to represent a 'range marker'.
571      *
572      * @param g2 the graphics device.
573      * @param plot the plot.
574      * @param rangeAxis the range axis.
575      * @param marker the marker line.
576      * @param dataArea the axis data area.
577      */

578     public void drawRangeMarker(Graphics2D JavaDoc g2,
579                                 XYPlot plot,
580                                 ValueAxis rangeAxis,
581                                 Marker marker,
582                                 Rectangle2D JavaDoc dataArea) {
583
584         double value = marker.getValue();
585         Range range = rangeAxis.getRange();
586         if (!range.contains(value)) {
587             return;
588         }
589
590         double v = rangeAxis.translateValueToJava2D(marker.getValue(), dataArea,
591                                                     plot.getRangeAxisEdge());
592         PlotOrientation orientation = plot.getOrientation();
593         Line2D JavaDoc line = null;
594         if (orientation == PlotOrientation.HORIZONTAL) {
595             line = new Line2D.Double JavaDoc(v, dataArea.getMinY(), v, dataArea.getMaxY());
596         }
597         else if (orientation == PlotOrientation.VERTICAL) {
598             line = new Line2D.Double JavaDoc(dataArea.getMinX(), v, dataArea.getMaxX(), v);
599         }
600         Paint JavaDoc paint = marker.getOutlinePaint();
601         Stroke JavaDoc stroke = marker.getOutlineStroke();
602         g2.setPaint(paint != null ? paint : Plot.DEFAULT_OUTLINE_PAINT);
603         g2.setStroke(stroke != null ? stroke : Plot.DEFAULT_OUTLINE_STROKE);
604         g2.draw(line);
605
606         String JavaDoc label = marker.getLabel();
607         MarkerLabelPosition position = marker.getLabelPosition();
608         if (label != null) {
609             Font JavaDoc labelFont = marker.getLabelFont();
610             g2.setFont(labelFont);
611             g2.setPaint(marker.getLabelPaint());
612             double[] coordinates = calculateRangeMarkerTextPosition(g2, orientation, dataArea,
613                                                                     v, label, labelFont,
614                                                                position);
615             g2.drawString(label, (int) coordinates[0], (int) coordinates[1]);
616         }
617
618     }
619
620     /**
621      * Calculates the (x, y) coordinates for drawing a marker label.
622      *
623      * @param g2 the graphics device.
624      * @param orientation the plot orientation.
625      * @param dataArea the data area.
626      * @param coordinate the range value (converted to Java 2D space).
627      * @param label the label.
628      * @param font the font.
629      * @param position the label position.
630      *
631      * @return the coordinates for drawing the marker label.
632      */

633     private double[] calculateRangeMarkerTextPosition(Graphics2D JavaDoc g2,
634                                                       PlotOrientation orientation,
635                                                       Rectangle2D JavaDoc dataArea,
636                                                       double coordinate,
637                                                       String JavaDoc label,
638                                                       Font JavaDoc font,
639                                                       MarkerLabelPosition position) {
640                                                      
641         double[] result = new double[2];
642         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
643         FontMetrics JavaDoc fm = g2.getFontMetrics();
644         LineMetrics JavaDoc metrics = font.getLineMetrics(label, frc);
645         Rectangle2D JavaDoc bounds = fm.getStringBounds(label, g2);
646         if (orientation == PlotOrientation.HORIZONTAL) {
647             if (position == MarkerLabelPosition.TOP_LEFT) {
648                 result[0] = coordinate - bounds.getWidth() - 2.0;
649                 result[1] = dataArea.getMinY() + bounds.getHeight();
650             }
651             else if (position == MarkerLabelPosition.TOP_RIGHT) {
652                 result[0] = coordinate + 2.0;
653                 result[1] = dataArea.getMinY() + bounds.getHeight();
654             }
655             else if (position == MarkerLabelPosition.BOTTOM_LEFT) {
656                 result[0] = coordinate - bounds.getWidth() - 2.0;
657                 result[1] = dataArea.getMaxY() - metrics.getDescent() - metrics.getLeading();
658             }
659             else if (position == MarkerLabelPosition.BOTTOM_RIGHT) {
660                 result[0] = coordinate + 2.0;
661                 result[1] = dataArea.getMaxY() - metrics.getDescent() - metrics.getLeading();
662             }
663         }
664         else if (orientation == PlotOrientation.VERTICAL) {
665             if (position == MarkerLabelPosition.TOP_LEFT) {
666                 result[0] = dataArea.getMinX() + 2.0;
667                 result[1] = coordinate - metrics.getDescent() - metrics.getLeading();
668             }
669             else if (position == MarkerLabelPosition.TOP_RIGHT) {
670                 result[0] = dataArea.getMaxX() - bounds.getWidth() - 2.0;
671                 result[1] = coordinate - metrics.getDescent() - metrics.getLeading();
672             }
673             else if (position == MarkerLabelPosition.BOTTOM_LEFT) {
674                 result[0] = dataArea.getMinX() + 2.0;
675                 result[1] = coordinate + bounds.getHeight();
676             }
677             else if (position == MarkerLabelPosition.BOTTOM_RIGHT) {
678                 result[0] = dataArea.getMaxX() - bounds.getWidth() - 2.0;
679                 result[1] = coordinate + bounds.getHeight();
680             }
681         }
682         return result;
683         
684     }
685     
686     /**
687      * Returns a clone of the renderer.
688      *
689      * @return A clone.
690      *
691      * @throws CloneNotSupportedException if the renderer does not support cloning.
692      */

693     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
694         AbstractXYItemRenderer clone = (AbstractXYItemRenderer) super.clone();
695         // 'plot' : just retain reference, not a deep copy
696
if (this.toolTipGenerator != null && this.toolTipGenerator instanceof PublicCloneable) {
697             PublicCloneable pc = (PublicCloneable) this.toolTipGenerator;
698             clone.toolTipGenerator = (XYToolTipGenerator) pc.clone();
699         }
700         return clone;
701     }
702     
703     /**
704      * Tests this renderer for equality with another object.
705      *
706      * @param obj the object.
707      *
708      * @return <code>true</code> or <code>false</code>.
709      */

710     public boolean equals(Object JavaDoc obj) {
711
712         if (obj == null) {
713             return false;
714         }
715
716         if (obj == this) {
717             return true;
718         }
719
720         if (obj instanceof AbstractXYItemRenderer) {
721             AbstractXYItemRenderer renderer = (AbstractXYItemRenderer) obj;
722             if (super.equals(obj)) {
723                 boolean b0 = ObjectUtils.equal(this.toolTipGenerator, renderer.toolTipGenerator);
724                 boolean b1 = ObjectUtils.equal(this.urlGenerator, renderer.urlGenerator);
725                 return b0 && b1;
726             }
727             else {
728                 return false;
729             }
730         }
731
732         return false;
733
734     }
735     
736     /**
737      * Returns the drawing supplier from the plot.
738      *
739      * @return The drawing supplier (possibly <code>null</code>).
740      */

741     public DrawingSupplier getDrawingSupplier() {
742         DrawingSupplier result = null;
743         XYPlot p = getPlot();
744         if (p != null) {
745             result = p.getDrawingSupplier();
746         }
747         return result;
748     }
749
750 }
751
Popular Tags