1 47 48 package org.jfree.chart.annotations; 49 50 import java.awt.Graphics2D ; 51 import java.awt.geom.Rectangle2D ; 52 import java.io.Serializable ; 53 54 import org.jfree.chart.axis.CategoryAnchor; 55 import org.jfree.chart.axis.CategoryAxis; 56 import org.jfree.chart.axis.ValueAxis; 57 import org.jfree.chart.plot.CategoryPlot; 58 import org.jfree.chart.plot.Plot; 59 import org.jfree.chart.plot.PlotOrientation; 60 import org.jfree.data.category.CategoryDataset; 61 import org.jfree.text.TextUtilities; 62 import org.jfree.ui.RectangleEdge; 63 64 68 public class CategoryTextAnnotation extends TextAnnotation 69 implements CategoryAnnotation, 70 Cloneable , Serializable { 71 72 73 private static final long serialVersionUID = 3333360090781320147L; 74 75 76 private Comparable category; 77 78 79 private CategoryAnchor categoryAnchor; 80 81 82 private double value; 83 84 91 public CategoryTextAnnotation(String text, Comparable category, 92 double value) { 93 super(text); 94 if (category == null) { 95 throw new IllegalArgumentException ("Null 'category' argument."); 96 } 97 this.category = category; 98 this.value = value; 99 this.categoryAnchor = CategoryAnchor.MIDDLE; 100 } 101 102 107 public Comparable getCategory() { 108 return this.category; 109 } 110 111 116 public void setCategory(Comparable category) { 117 if (category == null) { 118 throw new IllegalArgumentException ("Null 'category' argument."); 119 } 120 this.category = category; 121 } 122 123 128 public CategoryAnchor getCategoryAnchor() { 129 return this.categoryAnchor; 130 } 131 132 137 public void setCategoryAnchor(CategoryAnchor anchor) { 138 if (anchor == null) { 139 throw new IllegalArgumentException ("Null 'anchor' argument."); 140 } 141 this.categoryAnchor = anchor; 142 } 143 144 149 public double getValue() { 150 return this.value; 151 } 152 153 158 public void setValue(double value) { 159 this.value = value; 160 } 161 162 171 public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea, 172 CategoryAxis domainAxis, ValueAxis rangeAxis) { 173 174 CategoryDataset dataset = plot.getDataset(); 175 int catIndex = dataset.getColumnIndex(this.category); 176 int catCount = dataset.getColumnCount(); 177 178 float anchorX = 0.0f; 179 float anchorY = 0.0f; 180 PlotOrientation orientation = plot.getOrientation(); 181 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 182 plot.getDomainAxisLocation(), orientation 183 ); 184 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 185 plot.getRangeAxisLocation(), orientation 186 ); 187 188 if (orientation == PlotOrientation.HORIZONTAL) { 189 anchorY = (float) domainAxis.getCategoryJava2DCoordinate( 190 this.categoryAnchor, catIndex, catCount, dataArea, domainEdge 191 ); 192 anchorX = (float) rangeAxis.valueToJava2D( 193 this.value, dataArea, rangeEdge 194 ); 195 } 196 else if (orientation == PlotOrientation.VERTICAL) { 197 anchorX = (float) domainAxis.getCategoryJava2DCoordinate( 198 this.categoryAnchor, catIndex, catCount, dataArea, domainEdge 199 ); 200 anchorY = (float) rangeAxis.valueToJava2D( 201 this.value, dataArea, rangeEdge 202 ); 203 } 204 g2.setFont(getFont()); 205 g2.setPaint(getPaint()); 206 TextUtilities.drawRotatedString( 207 getText(), 208 g2, 209 anchorX, 210 anchorY, 211 getTextAnchor(), 212 getRotationAngle(), 213 getRotationAnchor() 214 ); 215 216 } 217 218 225 public boolean equals(Object obj) { 226 if (obj == this) { 227 return true; 228 } 229 if (!(obj instanceof CategoryTextAnnotation)) { 230 return false; 231 } 232 CategoryTextAnnotation that = (CategoryTextAnnotation) obj; 233 if (!super.equals(obj)) { 234 return false; 235 } 236 if (!this.category.equals(that.getCategory())) { 237 return false; 238 } 239 if (!this.categoryAnchor.equals(that.getCategoryAnchor())) { 240 return false; 241 } 242 if (this.value != that.getValue()) { 243 return false; 244 } 245 return true; 246 } 247 248 256 public Object clone() throws CloneNotSupportedException { 257 return super.clone(); 258 } 259 260 } 261 | Popular Tags |