Skip to content

Commit

Permalink
Fix for bug #64, exception on extreme zoom on NumberAxis
Browse files Browse the repository at this point in the history
  • Loading branch information
jfree committed Oct 24, 2020
1 parent 7881741 commit 6dc65fc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ History

##### Version 1.6.0 (not yet released)
- update required JDK to version 1.8;
- modify buffer in ChartPanel to handle high DPI displays (bug #170)
- modify buffer in ChartPanel to handle high DPI displays (bug #170);
- fix for exception on extreme zoom on NumberAxis (bug #64);
- added generics
- DefaultIntervalCategoryDataset no longer allows null keys in constructor (this
is a consequence of introducing generics)
Expand Down
36 changes: 22 additions & 14 deletions src/main/java/org/jfree/chart/axis/NumberAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,27 +776,36 @@ protected void selectHorizontalAutoTickUnit(Graphics2D g2,

TickUnit unit = getTickUnit();
TickUnitSource tickUnitSource = getStandardTickUnits();
// we should use the current tick unit if it gives a count in the range
// 2 to 40 otherwise just estimate one that will give a count <= 20

// we should start with the current tick unit if it gives a count in
// the range 3 to 40 otherwise estimate one that will give a count <= 10
double length = getRange().getLength();
int count = (int) (length / unit.getSize());
if (count < 2 || count > 40) {
unit = tickUnitSource.getCeilingTickUnit(length / 20);
if (count < 3 || count > 40) {
unit = tickUnitSource.getCeilingTickUnit(length / 10);
}
double tickLabelWidth = estimateMaximumTickLabelWidth(g2, unit);

// now consider the label size relative to the width of the tick unit
// and make a guess at the ideal size
TickUnit unit1 = tickUnitSource.getCeilingTickUnit(unit);
double tickLabelWidth = estimateMaximumTickLabelWidth(g2, unit1);
double unit1Width = lengthToJava2D(unit1.getSize(), dataArea, edge);

// then extrapolate...
NumberTickUnit unit2 = (NumberTickUnit) unit1;
double guess = (tickLabelWidth / unit1Width) * unit1.getSize();
NumberTickUnit unit2 = (NumberTickUnit)
tickUnitSource.getCeilingTickUnit(guess);
double unit2Width = lengthToJava2D(unit2.getSize(), dataArea, edge);

tickLabelWidth = estimateMaximumTickLabelWidth(g2, unit2);
if (tickLabelWidth > unit2Width) {
unit2 = (NumberTickUnit) tickUnitSource.getLargerTickUnit(unit2);
// due to limitations of double precision, when you zoom very far into
// a chart, eventually the visible axis range will get reported as
// having length 0, and then 'guess' above will be infinite ... in that
// case we'll just stick with the tick unit we have, it's better than
// throwing an exception
// https://github.com/jfree/jfreechart/issues/64
if (Double.isFinite(guess)) {
unit2 = (NumberTickUnit) tickUnitSource.getCeilingTickUnit(guess);
double unit2Width = lengthToJava2D(unit2.getSize(), dataArea, edge);
tickLabelWidth = estimateMaximumTickLabelWidth(g2, unit2);
if (tickLabelWidth > unit2Width) {
unit2 = (NumberTickUnit) tickUnitSource.getLargerTickUnit(unit2);
}
}
setTickUnit(unit2, false, false);
}
Expand Down Expand Up @@ -833,7 +842,6 @@ protected void selectVerticalAutoTickUnit(Graphics2D g2,
if (tickLabelHeight > unit2Height) {
unit2 = (NumberTickUnit) tickUnits.getLargerTickUnit(unit2);
}

setTickUnit(unit2, false, false);

}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jfree/chart/axis/NumberTickUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
* (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
Expand Down Expand Up @@ -147,7 +147,8 @@ public boolean equals(Object obj) {
*/
@Override
public String toString() {
return "[size=" + this.valueToString(this.getSize()) + "]";
return "[NumberTickUnit: size=" + this.valueToString(this.getSize())
+ ", formatter=" + this.formatter + "]";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
* (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
* (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
Expand All @@ -27,16 +27,11 @@
* ---------------------------
* StandardTickUnitSource.java
* ---------------------------
* (C) Copyright 2003-2008, by Object Refinery Limited.
* (C) Copyright 2003-2020, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 23-Sep-2003 : Version 1 (DG);
* 25-Oct-2007 : Implemented Serializable and equals() method (DG);
*
*/

package org.jfree.chart.axis;
Expand Down

0 comments on commit 6dc65fc

Please sign in to comment.