001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2009, 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 * LogAxis.java
029 * ------------
030 * (C) Copyright 2006-2009, by Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Andrew Mickish (patch 1868745);
034 * Peter Kolb (patches 1934255 and 2603321);
035 *
036 * Changes
037 * -------
038 * 24-Aug-2006 : Version 1 (DG);
039 * 22-Mar-2007 : Use defaultAutoArrange attribute (DG);
040 * 02-Aug-2007 : Fixed zooming bug, added support for margins (DG);
041 * 14-Feb-2008 : Changed default minorTickCount to 9 - see bug report
042 * 1892419 (DG);
043 * 15-Feb-2008 : Applied a variation of patch 1868745 by Andrew Mickish to
044 * fix a labelling bug when the axis appears at the top or
045 * right of the chart (DG);
046 * 19-Mar-2008 : Applied patch 1902418 by Andrew Mickish to fix bug in tick
047 * labels for vertical axis (DG);
048 * 26-Mar-2008 : Changed createTickLabel() method from private to protected -
049 * see patch 1918209 by Andrew Mickish (DG);
050 * 25-Sep-2008 : Moved minor tick fields up to superclass, see patch 1934255
051 * by Peter Kolb (DG);
052 * 14-Jan-2009 : Fetch minor ticks from TickUnit, and corrected
053 * createLogTickUnits() (DG);
054 * 21-Jan-2009 : No need to call setMinorTickCount() in constructor (DG);
055 * 19-Mar-2009 : Added entity support - see patch 2603321 by Peter Kolb (DG);
056 * 30-Mar-2009 : Added pan(double) method (DG);
057 *
058 */
059
060 package org.jfree.chart.axis;
061
062 import java.awt.Font;
063 import java.awt.FontMetrics;
064 import java.awt.Graphics2D;
065 import java.awt.font.FontRenderContext;
066 import java.awt.font.LineMetrics;
067 import java.awt.geom.Rectangle2D;
068 import java.text.DecimalFormat;
069 import java.text.NumberFormat;
070 import java.util.ArrayList;
071 import java.util.List;
072 import java.util.Locale;
073
074 import org.jfree.chart.event.AxisChangeEvent;
075 import org.jfree.chart.plot.Plot;
076 import org.jfree.chart.plot.PlotRenderingInfo;
077 import org.jfree.chart.plot.ValueAxisPlot;
078 import org.jfree.chart.util.LogFormat;
079 import org.jfree.data.Range;
080 import org.jfree.ui.RectangleEdge;
081 import org.jfree.ui.RectangleInsets;
082 import org.jfree.ui.TextAnchor;
083
084 /**
085 * A numerical axis that uses a logarithmic scale. The class is an
086 * alternative to the {@link LogarithmicAxis} class.
087 *
088 * @since 1.0.7
089 */
090 public class LogAxis extends ValueAxis {
091
092 /** The logarithm base. */
093 private double base = 10.0;
094
095 /** The logarithm of the base value - cached for performance. */
096 private double baseLog = Math.log(10.0);
097
098 /** The smallest value permitted on the axis. */
099 private double smallestValue = 1E-100;
100
101 /** The current tick unit. */
102 private NumberTickUnit tickUnit;
103
104 /** The override number format. */
105 private NumberFormat numberFormatOverride;
106
107 /**
108 * Creates a new <code>LogAxis</code> with no label.
109 */
110 public LogAxis() {
111 this(null);
112 }
113
114 /**
115 * Creates a new <code>LogAxis</code> with the given label.
116 *
117 * @param label the axis label (<code>null</code> permitted).
118 */
119 public LogAxis(String label) {
120 super(label, createLogTickUnits(Locale.getDefault()));
121 setDefaultAutoRange(new Range(0.01, 1.0));
122 this.tickUnit = new NumberTickUnit(1.0, new DecimalFormat("0.#"), 9);
123 }
124
125 /**
126 * Returns the base for the logarithm calculation.
127 *
128 * @return The base for the logarithm calculation.
129 *
130 * @see #setBase(double)
131 */
132 public double getBase() {
133 return this.base;
134 }
135
136 /**
137 * Sets the base for the logarithm calculation and sends an
138 * {@link AxisChangeEvent} to all registered listeners.
139 *
140 * @param base the base value (must be > 1.0).
141 *
142 * @see #getBase()
143 */
144 public void setBase(double base) {
145 if (base <= 1.0) {
146 throw new IllegalArgumentException("Requires 'base' > 1.0.");
147 }
148 this.base = base;
149 this.baseLog = Math.log(base);
150 notifyListeners(new AxisChangeEvent(this));
151 }
152
153 /**
154 * Returns the smallest value represented by the axis.
155 *
156 * @return The smallest value represented by the axis.
157 *
158 * @see #setSmallestValue(double)
159 */
160 public double getSmallestValue() {
161 return this.smallestValue;
162 }
163
164 /**
165 * Sets the smallest value represented by the axis and sends an
166 * {@link AxisChangeEvent} to all registered listeners.
167 *
168 * @param value the value.
169 *
170 * @see #getSmallestValue()
171 */
172 public void setSmallestValue(double value) {
173 if (value <= 0.0) {
174 throw new IllegalArgumentException("Requires 'value' > 0.0.");
175 }
176 this.smallestValue = value;
177 notifyListeners(new AxisChangeEvent(this));
178 }
179
180 /**
181 * Returns the current tick unit.
182 *
183 * @return The current tick unit.
184 *
185 * @see #setTickUnit(NumberTickUnit)
186 */
187 public NumberTickUnit getTickUnit() {
188 return this.tickUnit;
189 }
190
191 /**
192 * Sets the tick unit for the axis and sends an {@link AxisChangeEvent} to
193 * all registered listeners. A side effect of calling this method is that
194 * the "auto-select" feature for tick units is switched off (you can
195 * restore it using the {@link ValueAxis#setAutoTickUnitSelection(boolean)}
196 * method).
197 *
198 * @param unit the new tick unit (<code>null</code> not permitted).
199 *
200 * @see #getTickUnit()
201 */
202 public void setTickUnit(NumberTickUnit unit) {
203 // defer argument checking...
204 setTickUnit(unit, true, true);
205 }
206
207 /**
208 * Sets the tick unit for the axis and, if requested, sends an
209 * {@link AxisChangeEvent} to all registered listeners. In addition, an
210 * option is provided to turn off the "auto-select" feature for tick units
211 * (you can restore it using the
212 * {@link ValueAxis#setAutoTickUnitSelection(boolean)} method).
213 *
214 * @param unit the new tick unit (<code>null</code> not permitted).
215 * @param notify notify listeners?
216 * @param turnOffAutoSelect turn off the auto-tick selection?
217 *
218 * @see #getTickUnit()
219 */
220 public void setTickUnit(NumberTickUnit unit, boolean notify,
221 boolean turnOffAutoSelect) {
222
223 if (unit == null) {
224 throw new IllegalArgumentException("Null 'unit' argument.");
225 }
226 this.tickUnit = unit;
227 if (turnOffAutoSelect) {
228 setAutoTickUnitSelection(false, false);
229 }
230 if (notify) {
231 notifyListeners(new AxisChangeEvent(this));
232 }
233
234 }
235
236 /**
237 * Returns the number format override. If this is non-null, then it will
238 * be used to format the numbers on the axis.
239 *
240 * @return The number formatter (possibly <code>null</code>).
241 *
242 * @see #setNumberFormatOverride(NumberFormat)
243 */
244 public NumberFormat getNumberFormatOverride() {
245 return this.numberFormatOverride;
246 }
247
248 /**
249 * Sets the number format override. If this is non-null, then it will be
250 * used to format the numbers on the axis.
251 *
252 * @param formatter the number formatter (<code>null</code> permitted).
253 *
254 * @see #getNumberFormatOverride()
255 */
256 public void setNumberFormatOverride(NumberFormat formatter) {
257 this.numberFormatOverride = formatter;
258 notifyListeners(new AxisChangeEvent(this));
259 }
260
261 /**
262 * Calculates the log of the given value, using the current base.
263 *
264 * @param value the value.
265 *
266 * @return The log of the given value.
267 *
268 * @see #calculateValue(double)
269 * @see #getBase()
270 */
271 public double calculateLog(double value) {
272 return Math.log(value) / this.baseLog;
273 }
274
275 /**
276 * Calculates the value from a given log.
277 *
278 * @param log the log value (must be > 0.0).
279 *
280 * @return The value with the given log.
281 *
282 * @see #calculateLog(double)
283 * @see #getBase()
284 */
285 public double calculateValue(double log) {
286 return Math.pow(this.base, log);
287 }
288
289 /**
290 * Converts a Java2D coordinate to an axis value, assuming that the
291 * axis covers the specified <code>edge</code> of the <code>area</code>.
292 *
293 * @param java2DValue the Java2D coordinate.
294 * @param area the area.
295 * @param edge the edge that the axis belongs to.
296 *
297 * @return A value along the axis scale.
298 */
299 public double java2DToValue(double java2DValue, Rectangle2D area,
300 RectangleEdge edge) {
301
302 Range range = getRange();
303 double axisMin = calculateLog(range.getLowerBound());
304 double axisMax = calculateLog(range.getUpperBound());
305
306 double min = 0.0;
307 double max = 0.0;
308 if (RectangleEdge.isTopOrBottom(edge)) {
309 min = area.getX();
310 max = area.getMaxX();
311 }
312 else if (RectangleEdge.isLeftOrRight(edge)) {
313 min = area.getMaxY();
314 max = area.getY();
315 }
316 double log = 0.0;
317 if (isInverted()) {
318 log = axisMax - (java2DValue - min) / (max - min)
319 * (axisMax - axisMin);
320 }
321 else {
322 log = axisMin + (java2DValue - min) / (max - min)
323 * (axisMax - axisMin);
324 }
325 return calculateValue(log);
326 }
327
328 /**
329 * Converts a value on the axis scale to a Java2D coordinate relative to
330 * the given <code>area</code>, based on the axis running along the
331 * specified <code>edge</code>.
332 *
333 * @param value the data value.
334 * @param area the area.
335 * @param edge the edge.
336 *
337 * @return The Java2D coordinate corresponding to <code>value</code>.
338 */
339 public double valueToJava2D(double value, Rectangle2D area,
340 RectangleEdge edge) {
341
342 Range range = getRange();
343 double axisMin = calculateLog(range.getLowerBound());
344 double axisMax = calculateLog(range.getUpperBound());
345 value = calculateLog(value);
346
347 double min = 0.0;
348 double max = 0.0;
349 if (RectangleEdge.isTopOrBottom(edge)) {
350 min = area.getX();
351 max = area.getMaxX();
352 }
353 else if (RectangleEdge.isLeftOrRight(edge)) {
354 max = area.getMinY();
355 min = area.getMaxY();
356 }
357 if (isInverted()) {
358 return max
359 - ((value - axisMin) / (axisMax - axisMin)) * (max - min);
360 }
361 else {
362 return min
363 + ((value - axisMin) / (axisMax - axisMin)) * (max - min);
364 }
365 }
366
367 /**
368 * Configures the axis. This method is typically called when an axis
369 * is assigned to a new plot.
370 */
371 public void configure() {
372 if (isAutoRange()) {
373 autoAdjustRange();
374 }
375 }
376
377 /**
378 * Adjusts the axis range to match the data range that the axis is
379 * required to display.
380 */
381 protected void autoAdjustRange() {
382 Plot plot = getPlot();
383 if (plot == null) {
384 return; // no plot, no data
385 }
386
387 if (plot instanceof ValueAxisPlot) {
388 ValueAxisPlot vap = (ValueAxisPlot) plot;
389
390 Range r = vap.getDataRange(this);
391 if (r == null) {
392 r = getDefaultAutoRange();
393 }
394
395 double upper = r.getUpperBound();
396 double lower = Math.max(r.getLowerBound(), this.smallestValue);
397 double range = upper - lower;
398
399 // if fixed auto range, then derive lower bound...
400 double fixedAutoRange = getFixedAutoRange();
401 if (fixedAutoRange > 0.0) {
402 lower = Math.max(upper - fixedAutoRange, this.smallestValue);
403 }
404 else {
405 // ensure the autorange is at least <minRange> in size...
406 double minRange = getAutoRangeMinimumSize();
407 if (range < minRange) {
408 double expand = (minRange - range) / 2;
409 upper = upper + expand;
410 lower = lower - expand;
411 }
412
413 // apply the margins - these should apply to the exponent range
414 double logUpper = calculateLog(upper);
415 double logLower = calculateLog(lower);
416 double logRange = logUpper - logLower;
417 logUpper = logUpper + getUpperMargin() * logRange;
418 logLower = logLower - getLowerMargin() * logRange;
419 upper = calculateValue(logUpper);
420 lower = calculateValue(logLower);
421 }
422
423 setRange(new Range(lower, upper), false, false);
424 }
425
426 }
427
428 /**
429 * Draws the axis on a Java 2D graphics device (such as the screen or a
430 * printer).
431 *
432 * @param g2 the graphics device (<code>null</code> not permitted).
433 * @param cursor the cursor location (determines where to draw the axis).
434 * @param plotArea the area within which the axes and plot should be drawn.
435 * @param dataArea the area within which the data should be drawn.
436 * @param edge the axis location (<code>null</code> not permitted).
437 * @param plotState collects information about the plot
438 * (<code>null</code> permitted).
439 *
440 * @return The axis state (never <code>null</code>).
441 */
442 public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea,
443 Rectangle2D dataArea, RectangleEdge edge,
444 PlotRenderingInfo plotState) {
445
446 AxisState state = null;
447 // if the axis is not visible, don't draw it...
448 if (!isVisible()) {
449 state = new AxisState(cursor);
450 // even though the axis is not visible, we need ticks for the
451 // gridlines...
452 List ticks = refreshTicks(g2, state, dataArea, edge);
453 state.setTicks(ticks);
454 return state;
455 }
456 state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge);
457 state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state);
458 createAndAddEntity(cursor, state, dataArea, edge, plotState);
459 return state;
460 }
461
462 /**
463 * Calculates the positions of the tick labels for the axis, storing the
464 * results in the tick label list (ready for drawing).
465 *
466 * @param g2 the graphics device.
467 * @param state the axis state.
468 * @param dataArea the area in which the plot should be drawn.
469 * @param edge the location of the axis.
470 *
471 * @return A list of ticks.
472 *
473 */
474 public List refreshTicks(Graphics2D g2, AxisState state,
475 Rectangle2D dataArea, RectangleEdge edge) {
476
477 List result = new java.util.ArrayList();
478 if (RectangleEdge.isTopOrBottom(edge)) {
479 result = refreshTicksHorizontal(g2, dataArea, edge);
480 }
481 else if (RectangleEdge.isLeftOrRight(edge)) {
482 result = refreshTicksVertical(g2, dataArea, edge);
483 }
484 return result;
485
486 }
487
488 /**
489 * Returns a list of ticks for an axis at the top or bottom of the chart.
490 *
491 * @param g2 the graphics device.
492 * @param dataArea the data area.
493 * @param edge the edge.
494 *
495 * @return A list of ticks.
496 */
497 protected List refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea,
498 RectangleEdge edge) {
499
500 Range range = getRange();
501 List ticks = new ArrayList();
502 Font tickLabelFont = getTickLabelFont();
503 g2.setFont(tickLabelFont);
504 TextAnchor textAnchor;
505 if (edge == RectangleEdge.TOP) {
506 textAnchor = TextAnchor.BOTTOM_CENTER;
507 }
508 else {
509 textAnchor = TextAnchor.TOP_CENTER;
510 }
511
512 if (isAutoTickUnitSelection()) {
513 selectAutoTickUnit(g2, dataArea, edge);
514 }
515 int minorTickCount = this.tickUnit.getMinorTickCount();
516 double start = Math.floor(calculateLog(getLowerBound()));
517 double end = Math.ceil(calculateLog(getUpperBound()));
518 double current = start;
519 while (current <= end) {
520 double v = calculateValue(current);
521 if (range.contains(v)) {
522 ticks.add(new NumberTick(TickType.MAJOR, v, createTickLabel(v),
523 textAnchor, TextAnchor.CENTER, 0.0));
524 }
525 // add minor ticks (for gridlines)
526 double next = Math.pow(this.base, current
527 + this.tickUnit.getSize());
528 for (int i = 1; i < minorTickCount; i++) {
529 double minorV = v + i * ((next - v) / minorTickCount);
530 if (range.contains(minorV)) {
531 ticks.add(new NumberTick(TickType.MINOR, minorV, "",
532 textAnchor, TextAnchor.CENTER, 0.0));
533 }
534 }
535 current = current + this.tickUnit.getSize();
536 }
537 return ticks;
538 }
539
540 /**
541 * Returns a list of ticks for an axis at the left or right of the chart.
542 *
543 * @param g2 the graphics device.
544 * @param dataArea the data area.
545 * @param edge the edge.
546 *
547 * @return A list of ticks.
548 */
549 protected List refreshTicksVertical(Graphics2D g2, Rectangle2D dataArea,
550 RectangleEdge edge) {
551
552 Range range = getRange();
553 List ticks = new ArrayList();
554 Font tickLabelFont = getTickLabelFont();
555 g2.setFont(tickLabelFont);
556 TextAnchor textAnchor;
557 if (edge == RectangleEdge.RIGHT) {
558 textAnchor = TextAnchor.CENTER_LEFT;
559 }
560 else {
561 textAnchor = TextAnchor.CENTER_RIGHT;
562 }
563
564 if (isAutoTickUnitSelection()) {
565 selectAutoTickUnit(g2, dataArea, edge);
566 }
567 int minorTickCount = this.tickUnit.getMinorTickCount();
568 double start = Math.floor(calculateLog(getLowerBound()));
569 double end = Math.ceil(calculateLog(getUpperBound()));
570 double current = start;
571 while (current <= end) {
572 double v = calculateValue(current);
573 if (range.contains(v)) {
574 ticks.add(new NumberTick(TickType.MAJOR, v, createTickLabel(v),
575 textAnchor, TextAnchor.CENTER, 0.0));
576 }
577 // add minor ticks (for gridlines)
578 double next = Math.pow(this.base, current
579 + this.tickUnit.getSize());
580 for (int i = 1; i < minorTickCount; i++) {
581 double minorV = v + i * ((next - v) / minorTickCount);
582 if (range.contains(minorV)) {
583 ticks.add(new NumberTick(TickType.MINOR, minorV, "",
584 textAnchor, TextAnchor.CENTER, 0.0));
585 }
586 }
587 current = current + this.tickUnit.getSize();
588 }
589 return ticks;
590 }
591
592 /**
593 * Selects an appropriate tick value for the axis. The strategy is to
594 * display as many ticks as possible (selected from an array of 'standard'
595 * tick units) without the labels overlapping.
596 *
597 * @param g2 the graphics device.
598 * @param dataArea the area defined by the axes.
599 * @param edge the axis location.
600 *
601 * @since 1.0.7
602 */
603 protected void selectAutoTickUnit(Graphics2D g2, Rectangle2D dataArea,
604 RectangleEdge edge) {
605
606 if (RectangleEdge.isTopOrBottom(edge)) {
607 selectHorizontalAutoTickUnit(g2, dataArea, edge);
608 }
609 else if (RectangleEdge.isLeftOrRight(edge)) {
610 selectVerticalAutoTickUnit(g2, dataArea, edge);
611 }
612
613 }
614
615 /**
616 * Selects an appropriate tick value for the axis. The strategy is to
617 * display as many ticks as possible (selected from an array of 'standard'
618 * tick units) without the labels overlapping.
619 *
620 * @param g2 the graphics device.
621 * @param dataArea the area defined by the axes.
622 * @param edge the axis location.
623 *
624 * @since 1.0.7
625 */
626 protected void selectHorizontalAutoTickUnit(Graphics2D g2,
627 Rectangle2D dataArea, RectangleEdge edge) {
628
629 double tickLabelWidth = estimateMaximumTickLabelWidth(g2,
630 getTickUnit());
631
632 // start with the current tick unit...
633 TickUnitSource tickUnits = getStandardTickUnits();
634 TickUnit unit1 = tickUnits.getCeilingTickUnit(getTickUnit());
635 double unit1Width = exponentLengthToJava2D(unit1.getSize(), dataArea,
636 edge);
637
638 // then extrapolate...
639 double guess = (tickLabelWidth / unit1Width) * unit1.getSize();
640
641 NumberTickUnit unit2 = (NumberTickUnit)
642 tickUnits.getCeilingTickUnit(guess);
643 double unit2Width = exponentLengthToJava2D(unit2.getSize(), dataArea,
644 edge);
645
646 tickLabelWidth = estimateMaximumTickLabelWidth(g2, unit2);
647 if (tickLabelWidth > unit2Width) {
648 unit2 = (NumberTickUnit) tickUnits.getLargerTickUnit(unit2);
649 }
650
651 setTickUnit(unit2, false, false);
652
653 }
654
655 /**
656 * Converts a length in data coordinates into the corresponding length in
657 * Java2D coordinates.
658 *
659 * @param length the length.
660 * @param area the plot area.
661 * @param edge the edge along which the axis lies.
662 *
663 * @return The length in Java2D coordinates.
664 *
665 * @since 1.0.7
666 */
667 public double exponentLengthToJava2D(double length, Rectangle2D area,
668 RectangleEdge edge) {
669 double one = valueToJava2D(calculateValue(1.0), area, edge);
670 double l = valueToJava2D(calculateValue(length + 1.0), area, edge);
671 return Math.abs(l - one);
672 }
673
674 /**
675 * Selects an appropriate tick value for the axis. The strategy is to
676 * display as many ticks as possible (selected from an array of 'standard'
677 * tick units) without the labels overlapping.
678 *
679 * @param g2 the graphics device.
680 * @param dataArea the area in which the plot should be drawn.
681 * @param edge the axis location.
682 *
683 * @since 1.0.7
684 */
685 protected void selectVerticalAutoTickUnit(Graphics2D g2,
686 Rectangle2D dataArea,
687 RectangleEdge edge) {
688
689 double tickLabelHeight = estimateMaximumTickLabelHeight(g2);
690
691 // start with the current tick unit...
692 TickUnitSource tickUnits = getStandardTickUnits();
693 TickUnit unit1 = tickUnits.getCeilingTickUnit(getTickUnit());
694 double unitHeight = exponentLengthToJava2D(unit1.getSize(), dataArea,
695 edge);
696
697 // then extrapolate...
698 double guess = (tickLabelHeight / unitHeight) * unit1.getSize();
699
700 NumberTickUnit unit2 = (NumberTickUnit)
701 tickUnits.getCeilingTickUnit(guess);
702 double unit2Height = exponentLengthToJava2D(unit2.getSize(), dataArea,
703 edge);
704
705 tickLabelHeight = estimateMaximumTickLabelHeight(g2);
706 if (tickLabelHeight > unit2Height) {
707 unit2 = (NumberTickUnit) tickUnits.getLargerTickUnit(unit2);
708 }
709
710 setTickUnit(unit2, false, false);
711
712 }
713
714 /**
715 * Estimates the maximum tick label height.
716 *
717 * @param g2 the graphics device.
718 *
719 * @return The maximum height.
720 *
721 * @since 1.0.7
722 */
723 protected double estimateMaximumTickLabelHeight(Graphics2D g2) {
724
725 RectangleInsets tickLabelInsets = getTickLabelInsets();
726 double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();
727
728 Font tickLabelFont = getTickLabelFont();
729 FontRenderContext frc = g2.getFontRenderContext();
730 result += tickLabelFont.getLineMetrics("123", frc).getHeight();
731 return result;
732
733 }
734
735 /**
736 * Estimates the maximum width of the tick labels, assuming the specified
737 * tick unit is used.
738 * <P>
739 * Rather than computing the string bounds of every tick on the axis, we
740 * just look at two values: the lower bound and the upper bound for the
741 * axis. These two values will usually be representative.
742 *
743 * @param g2 the graphics device.
744 * @param unit the tick unit to use for calculation.
745 *
746 * @return The estimated maximum width of the tick labels.
747 *
748 * @since 1.0.7
749 */
750 protected double estimateMaximumTickLabelWidth(Graphics2D g2,
751 TickUnit unit) {
752
753 RectangleInsets tickLabelInsets = getTickLabelInsets();
754 double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();
755
756 if (isVerticalTickLabels()) {
757 // all tick labels have the same width (equal to the height of the
758 // font)...
759 FontRenderContext frc = g2.getFontRenderContext();
760 LineMetrics lm = getTickLabelFont().getLineMetrics("0", frc);
761 result += lm.getHeight();
762 }
763 else {
764 // look at lower and upper bounds...
765 FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
766 Range range = getRange();
767 double lower = range.getLowerBound();
768 double upper = range.getUpperBound();
769 String lowerStr = "";
770 String upperStr = "";
771 NumberFormat formatter = getNumberFormatOverride();
772 if (formatter != null) {
773 lowerStr = formatter.format(lower);
774 upperStr = formatter.format(upper);
775 }
776 else {
777 lowerStr = unit.valueToString(lower);
778 upperStr = unit.valueToString(upper);
779 }
780 double w1 = fm.stringWidth(lowerStr);
781 double w2 = fm.stringWidth(upperStr);
782 result += Math.max(w1, w2);
783 }
784
785 return result;
786
787 }
788
789 /**
790 * Zooms in on the current range.
791 *
792 * @param lowerPercent the new lower bound.
793 * @param upperPercent the new upper bound.
794 */
795 public void zoomRange(double lowerPercent, double upperPercent) {
796 Range range = getRange();
797 double start = range.getLowerBound();
798 double end = range.getUpperBound();
799 double log1 = calculateLog(start);
800 double log2 = calculateLog(end);
801 double length = log2 - log1;
802 Range adjusted = null;
803 if (isInverted()) {
804 double logA = log1 + length * (1 - upperPercent);
805 double logB = log1 + length * (1 - lowerPercent);
806 adjusted = new Range(calculateValue(logA), calculateValue(logB));
807 }
808 else {
809 double logA = log1 + length * lowerPercent;
810 double logB = log1 + length * upperPercent;
811 adjusted = new Range(calculateValue(logA), calculateValue(logB));
812 }
813 setRange(adjusted);
814 }
815
816 /**
817 * Slides the axis range by the specified percentage.
818 *
819 * @param percent the percentage.
820 *
821 * @since 1.0.13
822 */
823 public void pan(double percent) {
824 Range range = getRange();
825 double lower = range.getLowerBound();
826 double upper = range.getUpperBound();
827 double log1 = calculateLog(lower);
828 double log2 = calculateLog(upper);
829 double length = log2 - log1;
830 double adj = length * percent;
831 log1 = log1 + adj;
832 log2 = log2 + adj;
833 setRange(calculateValue(log1), calculateValue(log2));
834 }
835
836 /**
837 * Creates a tick label for the specified value. Note that this method
838 * was 'private' prior to version 1.0.10.
839 *
840 * @param value the value.
841 *
842 * @return The label.
843 *
844 * @since 1.0.10
845 */
846 protected String createTickLabel(double value) {
847 if (this.numberFormatOverride != null) {
848 return this.numberFormatOverride.format(value);
849 }
850 else {
851 return this.tickUnit.valueToString(value);
852 }
853 }
854
855 /**
856 * Tests this axis for equality with an arbitrary object.
857 *
858 * @param obj the object (<code>null</code> permitted).
859 *
860 * @return A boolean.
861 */
862 public boolean equals(Object obj) {
863 if (obj == this) {
864 return true;
865 }
866 if (!(obj instanceof LogAxis)) {
867 return false;
868 }
869 LogAxis that = (LogAxis) obj;
870 if (this.base != that.base) {
871 return false;
872 }
873 if (this.smallestValue != that.smallestValue) {
874 return false;
875 }
876 return super.equals(obj);
877 }
878
879 /**
880 * Returns a hash code for this instance.
881 *
882 * @return A hash code.
883 */
884 public int hashCode() {
885 int result = 193;
886 long temp = Double.doubleToLongBits(this.base);
887 result = 37 * result + (int) (temp ^ (temp >>> 32));
888 temp = Double.doubleToLongBits(this.smallestValue);
889 result = 37 * result + (int) (temp ^ (temp >>> 32));
890 if (this.numberFormatOverride != null) {
891 result = 37 * result + this.numberFormatOverride.hashCode();
892 }
893 result = 37 * result + this.tickUnit.hashCode();
894 return result;
895 }
896
897 /**
898 * Returns a collection of tick units for log (base 10) values.
899 * Uses a given Locale to create the DecimalFormats.
900 *
901 * @param locale the locale to use to represent Numbers.
902 *
903 * @return A collection of tick units for integer values.
904 *
905 * @since 1.0.7
906 */
907 public static TickUnitSource createLogTickUnits(Locale locale) {
908 TickUnits units = new TickUnits();
909 NumberFormat numberFormat = new LogFormat();
910 units.add(new NumberTickUnit(0.05, numberFormat, 2));
911 units.add(new NumberTickUnit(0.1, numberFormat, 10));
912 units.add(new NumberTickUnit(0.2, numberFormat, 2));
913 units.add(new NumberTickUnit(0.5, numberFormat, 5));
914 units.add(new NumberTickUnit(1, numberFormat, 10));
915 units.add(new NumberTickUnit(2, numberFormat, 10));
916 units.add(new NumberTickUnit(3, numberFormat, 15));
917 units.add(new NumberTickUnit(4, numberFormat, 20));
918 units.add(new NumberTickUnit(5, numberFormat, 25));
919 units.add(new NumberTickUnit(6, numberFormat));
920 units.add(new NumberTickUnit(7, numberFormat));
921 units.add(new NumberTickUnit(8, numberFormat));
922 units.add(new NumberTickUnit(9, numberFormat));
923 units.add(new NumberTickUnit(10, numberFormat));
924 return units;
925 }
926
927 }