001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ----------------------
028 * XYTitleAnnotation.java
029 * ----------------------
030 * (C) Copyright 2007-2022, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Andrew Mickish;
034 *                   Peter Kolb (patch 2809117);
035 *
036 */
037
038package org.jfree.chart.annotations;
039
040import java.awt.Graphics2D;
041import java.awt.geom.Point2D;
042import java.awt.geom.Rectangle2D;
043import java.io.Serializable;
044import java.util.Objects;
045
046import org.jfree.chart.internal.HashUtils;
047import org.jfree.chart.axis.AxisLocation;
048import org.jfree.chart.axis.ValueAxis;
049import org.jfree.chart.block.BlockParams;
050import org.jfree.chart.block.EntityBlockResult;
051import org.jfree.chart.block.RectangleConstraint;
052import org.jfree.chart.event.AnnotationChangeEvent;
053import org.jfree.chart.plot.Plot;
054import org.jfree.chart.plot.PlotOrientation;
055import org.jfree.chart.plot.PlotRenderingInfo;
056import org.jfree.chart.plot.XYPlot;
057import org.jfree.chart.title.Title;
058import org.jfree.chart.api.RectangleAnchor;
059import org.jfree.chart.api.RectangleEdge;
060import org.jfree.chart.block.Size2D;
061import org.jfree.chart.internal.Args;
062import org.jfree.chart.api.PublicCloneable;
063import org.jfree.chart.api.XYCoordinateType;
064import org.jfree.data.Range;
065
066/**
067 * An annotation that allows any {@link Title} to be placed at a location on
068 * an {@link XYPlot}.
069 */
070public class XYTitleAnnotation extends AbstractXYAnnotation
071        implements Cloneable, PublicCloneable, Serializable {
072
073    /** For serialization. */
074    private static final long serialVersionUID = -4364694501921559958L;
075
076    /** The coordinate type. */
077    private XYCoordinateType coordinateType;
078
079    /** The x-coordinate (in data space). */
080    private double x;
081
082    /** The y-coordinate (in data space). */
083    private double y;
084
085    /** The maximum width. */
086    private double maxWidth;
087
088    /** The maximum height. */
089    private double maxHeight;
090
091    /** The title. */
092    private Title title;
093
094    /**
095     * The title anchor point.
096     */
097    private RectangleAnchor anchor;
098
099    /**
100     * Creates a new annotation to be displayed at the specified (x, y)
101     * location.
102     *
103     * @param x  the x-coordinate (in data space).
104     * @param y  the y-coordinate (in data space).
105     * @param title  the title ({@code null} not permitted).
106     */
107    public XYTitleAnnotation(double x, double y, Title title) {
108        this(x, y, title, RectangleAnchor.CENTER);
109    }
110
111    /**
112     * Creates a new annotation to be displayed at the specified (x, y)
113     * location.
114     *
115     * @param x  the x-coordinate (in data space).
116     * @param y  the y-coordinate (in data space).
117     * @param title  the title ({@code null} not permitted).
118     * @param anchor  the title anchor ({@code null} not permitted).
119     */
120    public XYTitleAnnotation(double x, double y, Title title,
121            RectangleAnchor anchor) {
122        super();
123        Args.requireFinite(x, "x");
124        Args.requireFinite(y, "y");
125        Args.nullNotPermitted(title, "title");
126        Args.nullNotPermitted(anchor, "anchor");
127        this.coordinateType = XYCoordinateType.RELATIVE;
128        this.x = x;
129        this.y = y;
130        this.maxWidth = 0.0;
131        this.maxHeight = 0.0;
132        this.title = title;
133        this.anchor = anchor;
134    }
135
136    /**
137     * Returns the coordinate type (set in the constructor).
138     *
139     * @return The coordinate type (never {@code null}).
140     */
141    public XYCoordinateType getCoordinateType() {
142        return this.coordinateType;
143    }
144
145    /**
146     * Returns the x-coordinate for the annotation.
147     *
148     * @return The x-coordinate.
149     */
150    public double getX() {
151        return this.x;
152    }
153
154    /**
155     * Returns the y-coordinate for the annotation.
156     *
157     * @return The y-coordinate.
158     */
159    public double getY() {
160        return this.y;
161    }
162
163    /**
164     * Returns the title for the annotation.
165     *
166     * @return The title.
167     */
168    public Title getTitle() {
169        return this.title;
170    }
171
172    /**
173     * Returns the title anchor for the annotation.
174     *
175     * @return The title anchor.
176     */
177    public RectangleAnchor getTitleAnchor() {
178        return this.anchor;
179    }
180
181    /**
182     * Returns the maximum width.
183     *
184     * @return The maximum width.
185     */
186    public double getMaxWidth() {
187        return this.maxWidth;
188    }
189
190    /**
191     * Sets the maximum width and sends an
192     * {@link AnnotationChangeEvent} to all registered listeners.
193     *
194     * @param max  the maximum width (0.0 or less means no maximum).
195     */
196    public void setMaxWidth(double max) {
197        this.maxWidth = max;
198        fireAnnotationChanged();
199    }
200
201    /**
202     * Returns the maximum height.
203     *
204     * @return The maximum height.
205     */
206    public double getMaxHeight() {
207        return this.maxHeight;
208    }
209
210    /**
211     * Sets the maximum height and sends an
212     * {@link AnnotationChangeEvent} to all registered listeners.
213     *
214     * @param max  the maximum height.
215     */
216    public void setMaxHeight(double max) {
217        this.maxHeight = max;
218        fireAnnotationChanged();
219    }
220
221    /**
222     * Draws the annotation.  This method is called by the drawing code in the
223     * {@link XYPlot} class, you don't normally need to call this method
224     * directly.
225     *
226     * @param g2  the graphics device.
227     * @param plot  the plot.
228     * @param dataArea  the data area.
229     * @param domainAxis  the domain axis.
230     * @param rangeAxis  the range axis.
231     * @param rendererIndex  the renderer index.
232     * @param info  if supplied, this info object will be populated with
233     *              entity information.
234     */
235    @Override
236    public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
237                     ValueAxis domainAxis, ValueAxis rangeAxis,
238                     int rendererIndex, PlotRenderingInfo info) {
239
240        PlotOrientation orientation = plot.getOrientation();
241        AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
242        AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
243        RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
244                domainAxisLocation, orientation);
245        RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
246                rangeAxisLocation, orientation);
247        Range xRange = domainAxis.getRange();
248        Range yRange = rangeAxis.getRange();
249        double anchorX, anchorY;
250        if (this.coordinateType == XYCoordinateType.RELATIVE) {
251            anchorX = xRange.getLowerBound() + (this.x * xRange.getLength());
252            anchorY = yRange.getLowerBound() + (this.y * yRange.getLength());
253        }
254        else {
255            anchorX = domainAxis.valueToJava2D(this.x, dataArea, domainEdge);
256            anchorY = rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge);
257        }
258
259        float j2DX = (float) domainAxis.valueToJava2D(anchorX, dataArea,
260                domainEdge);
261        float j2DY = (float) rangeAxis.valueToJava2D(anchorY, dataArea,
262                rangeEdge);
263        float xx = 0.0f;
264        float yy = 0.0f;
265        if (orientation == PlotOrientation.HORIZONTAL) {
266            xx = j2DY;
267            yy = j2DX;
268        }
269        else if (orientation == PlotOrientation.VERTICAL) {
270            xx = j2DX;
271            yy = j2DY;
272        }
273
274        double maxW = dataArea.getWidth();
275        double maxH = dataArea.getHeight();
276        if (this.coordinateType == XYCoordinateType.RELATIVE) {
277            if (this.maxWidth > 0.0) {
278                maxW = maxW * this.maxWidth;
279            }
280            if (this.maxHeight > 0.0) {
281                maxH = maxH * this.maxHeight;
282            }
283        }
284        if (this.coordinateType == XYCoordinateType.DATA) {
285            maxW = this.maxWidth;
286            maxH = this.maxHeight;
287        }
288        RectangleConstraint rc = new RectangleConstraint(
289                new Range(0, maxW), new Range(0, maxH));
290
291        Size2D size = this.title.arrange(g2, rc);
292        Rectangle2D titleRect = new Rectangle2D.Double(0, 0, size.width,
293                size.height);
294        Point2D anchorPoint = this.anchor.getAnchorPoint(titleRect);
295        xx = xx - (float) anchorPoint.getX();
296        yy = yy - (float) anchorPoint.getY();
297        titleRect.setRect(xx, yy, titleRect.getWidth(), titleRect.getHeight());
298        BlockParams p = new BlockParams();
299        if (info != null) {
300            if (info.getOwner().getEntityCollection() != null) {
301                p.setGenerateEntities(true);
302            }
303        }
304        Object result = this.title.draw(g2, titleRect, p);
305        if (info != null) {
306            if (result instanceof EntityBlockResult) {
307                EntityBlockResult ebr = (EntityBlockResult) result;
308                info.getOwner().getEntityCollection().addAll(
309                        ebr.getEntityCollection());
310            }
311            String toolTip = getToolTipText();
312            String url = getURL();
313            if (toolTip != null || url != null) {
314                addEntity(info, new Rectangle2D.Float(xx, yy,
315                        (float) size.width, (float) size.height),
316                        rendererIndex, toolTip, url);
317            }
318        }
319    }
320
321    /**
322     * Tests this object for equality with an arbitrary object.
323     *
324     * @param obj  the object ({@code null} permitted).
325     *
326     * @return A boolean.
327     */
328    @Override
329    public boolean equals(Object obj) {
330        if (obj == this) {
331            return true;
332        }
333        if (!(obj instanceof XYTitleAnnotation)) {
334            return false;
335        }
336        XYTitleAnnotation that = (XYTitleAnnotation) obj;
337        if (this.coordinateType != that.coordinateType) {
338            return false;
339        }
340        if (this.x != that.x) {
341            return false;
342        }
343        if (this.y != that.y) {
344            return false;
345        }
346        if (this.maxWidth != that.maxWidth) {
347            return false;
348        }
349        if (this.maxHeight != that.maxHeight) {
350            return false;
351        }
352        if (!Objects.equals(this.title, that.title)) {
353            return false;
354        }
355        if (!this.anchor.equals(that.anchor)) {
356            return false;
357        }
358        return super.equals(obj);
359    }
360
361    /**
362     * Returns a hash code for this object.
363     *
364     * @return A hash code.
365     */
366    @Override
367    public int hashCode() {
368        int result = 193;
369        result = HashUtils.hashCode(result, this.anchor);
370        result = HashUtils.hashCode(result, this.coordinateType);
371        result = HashUtils.hashCode(result, this.x);
372        result = HashUtils.hashCode(result, this.y);
373        result = HashUtils.hashCode(result, this.maxWidth);
374        result = HashUtils.hashCode(result, this.maxHeight);
375        result = HashUtils.hashCode(result, this.title);
376        return result;
377    }
378
379    /**
380     * Returns a clone of the annotation.
381     *
382     * @return A clone.
383     *
384     * @throws CloneNotSupportedException  if the annotation can't be cloned.
385     */
386    @Override
387    public Object clone() throws CloneNotSupportedException {
388        return super.clone();
389    }
390
391}