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