Skip to content

Commit a811e50

Browse files
authored
Merge pull request #2719 from ControlSystemStudio/CSSTUDIO-1928-branch2
CSSTUDIO-1928 Fix autoscale of logarithmic axes
2 parents eefddfe + ef859b9 commit a811e50

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/AxisPart.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,7 @@ final synchronized public AxisRange<T> getValueRange()
263263
public boolean setValueRange(T low, T high)
264264
{
265265
synchronized (this)
266-
{ // Any change at all?
267-
if (low.equals(range.getLow()) && high.equals(range.getHigh()))
268-
return false;
266+
{
269267
logger.log(Level.FINE, "Axis {0}: Value range {1} ... {2}",
270268
new Object[] { getName(), low, high });
271269
// Adjust range if necessary
@@ -274,9 +272,14 @@ public boolean setValueRange(T low, T high)
274272
T newHigh = possiblyNewLowAndHigh.getValue();
275273
if (newLow != low || newHigh != high)
276274
{
277-
logger.log(Level.WARNING, "Axis {0}: Bad value range {1} ... {2}. Adjusting the range to {3} ... {4}.",
275+
logger.log(Level.WARNING, "Axis {0}: Invalid value range {1,number,#.###############E0} ... {2,number,#.###############E0}. Adjusting the range to {3,number,#.###############E0} ... {4,number,#.###############E0}.",
278276
new Object[] { getName(), low, high, newLow, newHigh });
279277
}
278+
279+
// Any change at all?
280+
if (newLow.equals(range.getLow()) && newHigh.equals(range.getHigh()))
281+
return false;
282+
280283
range = new AxisRange<>(newLow, newHigh);
281284
transform.config(newLow, newHigh, low_screen, high_screen);
282285
}
@@ -361,11 +364,11 @@ protected void computeTicks(final Graphics2D gc)
361364
{
362365
if (! dirty_ticks)
363366
return;
364-
final AxisRange<T> safe_range = range;
367+
setValueRange(range.getLow(), range.getHigh()); // Performs checks and possibly adjusts range.
365368
if (horizontal)
366-
ticks.compute(safe_range.getLow(), safe_range.getHigh(), gc, getBounds().width);
369+
ticks.compute(range.getLow(), range.getHigh(), gc, getBounds().width);
367370
else
368-
ticks.compute(safe_range.getLow(), safe_range.getHigh(), gc, getBounds().height);
371+
ticks.compute(range.getLow(), range.getHigh(), gc, getBounds().height);
369372
// If ticks changed, the layout of tick labels may change
370373
requestLayout();
371374
requestRefresh();

app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LinearTicks.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,24 @@ public Pair<Double, Double> adjustRange(Double low, Double high)
7878
if (Math.abs(high - low) < 3*Math.ulp(low)) {
7979
high = low + 3*Math.ulp(low);
8080
}
81-
82-
if (high < low) {
83-
return new Pair<>(high, low);
84-
}
85-
else {
86-
return new Pair<>(low, high);
87-
}
81+
return new Pair<>(low, high);
8882
}
8983

9084
/** {@inheritDoc} */
9185
@Override
9286
public void compute(Double low, Double high, final Graphics2D gc, final int screen_width)
9387
{
88+
Pair<Double, Double> adjustedRange = adjustRange(low, high);
89+
double newLow = adjustedRange.getKey();
90+
double newHigh = adjustedRange.getValue();
91+
92+
if (newLow != low || newHigh != high) {
93+
logger.log(Level.WARNING, "Invalid value range for a linear scale {0,number,#.###############E0} ... {1,number,#.###############E0}. Adjusting the range to {2,number,#.###############E0} ... {3,number,#.###############E0}.",
94+
new Object[] {low, high, newLow, newHigh });
95+
high = newHigh;
96+
low = newLow;
97+
}
98+
9499
logger.log(Level.FINE, "Compute linear ticks, width {0}, for {1} - {2}",
95100
new Object[] { screen_width, low, high });
96101

app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LogTicks.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,21 @@ public Pair<Double, Double> adjustRange(Double low, Double high) {
6060

6161
/** {@inheritDoc} */
6262
@Override
63-
public void compute(final Double low, final Double high, final Graphics2D gc, final int screen_width)
63+
public void compute(Double low, Double high, final Graphics2D gc, final int screen_width)
6464
{
65+
Pair<Double, Double> adjustedRange = adjustRange(low, high);
66+
double newLow = adjustedRange.getKey();
67+
double newHigh = adjustedRange.getValue();
68+
69+
if (newLow != low || newHigh != high) {
70+
logger.log(Level.WARNING, "Invalid value range for a logarithmic scale {0,number,#.###############E0} ... {1,number,#.###############E0}. Adjusting the range to {2,number,#.###############E0} ... {3,number,#.###############E0}.",
71+
new Object[] {low, high, newLow, newHigh });
72+
high = newHigh;
73+
low = newLow;
74+
}
75+
6576
logger.log(Level.FINE, "Compute log ticks, width {0}, for {1} - {2}",
66-
new Object[] { screen_width, low, high });
77+
new Object[] { screen_width, low, high });
6778

6879
double low_exp_exact = Log10.log10(low);
6980
double high_exp_exact = Log10.log10(high);

app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/PlotProcessor.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.concurrent.TimeoutException;
2020
import java.util.logging.Level;
2121

22+
import javafx.util.Pair;
2223
import org.csstudio.javafx.rtplot.Axis;
2324
import org.csstudio.javafx.rtplot.AxisRange;
2425
import org.csstudio.javafx.rtplot.Messages;
@@ -490,32 +491,26 @@ public void autoscale()
490491
}
491492
}
492493
if (axis.isLogarithmic())
493-
{ // Perform adjustment in log space.
494-
// But first, refuse to deal with <= 0
495-
if (low <= 0.0)
496-
low = 1;
497-
if (high <= low)
498-
high = low * 100.0;
499-
low = Log10.log10(low);
500-
high = Log10.log10(high);
501-
}
502-
final ValueRange rounded = roundValueRange(low, high);
503-
low = rounded.getLow();
504-
high = rounded.getHigh();
505-
if (axis.isLogarithmic())
506494
{
507-
low = Log10.pow10(low);
508-
high = Log10.pow10(high);
495+
low = Log10.pow10(Math.floor(Log10.log10(low))); // Note: may set "low" to 0.0.
496+
high = Log10.pow10(Math.ceil(Log10.log10(high)));
497+
Pair<Double, Double> adjustedRange = axis.ticks.adjustRange(low, high);
498+
low = adjustedRange.getKey();
499+
high = adjustedRange.getValue();
500+
509501
}
510-
else
511-
{ // Stretch range a little bit
502+
else {
503+
final ValueRange rounded = roundValueRange(low, high);
504+
low = rounded.getLow();
505+
high = rounded.getHigh();
506+
// Stretch range a little bit
512507
// (but not for log scale, where low just above 0
513508
// could be stretched to <= 0)
514509
final double headroom = (high - low) * 0.05;
515510
low -= headroom;
516511
high += headroom;
517-
518512
}
513+
519514
// Autoscale happens 'all the time'.
520515
// Do not use undo, but notify listeners.
521516
if (low != high)

0 commit comments

Comments
 (0)