001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2021, 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 * [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-2021, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 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.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.ui.RectangleAnchor; 059import org.jfree.chart.ui.RectangleEdge; 060import org.jfree.chart.ui.Size2D; 061import org.jfree.chart.util.Args; 062import org.jfree.chart.util.PublicCloneable; 063import org.jfree.chart.util.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.nullNotPermitted(title, "title"); 124 Args.nullNotPermitted(anchor, "anchor"); 125 this.coordinateType = XYCoordinateType.RELATIVE; 126 this.x = x; 127 this.y = y; 128 this.maxWidth = 0.0; 129 this.maxHeight = 0.0; 130 this.title = title; 131 this.anchor = anchor; 132 } 133 134 /** 135 * Returns the coordinate type (set in the constructor). 136 * 137 * @return The coordinate type (never {@code null}). 138 */ 139 public XYCoordinateType getCoordinateType() { 140 return this.coordinateType; 141 } 142 143 /** 144 * Returns the x-coordinate for the annotation. 145 * 146 * @return The x-coordinate. 147 */ 148 public double getX() { 149 return this.x; 150 } 151 152 /** 153 * Returns the y-coordinate for the annotation. 154 * 155 * @return The y-coordinate. 156 */ 157 public double getY() { 158 return this.y; 159 } 160 161 /** 162 * Returns the title for the annotation. 163 * 164 * @return The title. 165 */ 166 public Title getTitle() { 167 return this.title; 168 } 169 170 /** 171 * Returns the title anchor for the annotation. 172 * 173 * @return The title anchor. 174 */ 175 public RectangleAnchor getTitleAnchor() { 176 return this.anchor; 177 } 178 179 /** 180 * Returns the maximum width. 181 * 182 * @return The maximum width. 183 */ 184 public double getMaxWidth() { 185 return this.maxWidth; 186 } 187 188 /** 189 * Sets the maximum width and sends an 190 * {@link AnnotationChangeEvent} to all registered listeners. 191 * 192 * @param max the maximum width (0.0 or less means no maximum). 193 */ 194 public void setMaxWidth(double max) { 195 this.maxWidth = max; 196 fireAnnotationChanged(); 197 } 198 199 /** 200 * Returns the maximum height. 201 * 202 * @return The maximum height. 203 */ 204 public double getMaxHeight() { 205 return this.maxHeight; 206 } 207 208 /** 209 * Sets the maximum height and sends an 210 * {@link AnnotationChangeEvent} to all registered listeners. 211 * 212 * @param max the maximum height. 213 */ 214 public void setMaxHeight(double max) { 215 this.maxHeight = max; 216 fireAnnotationChanged(); 217 } 218 219 /** 220 * Draws the annotation. This method is called by the drawing code in the 221 * {@link XYPlot} class, you don't normally need to call this method 222 * directly. 223 * 224 * @param g2 the graphics device. 225 * @param plot the plot. 226 * @param dataArea the data area. 227 * @param domainAxis the domain axis. 228 * @param rangeAxis the range axis. 229 * @param rendererIndex the renderer index. 230 * @param info if supplied, this info object will be populated with 231 * entity information. 232 */ 233 @Override 234 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 235 ValueAxis domainAxis, ValueAxis rangeAxis, 236 int rendererIndex, PlotRenderingInfo info) { 237 238 PlotOrientation orientation = plot.getOrientation(); 239 AxisLocation domainAxisLocation = plot.getDomainAxisLocation(); 240 AxisLocation rangeAxisLocation = plot.getRangeAxisLocation(); 241 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 242 domainAxisLocation, orientation); 243 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 244 rangeAxisLocation, orientation); 245 Range xRange = domainAxis.getRange(); 246 Range yRange = rangeAxis.getRange(); 247 double anchorX, anchorY; 248 if (this.coordinateType == XYCoordinateType.RELATIVE) { 249 anchorX = xRange.getLowerBound() + (this.x * xRange.getLength()); 250 anchorY = yRange.getLowerBound() + (this.y * yRange.getLength()); 251 } 252 else { 253 anchorX = domainAxis.valueToJava2D(this.x, dataArea, domainEdge); 254 anchorY = rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge); 255 } 256 257 float j2DX = (float) domainAxis.valueToJava2D(anchorX, dataArea, 258 domainEdge); 259 float j2DY = (float) rangeAxis.valueToJava2D(anchorY, dataArea, 260 rangeEdge); 261 float xx = 0.0f; 262 float yy = 0.0f; 263 if (orientation == PlotOrientation.HORIZONTAL) { 264 xx = j2DY; 265 yy = j2DX; 266 } 267 else if (orientation == PlotOrientation.VERTICAL) { 268 xx = j2DX; 269 yy = j2DY; 270 } 271 272 double maxW = dataArea.getWidth(); 273 double maxH = dataArea.getHeight(); 274 if (this.coordinateType == XYCoordinateType.RELATIVE) { 275 if (this.maxWidth > 0.0) { 276 maxW = maxW * this.maxWidth; 277 } 278 if (this.maxHeight > 0.0) { 279 maxH = maxH * this.maxHeight; 280 } 281 } 282 if (this.coordinateType == XYCoordinateType.DATA) { 283 maxW = this.maxWidth; 284 maxH = this.maxHeight; 285 } 286 RectangleConstraint rc = new RectangleConstraint( 287 new Range(0, maxW), new Range(0, maxH)); 288 289 Size2D size = this.title.arrange(g2, rc); 290 Rectangle2D titleRect = new Rectangle2D.Double(0, 0, size.width, 291 size.height); 292 Point2D anchorPoint = this.anchor.getAnchorPoint(titleRect); 293 xx = xx - (float) anchorPoint.getX(); 294 yy = yy - (float) anchorPoint.getY(); 295 titleRect.setRect(xx, yy, titleRect.getWidth(), titleRect.getHeight()); 296 BlockParams p = new BlockParams(); 297 if (info != null) { 298 if (info.getOwner().getEntityCollection() != null) { 299 p.setGenerateEntities(true); 300 } 301 } 302 Object result = this.title.draw(g2, titleRect, p); 303 if (info != null) { 304 if (result instanceof EntityBlockResult) { 305 EntityBlockResult ebr = (EntityBlockResult) result; 306 info.getOwner().getEntityCollection().addAll( 307 ebr.getEntityCollection()); 308 } 309 String toolTip = getToolTipText(); 310 String url = getURL(); 311 if (toolTip != null || url != null) { 312 addEntity(info, new Rectangle2D.Float(xx, yy, 313 (float) size.width, (float) size.height), 314 rendererIndex, toolTip, url); 315 } 316 } 317 } 318 319 /** 320 * Tests this object for equality with an arbitrary object. 321 * 322 * @param obj the object ({@code null} permitted). 323 * 324 * @return A boolean. 325 */ 326 @Override 327 public boolean equals(Object obj) { 328 if (obj == this) { 329 return true; 330 } 331 if (!(obj instanceof XYTitleAnnotation)) { 332 return false; 333 } 334 XYTitleAnnotation that = (XYTitleAnnotation) obj; 335 if (this.coordinateType != that.coordinateType) { 336 return false; 337 } 338 if (this.x != that.x) { 339 return false; 340 } 341 if (this.y != that.y) { 342 return false; 343 } 344 if (this.maxWidth != that.maxWidth) { 345 return false; 346 } 347 if (this.maxHeight != that.maxHeight) { 348 return false; 349 } 350 if (!Objects.equals(this.title, that.title)) { 351 return false; 352 } 353 if (!this.anchor.equals(that.anchor)) { 354 return false; 355 } 356 return super.equals(obj); 357 } 358 359 /** 360 * Returns a hash code for this object. 361 * 362 * @return A hash code. 363 */ 364 @Override 365 public int hashCode() { 366 int result = 193; 367 result = HashUtils.hashCode(result, this.anchor); 368 result = HashUtils.hashCode(result, this.coordinateType); 369 result = HashUtils.hashCode(result, this.x); 370 result = HashUtils.hashCode(result, this.y); 371 result = HashUtils.hashCode(result, this.maxWidth); 372 result = HashUtils.hashCode(result, this.maxHeight); 373 result = HashUtils.hashCode(result, this.title); 374 return result; 375 } 376 377 /** 378 * Returns a clone of the annotation. 379 * 380 * @return A clone. 381 * 382 * @throws CloneNotSupportedException if the annotation can't be cloned. 383 */ 384 @Override 385 public Object clone() throws CloneNotSupportedException { 386 return super.clone(); 387 } 388 389}