001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * --------------
028 * ValueAxis.java
029 * --------------
030 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Jonathan Nash;
034 * Nicolas Brodu (for Astrium and EADS Corporate Research
035 * Center);
036 * Peter Kolb (patch 1934255);
037 * Andrew Mickish (patch 1870189);
038 *
039 * Changes
040 * -------
041 * 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG);
042 * 23-Nov-2001 : Overhauled standard tick unit code (DG);
043 * 04-Dec-2001 : Changed constructors to protected, and tidied up default
044 * values (DG);
045 * 12-Dec-2001 : Fixed vertical gridlines bug (DG);
046 * 16-Jan-2002 : Added an optional crosshair, based on the implementation by
047 * Jonathan Nash (DG);
048 * 23-Jan-2002 : Moved the minimum and maximum values to here from NumberAxis,
049 * and changed the type from Number to double (DG);
050 * 25-Feb-2002 : Added default value for autoRange. Changed autoAdjustRange
051 * from public to protected. Updated import statements (DG);
052 * 23-Apr-2002 : Added setRange() method (DG);
053 * 29-Apr-2002 : Added range adjustment methods (DG);
054 * 13-Jun-2002 : Modified setCrosshairValue() to notify listeners only when the
055 * crosshairs are visible, to avoid unnecessary repaints, as
056 * suggested by Kees Kuip (DG);
057 * 25-Jul-2002 : Moved lower and upper margin attributes from the NumberAxis
058 * class (DG);
059 * 05-Sep-2002 : Updated constructor for changes in Axis class (DG);
060 * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
061 * 04-Oct-2002 : Moved standardTickUnits from NumberAxis --> ValueAxis (DG);
062 * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
063 * 19-Nov-2002 : Removed grid settings (now controlled by the plot) (DG);
064 * 27-Nov-2002 : Moved the 'inverted' attributed from NumberAxis to
065 * ValueAxis (DG);
066 * 03-Jan-2003 : Small fix to ensure auto-range minimum is observed
067 * immediately (DG);
068 * 14-Jan-2003 : Changed autoRangeMinimumSize from Number --> double (DG);
069 * 20-Jan-2003 : Replaced monolithic constructor (DG);
070 * 26-Mar-2003 : Implemented Serializable (DG);
071 * 09-May-2003 : Added AxisLocation parameter to translation methods (DG);
072 * 13-Aug-2003 : Implemented Cloneable (DG);
073 * 01-Sep-2003 : Fixed bug 793167 (setMaximumAxisValue exception) (DG);
074 * 02-Sep-2003 : Fixed bug 795366 (zooming on inverted axes) (DG);
075 * 08-Sep-2003 : Completed Serialization support (NB);
076 * 08-Sep-2003 : Renamed get/setMinimumValue --> get/setLowerBound,
077 * and get/setMaximumValue --> get/setUpperBound (DG);
078 * 27-Oct-2003 : Changed DEFAULT_AUTO_RANGE_MINIMUM_SIZE value - see bug ID
079 * 829606 (DG);
080 * 07-Nov-2003 : Changes to tick mechanism (DG);
081 * 06-Jan-2004 : Moved axis line attributes to Axis class (DG);
082 * 21-Jan-2004 : Removed redundant axisLineVisible attribute. Renamed
083 * translateJava2DToValue --> java2DToValue, and
084 * translateValueToJava2D --> valueToJava2D (DG);
085 * 23-Jan-2004 : Fixed setAxisLinePaint() and setAxisLineStroke() which had no
086 * effect (andreas.gawecki@coremedia.com);
087 * 07-Apr-2004 : Changed text bounds calculation (DG);
088 * 26-Apr-2004 : Added getter/setter methods for arrow shapes (DG);
089 * 18-May-2004 : Added methods to set axis range *including* current
090 * margins (DG);
091 * 02-Jun-2004 : Fixed bug in setRangeWithMargins() method (DG);
092 * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities
093 * --> TextUtilities (DG);
094 * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
095 * release (DG);
096 * 21-Apr-2005 : Replaced Insets with RectangleInsets (DG);
097 * ------------- JFREECHART 1.0.x ---------------------------------------------
098 * 10-Oct-2006 : Source reformatting (DG);
099 * 22-Mar-2007 : Added new defaultAutoRange attribute (DG);
100 * 02-Aug-2007 : Check for major tick when drawing label (DG);
101 * 25-Sep-2008 : Added minor tick support, see patch 1934255 by Peter Kolb (DG);
102 *
103 */
104
105 package org.jfree.chart.axis;
106
107 import java.awt.Font;
108 import java.awt.FontMetrics;
109 import java.awt.Graphics2D;
110 import java.awt.Polygon;
111 import java.awt.Shape;
112 import java.awt.font.LineMetrics;
113 import java.awt.geom.AffineTransform;
114 import java.awt.geom.Line2D;
115 import java.awt.geom.Rectangle2D;
116 import java.io.IOException;
117 import java.io.ObjectInputStream;
118 import java.io.ObjectOutputStream;
119 import java.io.Serializable;
120 import java.util.Iterator;
121 import java.util.List;
122
123 import org.jfree.chart.event.AxisChangeEvent;
124 import org.jfree.chart.plot.Plot;
125 import org.jfree.data.Range;
126 import org.jfree.io.SerialUtilities;
127 import org.jfree.text.TextUtilities;
128 import org.jfree.ui.RectangleEdge;
129 import org.jfree.ui.RectangleInsets;
130 import org.jfree.util.ObjectUtilities;
131 import org.jfree.util.PublicCloneable;
132
133 /**
134 * The base class for axes that display value data, where values are measured
135 * using the <code>double</code> primitive. The two key subclasses are
136 * {@link DateAxis} and {@link NumberAxis}.
137 */
138 public abstract class ValueAxis extends Axis
139 implements Cloneable, PublicCloneable, Serializable {
140
141 /** For serialization. */
142 private static final long serialVersionUID = 3698345477322391456L;
143
144 /** The default axis range. */
145 public static final Range DEFAULT_RANGE = new Range(0.0, 1.0);
146
147 /** The default auto-range value. */
148 public static final boolean DEFAULT_AUTO_RANGE = true;
149
150 /** The default inverted flag setting. */
151 public static final boolean DEFAULT_INVERTED = false;
152
153 /** The default minimum auto range. */
154 public static final double DEFAULT_AUTO_RANGE_MINIMUM_SIZE = 0.00000001;
155
156 /** The default value for the lower margin (0.05 = 5%). */
157 public static final double DEFAULT_LOWER_MARGIN = 0.05;
158
159 /** The default value for the upper margin (0.05 = 5%). */
160 public static final double DEFAULT_UPPER_MARGIN = 0.05;
161
162 /**
163 * The default lower bound for the axis.
164 *
165 * @deprecated From 1.0.5 onwards, the axis defines a defaultRange
166 * attribute (see {@link #getDefaultAutoRange()}).
167 */
168 public static final double DEFAULT_LOWER_BOUND = 0.0;
169
170 /**
171 * The default upper bound for the axis.
172 *
173 * @deprecated From 1.0.5 onwards, the axis defines a defaultRange
174 * attribute (see {@link #getDefaultAutoRange()}).
175 */
176 public static final double DEFAULT_UPPER_BOUND = 1.0;
177
178 /** The default auto-tick-unit-selection value. */
179 public static final boolean DEFAULT_AUTO_TICK_UNIT_SELECTION = true;
180
181 /** The maximum tick count. */
182 public static final int MAXIMUM_TICK_COUNT = 500;
183
184 /**
185 * A flag that controls whether an arrow is drawn at the positive end of
186 * the axis line.
187 */
188 private boolean positiveArrowVisible;
189
190 /**
191 * A flag that controls whether an arrow is drawn at the negative end of
192 * the axis line.
193 */
194 private boolean negativeArrowVisible;
195
196 /** The shape used for an up arrow. */
197 private transient Shape upArrow;
198
199 /** The shape used for a down arrow. */
200 private transient Shape downArrow;
201
202 /** The shape used for a left arrow. */
203 private transient Shape leftArrow;
204
205 /** The shape used for a right arrow. */
206 private transient Shape rightArrow;
207
208 /** A flag that affects the orientation of the values on the axis. */
209 private boolean inverted;
210
211 /** The axis range. */
212 private Range range;
213
214 /**
215 * Flag that indicates whether the axis automatically scales to fit the
216 * chart data.
217 */
218 private boolean autoRange;
219
220 /** The minimum size for the 'auto' axis range (excluding margins). */
221 private double autoRangeMinimumSize;
222
223 /**
224 * The default range is used when the dataset is empty and the axis needs
225 * to determine the auto range.
226 *
227 * @since 1.0.5
228 */
229 private Range defaultAutoRange;
230
231 /**
232 * The upper margin percentage. This indicates the amount by which the
233 * maximum axis value exceeds the maximum data value (as a percentage of
234 * the range on the axis) when the axis range is determined automatically.
235 */
236 private double upperMargin;
237
238 /**
239 * The lower margin. This is a percentage that indicates the amount by
240 * which the minimum axis value is "less than" the minimum data value when
241 * the axis range is determined automatically.
242 */
243 private double lowerMargin;
244
245 /**
246 * If this value is positive, the amount is subtracted from the maximum
247 * data value to determine the lower axis range. This can be used to
248 * provide a fixed "window" on dynamic data.
249 */
250 private double fixedAutoRange;
251
252 /**
253 * Flag that indicates whether or not the tick unit is selected
254 * automatically.
255 */
256 private boolean autoTickUnitSelection;
257
258 /** The standard tick units for the axis. */
259 private TickUnitSource standardTickUnits;
260
261 /** An index into an array of standard tick values. */
262 private int autoTickIndex;
263
264 /** The number of minor ticks per major tick unit. */
265 private int minorTickCount;
266
267 /** A flag indicating whether or not tick labels are rotated to vertical. */
268 private boolean verticalTickLabels;
269
270 /**
271 * Constructs a value axis.
272 *
273 * @param label the axis label (<code>null</code> permitted).
274 * @param standardTickUnits the source for standard tick units
275 * (<code>null</code> permitted).
276 */
277 protected ValueAxis(String label, TickUnitSource standardTickUnits) {
278
279 super(label);
280
281 this.positiveArrowVisible = false;
282 this.negativeArrowVisible = false;
283
284 this.range = DEFAULT_RANGE;
285 this.autoRange = DEFAULT_AUTO_RANGE;
286 this.defaultAutoRange = DEFAULT_RANGE;
287
288 this.inverted = DEFAULT_INVERTED;
289 this.autoRangeMinimumSize = DEFAULT_AUTO_RANGE_MINIMUM_SIZE;
290
291 this.lowerMargin = DEFAULT_LOWER_MARGIN;
292 this.upperMargin = DEFAULT_UPPER_MARGIN;
293
294 this.fixedAutoRange = 0.0;
295
296 this.autoTickUnitSelection = DEFAULT_AUTO_TICK_UNIT_SELECTION;
297 this.standardTickUnits = standardTickUnits;
298
299 Polygon p1 = new Polygon();
300 p1.addPoint(0, 0);
301 p1.addPoint(-2, 2);
302 p1.addPoint(2, 2);
303
304 this.upArrow = p1;
305
306 Polygon p2 = new Polygon();
307 p2.addPoint(0, 0);
308 p2.addPoint(-2, -2);
309 p2.addPoint(2, -2);
310
311 this.downArrow = p2;
312
313 Polygon p3 = new Polygon();
314 p3.addPoint(0, 0);
315 p3.addPoint(-2, -2);
316 p3.addPoint(-2, 2);
317
318 this.rightArrow = p3;
319
320 Polygon p4 = new Polygon();
321 p4.addPoint(0, 0);
322 p4.addPoint(2, -2);
323 p4.addPoint(2, 2);
324
325 this.leftArrow = p4;
326
327 this.verticalTickLabels = false;
328 this.minorTickCount = 1;
329
330 }
331
332 /**
333 * Returns <code>true</code> if the tick labels should be rotated (to
334 * vertical), and <code>false</code> otherwise.
335 *
336 * @return <code>true</code> or <code>false</code>.
337 *
338 * @see #setVerticalTickLabels(boolean)
339 */
340 public boolean isVerticalTickLabels() {
341 return this.verticalTickLabels;
342 }
343
344 /**
345 * Sets the flag that controls whether the tick labels are displayed
346 * vertically (that is, rotated 90 degrees from horizontal). If the flag
347 * is changed, an {@link AxisChangeEvent} is sent to all registered
348 * listeners.
349 *
350 * @param flag the flag.
351 *
352 * @see #isVerticalTickLabels()
353 */
354 public void setVerticalTickLabels(boolean flag) {
355 if (this.verticalTickLabels != flag) {
356 this.verticalTickLabels = flag;
357 notifyListeners(new AxisChangeEvent(this));
358 }
359 }
360
361 /**
362 * Returns a flag that controls whether or not the axis line has an arrow
363 * drawn that points in the positive direction for the axis.
364 *
365 * @return A boolean.
366 *
367 * @see #setPositiveArrowVisible(boolean)
368 */
369 public boolean isPositiveArrowVisible() {
370 return this.positiveArrowVisible;
371 }
372
373 /**
374 * Sets a flag that controls whether or not the axis lines has an arrow
375 * drawn that points in the positive direction for the axis, and sends an
376 * {@link AxisChangeEvent} to all registered listeners.
377 *
378 * @param visible the flag.
379 *
380 * @see #isPositiveArrowVisible()
381 */
382 public void setPositiveArrowVisible(boolean visible) {
383 this.positiveArrowVisible = visible;
384 notifyListeners(new AxisChangeEvent(this));
385 }
386
387 /**
388 * Returns a flag that controls whether or not the axis line has an arrow
389 * drawn that points in the negative direction for the axis.
390 *
391 * @return A boolean.
392 *
393 * @see #setNegativeArrowVisible(boolean)
394 */
395 public boolean isNegativeArrowVisible() {
396 return this.negativeArrowVisible;
397 }
398
399 /**
400 * Sets a flag that controls whether or not the axis lines has an arrow
401 * drawn that points in the negative direction for the axis, and sends an
402 * {@link AxisChangeEvent} to all registered listeners.
403 *
404 * @param visible the flag.
405 *
406 * @see #setNegativeArrowVisible(boolean)
407 */
408 public void setNegativeArrowVisible(boolean visible) {
409 this.negativeArrowVisible = visible;
410 notifyListeners(new AxisChangeEvent(this));
411 }
412
413 /**
414 * Returns a shape that can be displayed as an arrow pointing upwards at
415 * the end of an axis line.
416 *
417 * @return A shape (never <code>null</code>).
418 *
419 * @see #setUpArrow(Shape)
420 */
421 public Shape getUpArrow() {
422 return this.upArrow;
423 }
424
425 /**
426 * Sets the shape that can be displayed as an arrow pointing upwards at
427 * the end of an axis line and sends an {@link AxisChangeEvent} to all
428 * registered listeners.
429 *
430 * @param arrow the arrow shape (<code>null</code> not permitted).
431 *
432 * @see #getUpArrow()
433 */
434 public void setUpArrow(Shape arrow) {
435 if (arrow == null) {
436 throw new IllegalArgumentException("Null 'arrow' argument.");
437 }
438 this.upArrow = arrow;
439 notifyListeners(new AxisChangeEvent(this));
440 }
441
442 /**
443 * Returns a shape that can be displayed as an arrow pointing downwards at
444 * the end of an axis line.
445 *
446 * @return A shape (never <code>null</code>).
447 *
448 * @see #setDownArrow(Shape)
449 */
450 public Shape getDownArrow() {
451 return this.downArrow;
452 }
453
454 /**
455 * Sets the shape that can be displayed as an arrow pointing downwards at
456 * the end of an axis line and sends an {@link AxisChangeEvent} to all
457 * registered listeners.
458 *
459 * @param arrow the arrow shape (<code>null</code> not permitted).
460 *
461 * @see #getDownArrow()
462 */
463 public void setDownArrow(Shape arrow) {
464 if (arrow == null) {
465 throw new IllegalArgumentException("Null 'arrow' argument.");
466 }
467 this.downArrow = arrow;
468 notifyListeners(new AxisChangeEvent(this));
469 }
470
471 /**
472 * Returns a shape that can be displayed as an arrow pointing left at the
473 * end of an axis line.
474 *
475 * @return A shape (never <code>null</code>).
476 *
477 * @see #setLeftArrow(Shape)
478 */
479 public Shape getLeftArrow() {
480 return this.leftArrow;
481 }
482
483 /**
484 * Sets the shape that can be displayed as an arrow pointing left at the
485 * end of an axis line and sends an {@link AxisChangeEvent} to all
486 * registered listeners.
487 *
488 * @param arrow the arrow shape (<code>null</code> not permitted).
489 *
490 * @see #getLeftArrow()
491 */
492 public void setLeftArrow(Shape arrow) {
493 if (arrow == null) {
494 throw new IllegalArgumentException("Null 'arrow' argument.");
495 }
496 this.leftArrow = arrow;
497 notifyListeners(new AxisChangeEvent(this));
498 }
499
500 /**
501 * Returns a shape that can be displayed as an arrow pointing right at the
502 * end of an axis line.
503 *
504 * @return A shape (never <code>null</code>).
505 *
506 * @see #setRightArrow(Shape)
507 */
508 public Shape getRightArrow() {
509 return this.rightArrow;
510 }
511
512 /**
513 * Sets the shape that can be displayed as an arrow pointing rightwards at
514 * the end of an axis line and sends an {@link AxisChangeEvent} to all
515 * registered listeners.
516 *
517 * @param arrow the arrow shape (<code>null</code> not permitted).
518 *
519 * @see #getRightArrow()
520 */
521 public void setRightArrow(Shape arrow) {
522 if (arrow == null) {
523 throw new IllegalArgumentException("Null 'arrow' argument.");
524 }
525 this.rightArrow = arrow;
526 notifyListeners(new AxisChangeEvent(this));
527 }
528
529 /**
530 * Draws an axis line at the current cursor position and edge.
531 *
532 * @param g2 the graphics device.
533 * @param cursor the cursor position.
534 * @param dataArea the data area.
535 * @param edge the edge.
536 */
537 protected void drawAxisLine(Graphics2D g2, double cursor,
538 Rectangle2D dataArea, RectangleEdge edge) {
539 Line2D axisLine = null;
540 if (edge == RectangleEdge.TOP) {
541 axisLine = new Line2D.Double(dataArea.getX(), cursor,
542 dataArea.getMaxX(), cursor);
543 }
544 else if (edge == RectangleEdge.BOTTOM) {
545 axisLine = new Line2D.Double(dataArea.getX(), cursor,
546 dataArea.getMaxX(), cursor);
547 }
548 else if (edge == RectangleEdge.LEFT) {
549 axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor,
550 dataArea.getMaxY());
551 }
552 else if (edge == RectangleEdge.RIGHT) {
553 axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor,
554 dataArea.getMaxY());
555 }
556 g2.setPaint(getAxisLinePaint());
557 g2.setStroke(getAxisLineStroke());
558 g2.draw(axisLine);
559
560 boolean drawUpOrRight = false;
561 boolean drawDownOrLeft = false;
562 if (this.positiveArrowVisible) {
563 if (this.inverted) {
564 drawDownOrLeft = true;
565 }
566 else {
567 drawUpOrRight = true;
568 }
569 }
570 if (this.negativeArrowVisible) {
571 if (this.inverted) {
572 drawUpOrRight = true;
573 }
574 else {
575 drawDownOrLeft = true;
576 }
577 }
578 if (drawUpOrRight) {
579 double x = 0.0;
580 double y = 0.0;
581 Shape arrow = null;
582 if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
583 x = dataArea.getMaxX();
584 y = cursor;
585 arrow = this.rightArrow;
586 }
587 else if (edge == RectangleEdge.LEFT
588 || edge == RectangleEdge.RIGHT) {
589 x = cursor;
590 y = dataArea.getMinY();
591 arrow = this.upArrow;
592 }
593
594 // draw the arrow...
595 AffineTransform transformer = new AffineTransform();
596 transformer.setToTranslation(x, y);
597 Shape shape = transformer.createTransformedShape(arrow);
598 g2.fill(shape);
599 g2.draw(shape);
600 }
601
602 if (drawDownOrLeft) {
603 double x = 0.0;
604 double y = 0.0;
605 Shape arrow = null;
606 if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
607 x = dataArea.getMinX();
608 y = cursor;
609 arrow = this.leftArrow;
610 }
611 else if (edge == RectangleEdge.LEFT
612 || edge == RectangleEdge.RIGHT) {
613 x = cursor;
614 y = dataArea.getMaxY();
615 arrow = this.downArrow;
616 }
617
618 // draw the arrow...
619 AffineTransform transformer = new AffineTransform();
620 transformer.setToTranslation(x, y);
621 Shape shape = transformer.createTransformedShape(arrow);
622 g2.fill(shape);
623 g2.draw(shape);
624 }
625
626 }
627
628 /**
629 * Calculates the anchor point for a tick label.
630 *
631 * @param tick the tick.
632 * @param cursor the cursor.
633 * @param dataArea the data area.
634 * @param edge the edge on which the axis is drawn.
635 *
636 * @return The x and y coordinates of the anchor point.
637 */
638 protected float[] calculateAnchorPoint(ValueTick tick,
639 double cursor,
640 Rectangle2D dataArea,
641 RectangleEdge edge) {
642
643 RectangleInsets insets = getTickLabelInsets();
644 float[] result = new float[2];
645 if (edge == RectangleEdge.TOP) {
646 result[0] = (float) valueToJava2D(tick.getValue(), dataArea, edge);
647 result[1] = (float) (cursor - insets.getBottom() - 2.0);
648 }
649 else if (edge == RectangleEdge.BOTTOM) {
650 result[0] = (float) valueToJava2D(tick.getValue(), dataArea, edge);
651 result[1] = (float) (cursor + insets.getTop() + 2.0);
652 }
653 else if (edge == RectangleEdge.LEFT) {
654 result[0] = (float) (cursor - insets.getLeft() - 2.0);
655 result[1] = (float) valueToJava2D(tick.getValue(), dataArea, edge);
656 }
657 else if (edge == RectangleEdge.RIGHT) {
658 result[0] = (float) (cursor + insets.getRight() + 2.0);
659 result[1] = (float) valueToJava2D(tick.getValue(), dataArea, edge);
660 }
661 return result;
662 }
663
664 /**
665 * Draws the axis line, tick marks and tick mark labels.
666 *
667 * @param g2 the graphics device.
668 * @param cursor the cursor.
669 * @param plotArea the plot area.
670 * @param dataArea the data area.
671 * @param edge the edge that the axis is aligned with.
672 *
673 * @return The width or height used to draw the axis.
674 */
675 protected AxisState drawTickMarksAndLabels(Graphics2D g2,
676 double cursor, Rectangle2D plotArea, Rectangle2D dataArea,
677 RectangleEdge edge) {
678
679 AxisState state = new AxisState(cursor);
680
681 if (isAxisLineVisible()) {
682 drawAxisLine(g2, cursor, dataArea, edge);
683 }
684
685 List ticks = refreshTicks(g2, state, dataArea, edge);
686 state.setTicks(ticks);
687 g2.setFont(getTickLabelFont());
688 Iterator iterator = ticks.iterator();
689 while (iterator.hasNext()) {
690 ValueTick tick = (ValueTick) iterator.next();
691 if (isTickLabelsVisible()) {
692 g2.setPaint(getTickLabelPaint());
693 float[] anchorPoint = calculateAnchorPoint(tick, cursor,
694 dataArea, edge);
695 TextUtilities.drawRotatedString(tick.getText(), g2,
696 anchorPoint[0], anchorPoint[1], tick.getTextAnchor(),
697 tick.getAngle(), tick.getRotationAnchor());
698 }
699
700 if ((isTickMarksVisible() && tick.getTickType().equals(
701 TickType.MAJOR)) || (isMinorTickMarksVisible()
702 && tick.getTickType().equals(TickType.MINOR))) {
703
704 double ol = (tick.getTickType().equals(TickType.MINOR)) ?
705 getMinorTickMarkOutsideLength() : getTickMarkOutsideLength();
706
707 double il = (tick.getTickType().equals(TickType.MINOR)) ?
708 getMinorTickMarkInsideLength() : getTickMarkInsideLength();
709
710 float xx = (float) valueToJava2D(tick.getValue(), dataArea,
711 edge);
712 Line2D mark = null;
713 g2.setStroke(getTickMarkStroke());
714 g2.setPaint(getTickMarkPaint());
715 if (edge == RectangleEdge.LEFT) {
716 mark = new Line2D.Double(cursor - ol, xx, cursor + il, xx);
717 }
718 else if (edge == RectangleEdge.RIGHT) {
719 mark = new Line2D.Double(cursor + ol, xx, cursor - il, xx);
720 }
721 else if (edge == RectangleEdge.TOP) {
722 mark = new Line2D.Double(xx, cursor - ol, xx, cursor + il);
723 }
724 else if (edge == RectangleEdge.BOTTOM) {
725 mark = new Line2D.Double(xx, cursor + ol, xx, cursor - il);
726 }
727 g2.draw(mark);
728 }
729 }
730
731 // need to work out the space used by the tick labels...
732 // so we can update the cursor...
733 double used = 0.0;
734 if (isTickLabelsVisible()) {
735 if (edge == RectangleEdge.LEFT) {
736 used += findMaximumTickLabelWidth(ticks, g2, plotArea,
737 isVerticalTickLabels());
738 state.cursorLeft(used);
739 }
740 else if (edge == RectangleEdge.RIGHT) {
741 used = findMaximumTickLabelWidth(ticks, g2, plotArea,
742 isVerticalTickLabels());
743 state.cursorRight(used);
744 }
745 else if (edge == RectangleEdge.TOP) {
746 used = findMaximumTickLabelHeight(ticks, g2, plotArea,
747 isVerticalTickLabels());
748 state.cursorUp(used);
749 }
750 else if (edge == RectangleEdge.BOTTOM) {
751 used = findMaximumTickLabelHeight(ticks, g2, plotArea,
752 isVerticalTickLabels());
753 state.cursorDown(used);
754 }
755 }
756
757 return state;
758 }
759
760 /**
761 * Returns the space required to draw the axis.
762 *
763 * @param g2 the graphics device.
764 * @param plot the plot that the axis belongs to.
765 * @param plotArea the area within which the plot should be drawn.
766 * @param edge the axis location.
767 * @param space the space already reserved (for other axes).
768 *
769 * @return The space required to draw the axis (including pre-reserved
770 * space).
771 */
772 public AxisSpace reserveSpace(Graphics2D g2, Plot plot,
773 Rectangle2D plotArea,
774 RectangleEdge edge, AxisSpace space) {
775
776 // create a new space object if one wasn't supplied...
777 if (space == null) {
778 space = new AxisSpace();
779 }
780
781 // if the axis is not visible, no additional space is required...
782 if (!isVisible()) {
783 return space;
784 }
785
786 // if the axis has a fixed dimension, return it...
787 double dimension = getFixedDimension();
788 if (dimension > 0.0) {
789 space.ensureAtLeast(dimension, edge);
790 }
791
792 // calculate the max size of the tick labels (if visible)...
793 double tickLabelHeight = 0.0;
794 double tickLabelWidth = 0.0;
795 if (isTickLabelsVisible()) {
796 g2.setFont(getTickLabelFont());
797 List ticks = refreshTicks(g2, new AxisState(), plotArea, edge);
798 if (RectangleEdge.isTopOrBottom(edge)) {
799 tickLabelHeight = findMaximumTickLabelHeight(ticks, g2,
800 plotArea, isVerticalTickLabels());
801 }
802 else if (RectangleEdge.isLeftOrRight(edge)) {
803 tickLabelWidth = findMaximumTickLabelWidth(ticks, g2, plotArea,
804 isVerticalTickLabels());
805 }
806 }
807
808 // get the axis label size and update the space object...
809 Rectangle2D labelEnclosure = getLabelEnclosure(g2, edge);
810 double labelHeight = 0.0;
811 double labelWidth = 0.0;
812 if (RectangleEdge.isTopOrBottom(edge)) {
813 labelHeight = labelEnclosure.getHeight();
814 space.add(labelHeight + tickLabelHeight, edge);
815 }
816 else if (RectangleEdge.isLeftOrRight(edge)) {
817 labelWidth = labelEnclosure.getWidth();
818 space.add(labelWidth + tickLabelWidth, edge);
819 }
820
821 return space;
822
823 }
824
825 /**
826 * A utility method for determining the height of the tallest tick label.
827 *
828 * @param ticks the ticks.
829 * @param g2 the graphics device.
830 * @param drawArea the area within which the plot and axes should be drawn.
831 * @param vertical a flag that indicates whether or not the tick labels
832 * are 'vertical'.
833 *
834 * @return The height of the tallest tick label.
835 */
836 protected double findMaximumTickLabelHeight(List ticks,
837 Graphics2D g2,
838 Rectangle2D drawArea,
839 boolean vertical) {
840
841 RectangleInsets insets = getTickLabelInsets();
842 Font font = getTickLabelFont();
843 double maxHeight = 0.0;
844 if (vertical) {
845 FontMetrics fm = g2.getFontMetrics(font);
846 Iterator iterator = ticks.iterator();
847 while (iterator.hasNext()) {
848 Tick tick = (Tick) iterator.next();
849 Rectangle2D labelBounds = TextUtilities.getTextBounds(
850 tick.getText(), g2, fm);
851 if (labelBounds.getWidth() + insets.getTop()
852 + insets.getBottom() > maxHeight) {
853 maxHeight = labelBounds.getWidth()
854 + insets.getTop() + insets.getBottom();
855 }
856 }
857 }
858 else {
859 LineMetrics metrics = font.getLineMetrics("ABCxyz",
860 g2.getFontRenderContext());
861 maxHeight = metrics.getHeight()
862 + insets.getTop() + insets.getBottom();
863 }
864 return maxHeight;
865
866 }
867
868 /**
869 * A utility method for determining the width of the widest tick label.
870 *
871 * @param ticks the ticks.
872 * @param g2 the graphics device.
873 * @param drawArea the area within which the plot and axes should be drawn.
874 * @param vertical a flag that indicates whether or not the tick labels
875 * are 'vertical'.
876 *
877 * @return The width of the tallest tick label.
878 */
879 protected double findMaximumTickLabelWidth(List ticks,
880 Graphics2D g2,
881 Rectangle2D drawArea,
882 boolean vertical) {
883
884 RectangleInsets insets = getTickLabelInsets();
885 Font font = getTickLabelFont();
886 double maxWidth = 0.0;
887 if (!vertical) {
888 FontMetrics fm = g2.getFontMetrics(font);
889 Iterator iterator = ticks.iterator();
890 while (iterator.hasNext()) {
891 Tick tick = (Tick) iterator.next();
892 Rectangle2D labelBounds = TextUtilities.getTextBounds(
893 tick.getText(), g2, fm);
894 if (labelBounds.getWidth() + insets.getLeft()
895 + insets.getRight() > maxWidth) {
896 maxWidth = labelBounds.getWidth()
897 + insets.getLeft() + insets.getRight();
898 }
899 }
900 }
901 else {
902 LineMetrics metrics = font.getLineMetrics("ABCxyz",
903 g2.getFontRenderContext());
904 maxWidth = metrics.getHeight()
905 + insets.getTop() + insets.getBottom();
906 }
907 return maxWidth;
908
909 }
910
911 /**
912 * Returns a flag that controls the direction of values on the axis.
913 * <P>
914 * For a regular axis, values increase from left to right (for a horizontal
915 * axis) and bottom to top (for a vertical axis). When the axis is
916 * 'inverted', the values increase in the opposite direction.
917 *
918 * @return The flag.
919 *
920 * @see #setInverted(boolean)
921 */
922 public boolean isInverted() {
923 return this.inverted;
924 }
925
926 /**
927 * Sets a flag that controls the direction of values on the axis, and
928 * notifies registered listeners that the axis has changed.
929 *
930 * @param flag the flag.
931 *
932 * @see #isInverted()
933 */
934 public void setInverted(boolean flag) {
935
936 if (this.inverted != flag) {
937 this.inverted = flag;
938 notifyListeners(new AxisChangeEvent(this));
939 }
940
941 }
942
943 /**
944 * Returns the flag that controls whether or not the axis range is
945 * automatically adjusted to fit the data values.
946 *
947 * @return The flag.
948 *
949 * @see #setAutoRange(boolean)
950 */
951 public boolean isAutoRange() {
952 return this.autoRange;
953 }
954
955 /**
956 * Sets a flag that determines whether or not the axis range is
957 * automatically adjusted to fit the data, and notifies registered
958 * listeners that the axis has been modified.
959 *
960 * @param auto the new value of the flag.
961 *
962 * @see #isAutoRange()
963 */
964 public void setAutoRange(boolean auto) {
965 setAutoRange(auto, true);
966 }
967
968 /**
969 * Sets the auto range attribute. If the <code>notify</code> flag is set,
970 * an {@link AxisChangeEvent} is sent to registered listeners.
971 *
972 * @param auto the flag.
973 * @param notify notify listeners?
974 *
975 * @see #isAutoRange()
976 */
977 protected void setAutoRange(boolean auto, boolean notify) {
978 if (this.autoRange != auto) {
979 this.autoRange = auto;
980 if (this.autoRange) {
981 autoAdjustRange();
982 }
983 if (notify) {
984 notifyListeners(new AxisChangeEvent(this));
985 }
986 }
987 }
988
989 /**
990 * Returns the minimum size allowed for the axis range when it is
991 * automatically calculated.
992 *
993 * @return The minimum range.
994 *
995 * @see #setAutoRangeMinimumSize(double)
996 */
997 public double getAutoRangeMinimumSize() {
998 return this.autoRangeMinimumSize;
999 }
1000
1001 /**
1002 * Sets the auto range minimum size and sends an {@link AxisChangeEvent}
1003 * to all registered listeners.
1004 *
1005 * @param size the size.
1006 *
1007 * @see #getAutoRangeMinimumSize()
1008 */
1009 public void setAutoRangeMinimumSize(double size) {
1010 setAutoRangeMinimumSize(size, true);
1011 }
1012
1013 /**
1014 * Sets the minimum size allowed for the axis range when it is
1015 * automatically calculated.
1016 * <p>
1017 * If requested, an {@link AxisChangeEvent} is forwarded to all registered
1018 * listeners.
1019 *
1020 * @param size the new minimum.
1021 * @param notify notify listeners?
1022 */
1023 public void setAutoRangeMinimumSize(double size, boolean notify) {
1024 if (size <= 0.0) {
1025 throw new IllegalArgumentException(
1026 "NumberAxis.setAutoRangeMinimumSize(double): must be > 0.0.");
1027 }
1028 if (this.autoRangeMinimumSize != size) {
1029 this.autoRangeMinimumSize = size;
1030 if (this.autoRange) {
1031 autoAdjustRange();
1032 }
1033 if (notify) {
1034 notifyListeners(new AxisChangeEvent(this));
1035 }
1036 }
1037
1038 }
1039
1040 /**
1041 * Returns the default auto range.
1042 *
1043 * @return The default auto range (never <code>null</code>).
1044 *
1045 * @see #setDefaultAutoRange(Range)
1046 *
1047 * @since 1.0.5
1048 */
1049 public Range getDefaultAutoRange() {
1050 return this.defaultAutoRange;
1051 }
1052
1053 /**
1054 * Sets the default auto range and sends an {@link AxisChangeEvent} to all
1055 * registered listeners.
1056 *
1057 * @param range the range (<code>null</code> not permitted).
1058 *
1059 * @see #getDefaultAutoRange()
1060 *
1061 * @since 1.0.5
1062 */
1063 public void setDefaultAutoRange(Range range) {
1064 if (range == null) {
1065 throw new IllegalArgumentException("Null 'range' argument.");
1066 }
1067 this.defaultAutoRange = range;
1068 notifyListeners(new AxisChangeEvent(this));
1069 }
1070
1071 /**
1072 * Returns the lower margin for the axis, expressed as a percentage of the
1073 * axis range. This controls the space added to the lower end of the axis
1074 * when the axis range is automatically calculated (it is ignored when the
1075 * axis range is set explicitly). The default value is 0.05 (five percent).
1076 *
1077 * @return The lower margin.
1078 *
1079 * @see #setLowerMargin(double)
1080 */
1081 public double getLowerMargin() {
1082 return this.lowerMargin;
1083 }
1084
1085 /**
1086 * Sets the lower margin for the axis (as a percentage of the axis range)
1087 * and sends an {@link AxisChangeEvent} to all registered listeners. This
1088 * margin is added only when the axis range is auto-calculated - if you set
1089 * the axis range manually, the margin is ignored.
1090 *
1091 * @param margin the margin percentage (for example, 0.05 is five percent).
1092 *
1093 * @see #getLowerMargin()
1094 * @see #setUpperMargin(double)
1095 */
1096 public void setLowerMargin(double margin) {
1097 this.lowerMargin = margin;
1098 if (isAutoRange()) {
1099 autoAdjustRange();
1100 }
1101 notifyListeners(new AxisChangeEvent(this));
1102 }
1103
1104 /**
1105 * Returns the upper margin for the axis, expressed as a percentage of the
1106 * axis range. This controls the space added to the lower end of the axis
1107 * when the axis range is automatically calculated (it is ignored when the
1108 * axis range is set explicitly). The default value is 0.05 (five percent).
1109 *
1110 * @return The upper margin.
1111 *
1112 * @see #setUpperMargin(double)
1113 */
1114 public double getUpperMargin() {
1115 return this.upperMargin;
1116 }
1117
1118 /**
1119 * Sets the upper margin for the axis (as a percentage of the axis range)
1120 * and sends an {@link AxisChangeEvent} to all registered listeners. This
1121 * margin is added only when the axis range is auto-calculated - if you set
1122 * the axis range manually, the margin is ignored.
1123 *
1124 * @param margin the margin percentage (for example, 0.05 is five percent).
1125 *
1126 * @see #getLowerMargin()
1127 * @see #setLowerMargin(double)
1128 */
1129 public void setUpperMargin(double margin) {
1130 this.upperMargin = margin;
1131 if (isAutoRange()) {
1132 autoAdjustRange();
1133 }
1134 notifyListeners(new AxisChangeEvent(this));
1135 }
1136
1137 /**
1138 * Returns the fixed auto range.
1139 *
1140 * @return The length.
1141 *
1142 * @see #setFixedAutoRange(double)
1143 */
1144 public double getFixedAutoRange() {
1145 return this.fixedAutoRange;
1146 }
1147
1148 /**
1149 * Sets the fixed auto range for the axis.
1150 *
1151 * @param length the range length.
1152 *
1153 * @see #getFixedAutoRange()
1154 */
1155 public void setFixedAutoRange(double length) {
1156 this.fixedAutoRange = length;
1157 if (isAutoRange()) {
1158 autoAdjustRange();
1159 }
1160 notifyListeners(new AxisChangeEvent(this));
1161 }
1162
1163 /**
1164 * Returns the lower bound of the axis range.
1165 *
1166 * @return The lower bound.
1167 *
1168 * @see #setLowerBound(double)
1169 */
1170 public double getLowerBound() {
1171 return this.range.getLowerBound();
1172 }
1173
1174 /**
1175 * Sets the lower bound for the axis range. An {@link AxisChangeEvent} is
1176 * sent to all registered listeners.
1177 *
1178 * @param min the new minimum.
1179 *
1180 * @see #getLowerBound()
1181 */
1182 public void setLowerBound(double min) {
1183 if (this.range.getUpperBound() > min) {
1184 setRange(new Range(min, this.range.getUpperBound()));
1185 }
1186 else {
1187 setRange(new Range(min, min + 1.0));
1188 }
1189 }
1190
1191 /**
1192 * Returns the upper bound for the axis range.
1193 *
1194 * @return The upper bound.
1195 *
1196 * @see #setUpperBound(double)
1197 */
1198 public double getUpperBound() {
1199 return this.range.getUpperBound();
1200 }
1201
1202 /**
1203 * Sets the upper bound for the axis range, and sends an
1204 * {@link AxisChangeEvent} to all registered listeners.
1205 *
1206 * @param max the new maximum.
1207 *
1208 * @see #getUpperBound()
1209 */
1210 public void setUpperBound(double max) {
1211 if (this.range.getLowerBound() < max) {
1212 setRange(new Range(this.range.getLowerBound(), max));
1213 }
1214 else {
1215 setRange(max - 1.0, max);
1216 }
1217 }
1218
1219 /**
1220 * Returns the range for the axis.
1221 *
1222 * @return The axis range (never <code>null</code>).
1223 *
1224 * @see #setRange(Range)
1225 */
1226 public Range getRange() {
1227 return this.range;
1228 }
1229
1230 /**
1231 * Sets the range attribute and sends an {@link AxisChangeEvent} to all
1232 * registered listeners. As a side-effect, the auto-range flag is set to
1233 * <code>false</code>.
1234 *
1235 * @param range the range (<code>null</code> not permitted).
1236 *
1237 * @see #getRange()
1238 */
1239 public void setRange(Range range) {
1240 // defer argument checking
1241 setRange(range, true, true);
1242 }
1243
1244 /**
1245 * Sets the range for the axis, if requested, sends an
1246 * {@link AxisChangeEvent} to all registered listeners. As a side-effect,
1247 * the auto-range flag is set to <code>false</code> (optional).
1248 *
1249 * @param range the range (<code>null</code> not permitted).
1250 * @param turnOffAutoRange a flag that controls whether or not the auto
1251 * range is turned off.
1252 * @param notify a flag that controls whether or not listeners are
1253 * notified.
1254 *
1255 * @see #getRange()
1256 */
1257 public void setRange(Range range, boolean turnOffAutoRange,
1258 boolean notify) {
1259 if (range == null) {
1260 throw new IllegalArgumentException("Null 'range' argument.");
1261 }
1262 if (turnOffAutoRange) {
1263 this.autoRange = false;
1264 }
1265 this.range = range;
1266 if (notify) {
1267 notifyListeners(new AxisChangeEvent(this));
1268 }
1269 }
1270
1271 /**
1272 * Sets the axis range and sends an {@link AxisChangeEvent} to all
1273 * registered listeners. As a side-effect, the auto-range flag is set to
1274 * <code>false</code>.
1275 *
1276 * @param lower the lower axis limit.
1277 * @param upper the upper axis limit.
1278 *
1279 * @see #getRange()
1280 * @see #setRange(Range)
1281 */
1282 public void setRange(double lower, double upper) {
1283 setRange(new Range(lower, upper));
1284 }
1285
1286 /**
1287 * Sets the range for the axis (after first adding the current margins to
1288 * the specified range) and sends an {@link AxisChangeEvent} to all
1289 * registered listeners.
1290 *
1291 * @param range the range (<code>null</code> not permitted).
1292 */
1293 public void setRangeWithMargins(Range range) {
1294 setRangeWithMargins(range, true, true);
1295 }
1296
1297 /**
1298 * Sets the range for the axis after first adding the current margins to
1299 * the range and, if requested, sends an {@link AxisChangeEvent} to all
1300 * registered listeners. As a side-effect, the auto-range flag is set to
1301 * <code>false</code> (optional).
1302 *
1303 * @param range the range (excluding margins, <code>null</code> not
1304 * permitted).
1305 * @param turnOffAutoRange a flag that controls whether or not the auto
1306 * range is turned off.
1307 * @param notify a flag that controls whether or not listeners are
1308 * notified.
1309 */
1310 public void setRangeWithMargins(Range range, boolean turnOffAutoRange,
1311 boolean notify) {
1312 if (range == null) {
1313 throw new IllegalArgumentException("Null 'range' argument.");
1314 }
1315 setRange(Range.expand(range, getLowerMargin(), getUpperMargin()),
1316 turnOffAutoRange, notify);
1317 }
1318
1319 /**
1320 * Sets the axis range (after first adding the current margins to the
1321 * range) and sends an {@link AxisChangeEvent} to all registered listeners.
1322 * As a side-effect, the auto-range flag is set to <code>false</code>.
1323 *
1324 * @param lower the lower axis limit.
1325 * @param upper the upper axis limit.
1326 */
1327 public void setRangeWithMargins(double lower, double upper) {
1328 setRangeWithMargins(new Range(lower, upper));
1329 }
1330
1331 /**
1332 * Sets the axis range, where the new range is 'size' in length, and
1333 * centered on 'value'.
1334 *
1335 * @param value the central value.
1336 * @param length the range length.
1337 */
1338 public void setRangeAboutValue(double value, double length) {
1339 setRange(new Range(value - length / 2, value + length / 2));
1340 }
1341
1342 /**
1343 * Returns a flag indicating whether or not the tick unit is automatically
1344 * selected from a range of standard tick units.
1345 *
1346 * @return A flag indicating whether or not the tick unit is automatically
1347 * selected.
1348 *
1349 * @see #setAutoTickUnitSelection(boolean)
1350 */
1351 public boolean isAutoTickUnitSelection() {
1352 return this.autoTickUnitSelection;
1353 }
1354
1355 /**
1356 * Sets a flag indicating whether or not the tick unit is automatically
1357 * selected from a range of standard tick units. If the flag is changed,
1358 * registered listeners are notified that the chart has changed.
1359 *
1360 * @param flag the new value of the flag.
1361 *
1362 * @see #isAutoTickUnitSelection()
1363 */
1364 public void setAutoTickUnitSelection(boolean flag) {
1365 setAutoTickUnitSelection(flag, true);
1366 }
1367
1368 /**
1369 * Sets a flag indicating whether or not the tick unit is automatically
1370 * selected from a range of standard tick units.
1371 *
1372 * @param flag the new value of the flag.
1373 * @param notify notify listeners?
1374 *
1375 * @see #isAutoTickUnitSelection()
1376 */
1377 public void setAutoTickUnitSelection(boolean flag, boolean notify) {
1378
1379 if (this.autoTickUnitSelection != flag) {
1380 this.autoTickUnitSelection = flag;
1381 if (notify) {
1382 notifyListeners(new AxisChangeEvent(this));
1383 }
1384 }
1385 }
1386
1387 /**
1388 * Returns the source for obtaining standard tick units for the axis.
1389 *
1390 * @return The source (possibly <code>null</code>).
1391 *
1392 * @see #setStandardTickUnits(TickUnitSource)
1393 */
1394 public TickUnitSource getStandardTickUnits() {
1395 return this.standardTickUnits;
1396 }
1397
1398 /**
1399 * Sets the source for obtaining standard tick units for the axis and sends
1400 * an {@link AxisChangeEvent} to all registered listeners. The axis will
1401 * try to select the smallest tick unit from the source that does not cause
1402 * the tick labels to overlap (see also the
1403 * {@link #setAutoTickUnitSelection(boolean)} method.
1404 *
1405 * @param source the source for standard tick units (<code>null</code>
1406 * permitted).
1407 *
1408 * @see #getStandardTickUnits()
1409 */
1410 public void setStandardTickUnits(TickUnitSource source) {
1411 this.standardTickUnits = source;
1412 notifyListeners(new AxisChangeEvent(this));
1413 }
1414
1415 /**
1416 * Returns the number of minor tick marks to display.
1417 *
1418 * @return The number of minor tick marks to display.
1419 *
1420 * @see #setMinorTickCount(int)
1421 *
1422 * @since 1.0.12
1423 */
1424 public int getMinorTickCount() {
1425 return this.minorTickCount;
1426 }
1427
1428 /**
1429 * Sets the number of minor tick marks to display, and sends an
1430 * {@link AxisChangeEvent} to all registered listeners.
1431 *
1432 * @param count the count.
1433 *
1434 * @see #getMinorTickCount()
1435 *
1436 * @since 1.0.12
1437 */
1438 public void setMinorTickCount(int count) {
1439 if (count <= 0) {
1440 throw new IllegalArgumentException("Requires 'count' > 0.");
1441 }
1442 this.minorTickCount = count;
1443 notifyListeners(new AxisChangeEvent(this));
1444 }
1445
1446 /**
1447 * Converts a data value to a coordinate in Java2D space, assuming that the
1448 * axis runs along one edge of the specified dataArea.
1449 * <p>
1450 * Note that it is possible for the coordinate to fall outside the area.
1451 *
1452 * @param value the data value.
1453 * @param area the area for plotting the data.
1454 * @param edge the edge along which the axis lies.
1455 *
1456 * @return The Java2D coordinate.
1457 *
1458 * @see #java2DToValue(double, Rectangle2D, RectangleEdge)
1459 */
1460 public abstract double valueToJava2D(double value, Rectangle2D area,
1461 RectangleEdge edge);
1462
1463 /**
1464 * Converts a length in data coordinates into the corresponding length in
1465 * Java2D coordinates.
1466 *
1467 * @param length the length.
1468 * @param area the plot area.
1469 * @param edge the edge along which the axis lies.
1470 *
1471 * @return The length in Java2D coordinates.
1472 */
1473 public double lengthToJava2D(double length, Rectangle2D area,
1474 RectangleEdge edge) {
1475 double zero = valueToJava2D(0.0, area, edge);
1476 double l = valueToJava2D(length, area, edge);
1477 return Math.abs(l - zero);
1478 }
1479
1480 /**
1481 * Converts a coordinate in Java2D space to the corresponding data value,
1482 * assuming that the axis runs along one edge of the specified dataArea.
1483 *
1484 * @param java2DValue the coordinate in Java2D space.
1485 * @param area the area in which the data is plotted.
1486 * @param edge the edge along which the axis lies.
1487 *
1488 * @return The data value.
1489 *
1490 * @see #valueToJava2D(double, Rectangle2D, RectangleEdge)
1491 */
1492 public abstract double java2DToValue(double java2DValue,
1493 Rectangle2D area,
1494 RectangleEdge edge);
1495
1496 /**
1497 * Automatically sets the axis range to fit the range of values in the
1498 * dataset. Sometimes this can depend on the renderer used as well (for
1499 * example, the renderer may "stack" values, requiring an axis range
1500 * greater than otherwise necessary).
1501 */
1502 protected abstract void autoAdjustRange();
1503
1504 /**
1505 * Centers the axis range about the specified value and sends an
1506 * {@link AxisChangeEvent} to all registered listeners.
1507 *
1508 * @param value the center value.
1509 */
1510 public void centerRange(double value) {
1511
1512 double central = this.range.getCentralValue();
1513 Range adjusted = new Range(this.range.getLowerBound() + value - central,
1514 this.range.getUpperBound() + value - central);
1515 setRange(adjusted);
1516
1517 }
1518
1519 /**
1520 * Increases or decreases the axis range by the specified percentage about
1521 * the central value and sends an {@link AxisChangeEvent} to all registered
1522 * listeners.
1523 * <P>
1524 * To double the length of the axis range, use 200% (2.0).
1525 * To halve the length of the axis range, use 50% (0.5).
1526 *
1527 * @param percent the resize factor.
1528 *
1529 * @see #resizeRange(double, double)
1530 */
1531 public void resizeRange(double percent) {
1532 resizeRange(percent, this.range.getCentralValue());
1533 }
1534
1535 /**
1536 * Increases or decreases the axis range by the specified percentage about
1537 * the specified anchor value and sends an {@link AxisChangeEvent} to all
1538 * registered listeners.
1539 * <P>
1540 * To double the length of the axis range, use 200% (2.0).
1541 * To halve the length of the axis range, use 50% (0.5).
1542 *
1543 * @param percent the resize factor.
1544 * @param anchorValue the new central value after the resize.
1545 *
1546 * @see #resizeRange(double)
1547 */
1548 public void resizeRange(double percent, double anchorValue) {
1549 if (percent > 0.0) {
1550 double halfLength = this.range.getLength() * percent / 2;
1551 Range adjusted = new Range(anchorValue - halfLength,
1552 anchorValue + halfLength);
1553 setRange(adjusted);
1554 }
1555 else {
1556 setAutoRange(true);
1557 }
1558 }
1559
1560 /**
1561 * Zooms in on the current range.
1562 *
1563 * @param lowerPercent the new lower bound.
1564 * @param upperPercent the new upper bound.
1565 */
1566 public void zoomRange(double lowerPercent, double upperPercent) {
1567 double start = this.range.getLowerBound();
1568 double length = this.range.getLength();
1569 Range adjusted = null;
1570 if (isInverted()) {
1571 adjusted = new Range(start + (length * (1 - upperPercent)),
1572 start + (length * (1 - lowerPercent)));
1573 }
1574 else {
1575 adjusted = new Range(start + length * lowerPercent,
1576 start + length * upperPercent);
1577 }
1578 setRange(adjusted);
1579 }
1580
1581 /**
1582 * Returns the auto tick index.
1583 *
1584 * @return The auto tick index.
1585 *
1586 * @see #setAutoTickIndex(int)
1587 */
1588 protected int getAutoTickIndex() {
1589 return this.autoTickIndex;
1590 }
1591
1592 /**
1593 * Sets the auto tick index.
1594 *
1595 * @param index the new value.
1596 *
1597 * @see #getAutoTickIndex()
1598 */
1599 protected void setAutoTickIndex(int index) {
1600 this.autoTickIndex = index;
1601 }
1602
1603 /**
1604 * Tests the axis for equality with an arbitrary object.
1605 *
1606 * @param obj the object (<code>null</code> permitted).
1607 *
1608 * @return <code>true</code> or <code>false</code>.
1609 */
1610 public boolean equals(Object obj) {
1611 if (obj == this) {
1612 return true;
1613 }
1614 if (!(obj instanceof ValueAxis)) {
1615 return false;
1616 }
1617 ValueAxis that = (ValueAxis) obj;
1618 if (this.positiveArrowVisible != that.positiveArrowVisible) {
1619 return false;
1620 }
1621 if (this.negativeArrowVisible != that.negativeArrowVisible) {
1622 return false;
1623 }
1624 if (this.inverted != that.inverted) {
1625 return false;
1626 }
1627 if (!ObjectUtilities.equal(this.range, that.range)) {
1628 return false;
1629 }
1630 if (this.autoRange != that.autoRange) {
1631 return false;
1632 }
1633 if (this.autoRangeMinimumSize != that.autoRangeMinimumSize) {
1634 return false;
1635 }
1636 if (!this.defaultAutoRange.equals(that.defaultAutoRange)) {
1637 return false;
1638 }
1639 if (this.upperMargin != that.upperMargin) {
1640 return false;
1641 }
1642 if (this.lowerMargin != that.lowerMargin) {
1643 return false;
1644 }
1645 if (this.fixedAutoRange != that.fixedAutoRange) {
1646 return false;
1647 }
1648 if (this.autoTickUnitSelection != that.autoTickUnitSelection) {
1649 return false;
1650 }
1651 if (!ObjectUtilities.equal(this.standardTickUnits,
1652 that.standardTickUnits)) {
1653 return false;
1654 }
1655 if (this.verticalTickLabels != that.verticalTickLabels) {
1656 return false;
1657 }
1658 if (this.minorTickCount != that.minorTickCount) {
1659 return false;
1660 }
1661 return super.equals(obj);
1662 }
1663
1664 /**
1665 * Returns a clone of the object.
1666 *
1667 * @return A clone.
1668 *
1669 * @throws CloneNotSupportedException if some component of the axis does
1670 * not support cloning.
1671 */
1672 public Object clone() throws CloneNotSupportedException {
1673 ValueAxis clone = (ValueAxis) super.clone();
1674 return clone;
1675 }
1676
1677 /**
1678 * Provides serialization support.
1679 *
1680 * @param stream the output stream.
1681 *
1682 * @throws IOException if there is an I/O error.
1683 */
1684 private void writeObject(ObjectOutputStream stream) throws IOException {
1685 stream.defaultWriteObject();
1686 SerialUtilities.writeShape(this.upArrow, stream);
1687 SerialUtilities.writeShape(this.downArrow, stream);
1688 SerialUtilities.writeShape(this.leftArrow, stream);
1689 SerialUtilities.writeShape(this.rightArrow, stream);
1690 }
1691
1692 /**
1693 * Provides serialization support.
1694 *
1695 * @param stream the input stream.
1696 *
1697 * @throws IOException if there is an I/O error.
1698 * @throws ClassNotFoundException if there is a classpath problem.
1699 */
1700 private void readObject(ObjectInputStream stream)
1701 throws IOException, ClassNotFoundException {
1702
1703 stream.defaultReadObject();
1704 this.upArrow = SerialUtilities.readShape(stream);
1705 this.downArrow = SerialUtilities.readShape(stream);
1706 this.leftArrow = SerialUtilities.readShape(stream);
1707 this.rightArrow = SerialUtilities.readShape(stream);
1708
1709 }
1710
1711 }