1 51 52 package org.jfree.chart.axis; 53 54 import java.awt.BasicStroke ; 55 import java.awt.Graphics2D ; 56 import java.awt.Paint ; 57 import java.awt.RenderingHints ; 58 import java.awt.Stroke ; 59 import java.awt.geom.Line2D ; 60 import java.awt.geom.Rectangle2D ; 61 import java.io.Serializable ; 62 63 import org.jfree.chart.plot.ContourPlot; 64 import org.jfree.chart.plot.Plot; 65 import org.jfree.chart.ui.ColorPalette; 66 import org.jfree.chart.ui.RainbowPalette; 67 import org.jfree.ui.RectangleEdge; 68 69 74 public class ColorBar implements Cloneable , Serializable { 75 76 77 private static final long serialVersionUID = -2101776212647268103L; 78 79 80 public static final int DEFAULT_COLORBAR_THICKNESS = 0; 81 82 83 public static final double DEFAULT_COLORBAR_THICKNESS_PERCENT = 0.10; 84 85 86 public static final int DEFAULT_OUTERGAP = 2; 87 88 89 private ValueAxis axis; 90 91 92 private int colorBarThickness = DEFAULT_COLORBAR_THICKNESS; 93 94 97 private double colorBarThicknessPercent 98 = DEFAULT_COLORBAR_THICKNESS_PERCENT; 99 100 101 private ColorPalette colorPalette = null; 102 103 104 private int colorBarLength = 0; 106 107 private int outerGap; 108 109 115 public ColorBar(String label) { 116 117 NumberAxis a = new NumberAxis(label); 118 a.setAutoRangeIncludesZero(false); 119 this.axis = a; 120 this.axis.setLowerMargin(0.0); 121 this.axis.setUpperMargin(0.0); 122 123 this.colorPalette = new RainbowPalette(); 124 this.colorBarThickness = DEFAULT_COLORBAR_THICKNESS; 125 this.colorBarThicknessPercent = DEFAULT_COLORBAR_THICKNESS_PERCENT; 126 this.outerGap = DEFAULT_OUTERGAP; 127 this.colorPalette.setMinZ(this.axis.getRange().getLowerBound()); 128 this.colorPalette.setMaxZ(this.axis.getRange().getUpperBound()); 129 130 } 131 132 137 public void configure(ContourPlot plot) { 138 double minZ = plot.getDataset().getMinZValue(); 139 double maxZ = plot.getDataset().getMaxZValue(); 140 setMinimumValue(minZ); 141 setMaximumValue(maxZ); 142 } 143 144 149 public ValueAxis getAxis() { 150 return this.axis; 151 } 152 153 158 public void setAxis(ValueAxis axis) { 159 this.axis = axis; 160 } 161 162 165 public void autoAdjustRange() { 166 this.axis.autoAdjustRange(); 167 this.colorPalette.setMinZ(this.axis.getLowerBound()); 168 this.colorPalette.setMaxZ(this.axis.getUpperBound()); 169 } 170 171 185 public double draw(Graphics2D g2, double cursor, 186 Rectangle2D plotArea, Rectangle2D dataArea, 187 Rectangle2D reservedArea, RectangleEdge edge) { 188 189 190 Rectangle2D colorBarArea = null; 191 192 double thickness = calculateBarThickness(dataArea, edge); 193 if (this.colorBarThickness > 0) { 194 thickness = this.colorBarThickness; } 196 197 double length = 0.0; 198 if (RectangleEdge.isLeftOrRight(edge)) { 199 length = dataArea.getHeight(); 200 } 201 else { 202 length = dataArea.getWidth(); 203 } 204 205 if (this.colorBarLength > 0) { 206 length = this.colorBarLength; 207 } 208 209 if (edge == RectangleEdge.BOTTOM) { 210 colorBarArea = new Rectangle2D.Double ( 211 dataArea.getX(), plotArea.getMaxY() + this.outerGap, 212 length, thickness 213 ); 214 } 215 else if (edge == RectangleEdge.TOP) { 216 colorBarArea = new Rectangle2D.Double ( 217 dataArea.getX(), reservedArea.getMinY() + this.outerGap, 218 length, thickness 219 ); 220 } 221 else if (edge == RectangleEdge.LEFT) { 222 colorBarArea = new Rectangle2D.Double ( 223 plotArea.getX() - thickness - this.outerGap , 224 dataArea.getMinY(), thickness, length 225 ); 226 } 227 else if (edge == RectangleEdge.RIGHT) { 228 colorBarArea = new Rectangle2D.Double ( 229 plotArea.getMaxX() + this.outerGap, dataArea.getMinY(), 230 thickness, length 231 ); 232 } 233 234 this.axis.refreshTicks( 236 g2, new AxisState(), colorBarArea, edge 237 ); 238 239 drawColorBar(g2, colorBarArea, edge); 240 241 AxisState state = null; 242 if (edge == RectangleEdge.TOP) { 243 cursor = colorBarArea.getMinY(); 244 state = this.axis.draw( 245 g2, cursor, reservedArea, colorBarArea, RectangleEdge.TOP, null 246 ); 247 } 248 else if (edge == RectangleEdge.BOTTOM) { 249 cursor = colorBarArea.getMaxY(); 250 state = this.axis.draw( 251 g2, cursor, reservedArea, colorBarArea, RectangleEdge.BOTTOM, 252 null 253 ); 254 } 255 else if (edge == RectangleEdge.LEFT) { 256 cursor = colorBarArea.getMinX(); 257 state = this.axis.draw( 258 g2, cursor, reservedArea, colorBarArea, RectangleEdge.LEFT, null 259 ); 260 } 261 else if (edge == RectangleEdge.RIGHT) { 262 cursor = colorBarArea.getMaxX(); 263 state = this.axis.draw( 264 g2, cursor, reservedArea, colorBarArea, RectangleEdge.RIGHT, 265 null 266 ); 267 } 268 return state.getCursor(); 269 270 } 271 272 280 public void drawColorBar(Graphics2D g2, Rectangle2D colorBarArea, 281 RectangleEdge edge) { 282 283 Object antiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 284 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 285 RenderingHints.VALUE_ANTIALIAS_OFF); 286 287 290 Stroke strokeSaved = g2.getStroke(); 291 g2.setStroke(new BasicStroke (1.0f)); 292 293 if (RectangleEdge.isTopOrBottom(edge)) { 294 double y1 = colorBarArea.getY(); 295 double y2 = colorBarArea.getMaxY(); 296 double xx = colorBarArea.getX(); 297 Line2D line = new Line2D.Double (); 298 while (xx <= colorBarArea.getMaxX()) { 299 double value = this.axis.java2DToValue(xx, colorBarArea, edge); 300 line.setLine(xx, y1, xx, y2); 301 g2.setPaint(getPaint(value)); 302 g2.draw(line); 303 xx += 1; 304 } 305 } 306 else { 307 double y1 = colorBarArea.getX(); 308 double y2 = colorBarArea.getMaxX(); 309 double xx = colorBarArea.getY(); 310 Line2D line = new Line2D.Double (); 311 while (xx <= colorBarArea.getMaxY()) { 312 double value = this.axis.java2DToValue(xx, colorBarArea, edge); 313 line.setLine(y1, xx, y2, xx); 314 g2.setPaint(getPaint(value)); 315 g2.draw(line); 316 xx += 1; 317 } 318 } 319 320 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias); 321 g2.setStroke(strokeSaved); 322 323 } 324 325 330 public ColorPalette getColorPalette() { 331 return this.colorPalette; 332 } 333 334 341 public Paint getPaint(double value) { 342 return this.colorPalette.getPaint(value); 343 } 344 345 350 public void setColorPalette(ColorPalette palette) { 351 this.colorPalette = palette; 352 } 353 354 359 public void setMaximumValue(double value) { 360 this.colorPalette.setMaxZ(value); 361 this.axis.setUpperBound(value); 362 } 363 364 369 public void setMinimumValue(double value) { 370 this.colorPalette.setMinZ(value); 371 this.axis.setLowerBound(value); 372 } 373 374 386 public AxisSpace reserveSpace(Graphics2D g2, Plot plot, 387 Rectangle2D plotArea, 388 Rectangle2D dataArea, RectangleEdge edge, 389 AxisSpace space) { 390 391 AxisSpace result = this.axis.reserveSpace( 392 g2, plot, plotArea, edge, space 393 ); 394 double thickness = calculateBarThickness(dataArea, edge); 395 result.add(thickness + 2 * this.outerGap, edge); 396 return result; 397 398 } 399 400 408 private double calculateBarThickness(Rectangle2D plotArea, 409 RectangleEdge edge) { 410 double result = 0.0; 411 if (RectangleEdge.isLeftOrRight(edge)) { 412 result = plotArea.getWidth() * this.colorBarThicknessPercent; 413 } 414 else { 415 result = plotArea.getHeight() * this.colorBarThicknessPercent; 416 } 417 return result; 418 } 419 420 428 public Object clone() throws CloneNotSupportedException { 429 430 ColorBar clone = (ColorBar) super.clone(); 431 clone.axis = (ValueAxis) this.axis.clone(); 432 return clone; 433 434 } 435 436 443 public boolean equals(Object obj) { 444 445 if (obj == this) { 446 return true; 447 } 448 if (!(obj instanceof ColorBar)) { 449 return false; 450 } 451 ColorBar that = (ColorBar) obj; 452 if (!this.axis.equals(that.axis)) { 453 return false; 454 } 455 if (this.colorBarThickness != that.colorBarThickness) { 456 return false; 457 } 458 if (this.colorBarThicknessPercent != that.colorBarThicknessPercent) { 459 return false; 460 } 461 if (!this.colorPalette.equals(that.colorPalette)) { 462 return false; 463 } 464 if (this.colorBarLength != that.colorBarLength) { 465 return false; 466 } 467 if (this.outerGap != that.outerGap) { 468 return false; 469 } 470 return true; 471 472 } 473 474 479 public int hashCode() { 480 return this.axis.hashCode(); 481 } 482 483 } 484 | Popular Tags |