KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > ColorBar


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------
27  * ColorBar.java
28  * -------------
29  * (C) Copyright 2002-2004, by David M. O'Donnell and Contributors.
30  *
31  * Original Author: David M. O'Donnell;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: ColorBar.java,v 1.6 2005/05/19 13:58:11 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 26-Nov-2002 : Version 1 contributed by David M. O'Donnell (DG);
39  * 14-Jan-2003 : Changed autoRangeMinimumSize from Number --> double (DG);
40  * 17-Jan-2003 : Moved plot classes to separate package (DG);
41  * 20-Jan-2003 : Removed unnecessary constructors (DG);
42  * 26-Mar-2003 : Implemented Serializable (DG);
43  * 09-Jul-2003 : Changed ColorBar from extending axis classes to enclosing
44  * them (DG);
45  * 05-Aug-2003 : Applied changes in bug report 780298 (DG);
46  * 14-Aug-2003 : Implemented Cloneable (DG);
47  * 08-Sep-2003 : Changed ValueAxis API (DG);
48  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
49  *
50  */

51
52 package org.jfree.chart.axis;
53
54 import java.awt.BasicStroke JavaDoc;
55 import java.awt.Graphics2D JavaDoc;
56 import java.awt.Paint JavaDoc;
57 import java.awt.RenderingHints JavaDoc;
58 import java.awt.Stroke JavaDoc;
59 import java.awt.geom.Line2D JavaDoc;
60 import java.awt.geom.Rectangle2D JavaDoc;
61 import java.io.Serializable JavaDoc;
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 /**
70  * A color bar.
71  *
72  * @author David M. O'Donnell
73  */

74 public class ColorBar implements Cloneable JavaDoc, Serializable JavaDoc {
75
76     /** For serialization. */
77     private static final long serialVersionUID = -2101776212647268103L;
78     
79     /** The default color bar thickness. */
80     public static final int DEFAULT_COLORBAR_THICKNESS = 0;
81
82     /** The default color bar thickness percentage. */
83     public static final double DEFAULT_COLORBAR_THICKNESS_PERCENT = 0.10;
84
85     /** The default outer gap. */
86     public static final int DEFAULT_OUTERGAP = 2;
87
88     /** The axis. */
89     private ValueAxis axis;
90     
91     /** The color bar thickness. */
92     private int colorBarThickness = DEFAULT_COLORBAR_THICKNESS;
93
94     /**
95      * The color bar thickness as a percentage of the height of the data area.
96      */

97     private double colorBarThicknessPercent
98         = DEFAULT_COLORBAR_THICKNESS_PERCENT;
99
100     /** The color palette. */
101     private ColorPalette colorPalette = null;
102
103     /** The color bar length. */
104     private int colorBarLength = 0; // default make height of plotArea
105

106     /** The amount of blank space around the colorbar. */
107     private int outerGap;
108
109     /**
110      * Constructs a horizontal colorbar axis, using default values where
111      * necessary.
112      *
113      * @param label the axis label.
114      */

115     public ColorBar(String JavaDoc 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     /**
133      * Configures the color bar.
134      *
135      * @param plot the plot.
136      */

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     /**
145      * Returns the axis.
146      *
147      * @return The axis.
148      */

149     public ValueAxis getAxis() {
150         return this.axis;
151     }
152     
153     /**
154      * Sets the axis.
155      *
156      * @param axis the axis.
157      */

158     public void setAxis(ValueAxis axis) {
159         this.axis = axis;
160     }
161     
162     /**
163      * Rescales the axis to ensure that all data are visible.
164      */

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     /**
172      * Draws the plot on a Java 2D graphics device (such as the screen or a
173      * printer).
174      *
175      * @param g2 the graphics device.
176      * @param cursor the cursor.
177      * @param plotArea the area within which the chart should be drawn.
178      * @param dataArea the area within which the plot should be drawn (a
179      * subset of the drawArea).
180      * @param reservedArea the reserved area.
181      * @param edge the color bar location.
182      *
183      * @return The new cursor location.
184      */

185     public double draw(Graphics2D JavaDoc g2, double cursor,
186                        Rectangle2D JavaDoc plotArea, Rectangle2D JavaDoc dataArea,
187                        Rectangle2D JavaDoc reservedArea, RectangleEdge edge) {
188
189
190         Rectangle2D JavaDoc colorBarArea = null;
191         
192         double thickness = calculateBarThickness(dataArea, edge);
193         if (this.colorBarThickness > 0) {
194             thickness = this.colorBarThickness; // allow fixed thickness
195
}
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 JavaDoc(
211                 dataArea.getX(), plotArea.getMaxY() + this.outerGap,
212                 length, thickness
213             );
214         }
215         else if (edge == RectangleEdge.TOP) {
216             colorBarArea = new Rectangle2D.Double JavaDoc(
217                 dataArea.getX(), reservedArea.getMinY() + this.outerGap,
218                 length, thickness
219             );
220         }
221         else if (edge == RectangleEdge.LEFT) {
222             colorBarArea = new Rectangle2D.Double JavaDoc(
223                 plotArea.getX() - thickness - this.outerGap ,
224                 dataArea.getMinY(), thickness, length
225             );
226         }
227         else if (edge == RectangleEdge.RIGHT) {
228             colorBarArea = new Rectangle2D.Double JavaDoc(
229                 plotArea.getMaxX() + this.outerGap, dataArea.getMinY(),
230                 thickness, length
231             );
232         }
233         
234         // update, but dont draw tick marks (needed for stepped colors)
235
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     /**
273      * Draws the plot on a Java 2D graphics device (such as the screen or a
274      * printer).
275      *
276      * @param g2 the graphics device.
277      * @param colorBarArea the area within which the axis should be drawn.
278      * @param edge the location.
279      */

280     public void drawColorBar(Graphics2D JavaDoc g2, Rectangle2D JavaDoc colorBarArea,
281                              RectangleEdge edge) {
282
283         Object JavaDoc antiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
284         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
285                             RenderingHints.VALUE_ANTIALIAS_OFF);
286
287         // setTickValues was missing from ColorPalette v. 0.96
288
//colorPalette.setTickValues(this.axis.getTicks());
289

290         Stroke JavaDoc strokeSaved = g2.getStroke();
291         g2.setStroke(new BasicStroke JavaDoc(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 JavaDoc line = new Line2D.Double JavaDoc();
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 JavaDoc line = new Line2D.Double JavaDoc();
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     /**
326      * Returns the color palette.
327      *
328      * @return The color palette.
329      */

330     public ColorPalette getColorPalette() {
331         return this.colorPalette;
332     }
333
334     /**
335      * Returns the Paint associated with a value.
336      *
337      * @param value the value.
338      *
339      * @return The paint.
340      */

341     public Paint JavaDoc getPaint(double value) {
342         return this.colorPalette.getPaint(value);
343     }
344
345     /**
346      * Sets the color palette.
347      *
348      * @param palette the new palette.
349      */

350     public void setColorPalette(ColorPalette palette) {
351         this.colorPalette = palette;
352     }
353
354     /**
355      * Sets the maximum value.
356      *
357      * @param value the maximum value.
358      */

359     public void setMaximumValue(double value) {
360         this.colorPalette.setMaxZ(value);
361         this.axis.setUpperBound(value);
362     }
363
364     /**
365      * Sets the minimum value.
366      *
367      * @param value the minimum value.
368      */

369     public void setMinimumValue(double value) {
370         this.colorPalette.setMinZ(value);
371         this.axis.setLowerBound(value);
372     }
373
374     /**
375      * Reserves the space required to draw the color bar.
376      *
377      * @param g2 the graphics device.
378      * @param plot the plot that the axis belongs to.
379      * @param plotArea the area within which the plot should be drawn.
380      * @param dataArea the data area.
381      * @param edge the axis location.
382      * @param space the space already reserved.
383      *
384      * @return The space required to draw the axis in the specified plot area.
385      */

386     public AxisSpace reserveSpace(Graphics2D JavaDoc g2, Plot plot,
387                                   Rectangle2D JavaDoc plotArea,
388                                   Rectangle2D JavaDoc 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     /**
401      * Calculates the bar thickness.
402      *
403      * @param plotArea the plot area.
404      * @param edge the location.
405      *
406      * @return The thickness.
407      */

408     private double calculateBarThickness(Rectangle2D JavaDoc 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     /**
421      * Returns a clone of the object.
422      *
423      * @return A clone.
424      *
425      * @throws CloneNotSupportedException if some component of the color bar
426      * does not support cloning.
427      */

428     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
429     
430         ColorBar clone = (ColorBar) super.clone();
431         clone.axis = (ValueAxis) this.axis.clone();
432         return clone;
433             
434     }
435     
436     /**
437      * Tests this object for equality with another.
438      *
439      * @param obj the object to test against.
440      *
441      * @return A boolean.
442      */

443     public boolean equals(Object JavaDoc 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     /**
475      * Returns a hash code for this object.
476      *
477      * @return A hash code.
478      */

479     public int hashCode() {
480         return this.axis.hashCode();
481     }
482     
483 }
484
Popular Tags