001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * CategoryTextAnnotation.java 029 * --------------------------- 030 * (C) Copyright 2003-2020, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Peter Kolb (patch 2809117); 034 * 035 */ 036 037package org.jfree.chart.annotations; 038 039import java.awt.Graphics2D; 040import java.awt.geom.Rectangle2D; 041import java.io.Serializable; 042 043import org.jfree.chart.axis.CategoryAnchor; 044import org.jfree.chart.axis.CategoryAxis; 045import org.jfree.chart.axis.ValueAxis; 046import org.jfree.chart.event.AnnotationChangeEvent; 047import org.jfree.chart.plot.CategoryPlot; 048import org.jfree.chart.plot.Plot; 049import org.jfree.chart.plot.PlotOrientation; 050import org.jfree.chart.text.TextUtils; 051import org.jfree.chart.ui.RectangleEdge; 052import org.jfree.chart.util.Args; 053import org.jfree.chart.util.PublicCloneable; 054import org.jfree.data.category.CategoryDataset; 055 056/** 057 * A text annotation that can be placed on a {@link CategoryPlot}. 058 */ 059public class CategoryTextAnnotation extends TextAnnotation 060 implements CategoryAnnotation, Cloneable, PublicCloneable, 061 Serializable { 062 063 /** For serialization. */ 064 private static final long serialVersionUID = 3333360090781320147L; 065 066 /** The category. */ 067 private Comparable category; 068 069 /** The category anchor (START, MIDDLE, or END). */ 070 private CategoryAnchor categoryAnchor; 071 072 /** The value. */ 073 private double value; 074 075 /** 076 * Creates a new annotation to be displayed at the given location. 077 * 078 * @param text the text ({@code null} not permitted). 079 * @param category the category ({@code null} not permitted). 080 * @param value the value. 081 */ 082 public CategoryTextAnnotation(String text, Comparable category, 083 double value) { 084 super(text); 085 Args.nullNotPermitted(category, "category"); 086 this.category = category; 087 this.value = value; 088 this.categoryAnchor = CategoryAnchor.MIDDLE; 089 } 090 091 /** 092 * Returns the category. 093 * 094 * @return The category (never {@code null}). 095 * 096 * @see #setCategory(Comparable) 097 */ 098 public Comparable getCategory() { 099 return this.category; 100 } 101 102 /** 103 * Sets the category that the annotation attaches to and sends an 104 * {@link AnnotationChangeEvent} to all registered listeners. 105 * 106 * @param category the category ({@code null} not permitted). 107 * 108 * @see #getCategory() 109 */ 110 public void setCategory(Comparable category) { 111 Args.nullNotPermitted(category, "category"); 112 this.category = category; 113 fireAnnotationChanged(); 114 } 115 116 /** 117 * Returns the category anchor point. 118 * 119 * @return The category anchor point. 120 * 121 * @see #setCategoryAnchor(CategoryAnchor) 122 */ 123 public CategoryAnchor getCategoryAnchor() { 124 return this.categoryAnchor; 125 } 126 127 /** 128 * Sets the category anchor point and sends an 129 * {@link AnnotationChangeEvent} to all registered listeners. 130 * 131 * @param anchor the anchor point ({@code null} not permitted). 132 * 133 * @see #getCategoryAnchor() 134 */ 135 public void setCategoryAnchor(CategoryAnchor anchor) { 136 Args.nullNotPermitted(anchor, "anchor"); 137 this.categoryAnchor = anchor; 138 fireAnnotationChanged(); 139 } 140 141 /** 142 * Returns the value that the annotation attaches to. 143 * 144 * @return The value. 145 * 146 * @see #setValue(double) 147 */ 148 public double getValue() { 149 return this.value; 150 } 151 152 /** 153 * Sets the value and sends an 154 * {@link AnnotationChangeEvent} to all registered listeners. 155 * 156 * @param value the value. 157 * 158 * @see #getValue() 159 */ 160 public void setValue(double value) { 161 this.value = value; 162 fireAnnotationChanged(); 163 } 164 165 /** 166 * Draws the annotation. 167 * 168 * @param g2 the graphics device. 169 * @param plot the plot. 170 * @param dataArea the data area. 171 * @param domainAxis the domain axis. 172 * @param rangeAxis the range axis. 173 */ 174 @Override 175 public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea, 176 CategoryAxis domainAxis, ValueAxis rangeAxis) { 177 178 CategoryDataset dataset = plot.getDataset(); 179 int catIndex = dataset.getColumnIndex(this.category); 180 int catCount = dataset.getColumnCount(); 181 182 float anchorX = 0.0f; 183 float anchorY = 0.0f; 184 PlotOrientation orientation = plot.getOrientation(); 185 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 186 plot.getDomainAxisLocation(), orientation); 187 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 188 plot.getRangeAxisLocation(), orientation); 189 190 if (orientation == PlotOrientation.HORIZONTAL) { 191 anchorY = (float) domainAxis.getCategoryJava2DCoordinate( 192 this.categoryAnchor, catIndex, catCount, dataArea, 193 domainEdge); 194 anchorX = (float) rangeAxis.valueToJava2D(this.value, dataArea, 195 rangeEdge); 196 } 197 else if (orientation == PlotOrientation.VERTICAL) { 198 anchorX = (float) domainAxis.getCategoryJava2DCoordinate( 199 this.categoryAnchor, catIndex, catCount, dataArea, 200 domainEdge); 201 anchorY = (float) rangeAxis.valueToJava2D(this.value, dataArea, 202 rangeEdge); 203 } 204 g2.setFont(getFont()); 205 g2.setPaint(getPaint()); 206 TextUtils.drawRotatedString(getText(), g2, anchorX, anchorY, 207 getTextAnchor(), getRotationAngle(), getRotationAnchor()); 208 209 } 210 211 /** 212 * Tests this object for equality with another. 213 * 214 * @param obj the object ({@code null} permitted). 215 * 216 * @return {@code true} or {@code false}. 217 */ 218 @Override 219 public boolean equals(Object obj) { 220 if (obj == this) { 221 return true; 222 } 223 if (!(obj instanceof CategoryTextAnnotation)) { 224 return false; 225 } 226 CategoryTextAnnotation that = (CategoryTextAnnotation) obj; 227 if (!super.equals(obj)) { 228 return false; 229 } 230 if (!this.category.equals(that.getCategory())) { 231 return false; 232 } 233 if (!this.categoryAnchor.equals(that.getCategoryAnchor())) { 234 return false; 235 } 236 if (this.value != that.getValue()) { 237 return false; 238 } 239 return true; 240 } 241 242 /** 243 * Returns a hash code for this instance. 244 * 245 * @return A hash code. 246 */ 247 @Override 248 public int hashCode() { 249 int result = super.hashCode(); 250 result = 37 * result + this.category.hashCode(); 251 result = 37 * result + this.categoryAnchor.hashCode(); 252 long temp = Double.doubleToLongBits(this.value); 253 result = 37 * result + (int) (temp ^ (temp >>> 32)); 254 return result; 255 } 256 257 /** 258 * Returns a clone of the annotation. 259 * 260 * @return A clone. 261 * 262 * @throws CloneNotSupportedException this class will not throw this 263 * exception, but subclasses (if any) might. 264 */ 265 @Override 266 public Object clone() throws CloneNotSupportedException { 267 return super.clone(); 268 } 269 270}