-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNiceAxisScale.java
More file actions
118 lines (94 loc) · 3.23 KB
/
Copy pathNiceAxisScale.java
File metadata and controls
118 lines (94 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package wf.util;
public class NiceAxisScale {
private final static NiceAxisScale INSTANCE = new NiceAxisScale();
private double minPoint;
private double maxPoint;
private double maxTicks = 10;
private double tickSpacing;
private double range;
private double niceMin;
private double niceMax;
/// **
// * Instantiates a new instance of the NiceScale class.
// *
// * @param min the minimum data point on the axis
// * @param max the maximum data point on the axis
// */
public NiceAxisScale() {
// this.minPoint = min;
// this.maxPoint = max;
// calculate();
}
public static NiceAxisScale getInstance() {
return INSTANCE;
}
/**
* Calculate and update values for tick spacing and nice
* minimum and maximum data points on the axis.
*/
private void calculate() {
range = niceNum(maxPoint - minPoint, false);
tickSpacing = niceNum(range / (maxTicks - 1), true);
niceMin = Math.floor(minPoint / tickSpacing) * tickSpacing;
niceMax = Math.ceil(maxPoint / tickSpacing) * tickSpacing;
}
/**
* Returns a "nice" number approximately equal to range Rounds
* the number if round = true Takes the ceiling if round = false.
*
* @param range the data range
* @param round whether to round the result
* @return a "nice" number to be used for the data range
*/
private static double niceNum(final double range, final boolean round) {
double exponent; /** exponent of range */
double fraction; /** fractional part of range */
double niceFraction; /** nice, rounded fraction */
exponent = Math.floor(Math.log10(range));
fraction = range / Math.pow(10, exponent);
if (round) {
if (fraction < 1.5) {
niceFraction = 1;
} else if (fraction < 3) {
niceFraction = 2;
} else if (fraction < 7) {
niceFraction = 5;
} else {
niceFraction = 10;
}
} else {
if (fraction <= 1) {
niceFraction = 1;
} else if (fraction <= 2) {
niceFraction = 2;
} else if (fraction <= 5) {
niceFraction = 5;
} else {
niceFraction = 10;
}
}
return niceFraction * Math.pow(10, exponent);
}
/**
* Sets the minimum and maximum data points for the axis.
*
* @param minPoint the minimum data point on the axis
* @param maxPoint the maximum data point on the axis
* @param maxTicks the maximum number of tick marks for the axis
*/
public void setAxisValues(final double minPoint, final double maxPoint, final double maxTicks) {
this.minPoint = minPoint;
this.maxPoint = maxPoint;
this.maxTicks = maxTicks;
calculate();
}
public double getTickSpacing() {
return tickSpacing;
}
public double getNiceMin() {
return niceMin;
}
public double getNiceMax() {
return niceMax;
}
}