diff --git a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LinearTicks.java b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LinearTicks.java index 6703336ff7..ec28af7e08 100644 --- a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LinearTicks.java +++ b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LinearTicks.java @@ -53,9 +53,9 @@ public class LinearTicks extends Ticks /** Threshold for order-of-magnitude to use exponential notation */ private long exponential_threshold = 4; - /** User-specified override format, or {@code null} for automatic selection. - * When set, it is applied to all non-empty major tick labels after - * {@code compute()} finishes its internal layout. + /** User-specified format for the tick labels, or {@code null} for the + * automatic format. Applied by {@link #format(Double)}, so it covers + * every label that {@code compute()} creates. */ private volatile NumberFormat label_fmt_override = null; @@ -93,21 +93,6 @@ public boolean isPerpendicularTickLabels() return perpendicular_tick_labels; } - /** Re-apply {@code fmt} to every non-empty major tick label in {@code ticks}. - * @param ticks List to mutate in-place - * @param fmt Format to use - */ - protected static void relabelTicks(final List> ticks, - final NumberFormat fmt) - { - for (int i = 0; i < ticks.size(); i++) - { - final MajorTick t = ticks.get(i); - if (!t.getLabel().isEmpty()) - ticks.set(i, new MajorTick<>(t.getValue(), fmt.format(t.getValue()))); - } - } - /** @param order_of_magnitude determines when to use exponential notation */ public void setExponentialThreshold(long order_of_magnitude) { @@ -194,7 +179,7 @@ public void compute(Double low, Double high, final Graphics2D gc, final int scre double distance = selectNiceStep(min_distance); if (distance == 0.0) throw new Error("Broken tickmark computation"); - + // Update num_fmt based on distance between major tick labels. // For example, an axis with range 0 .. 10 would ordinarily use precision 0 // and axis markers like 0, 2, 4, 6, 8, 10. @@ -385,9 +370,9 @@ public String format(final Double num) return "Inf"; // Patch numbers that are "very close to zero" // to avoid "-0.00" or "0.0e-22" - if (Math.abs(num) < zero_threshold) - return num_fmt.format(0.0); - return num_fmt.format(num); + final double val = Math.abs(num) < zero_threshold ? 0.0 : num; + final NumberFormat override = getLabelFormatOverride(); + return (override != null) ? override.format(val) : num_fmt.format(val); } /** {@inheritDoc} */ diff --git a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LogTicks.java b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LogTicks.java index 3a76def321..36fae4ff4d 100644 --- a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LogTicks.java +++ b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/LogTicks.java @@ -136,11 +136,6 @@ else if (decadeExps.size() >= 2) major_ticks.add( new MajorTick<>(high, format(high))); } - // Apply user-specified label format override if set. - final NumberFormat override = getLabelFormatOverride(); - if (override != null) - relabelTicks(major_ticks, override); - this.major_ticks = major_ticks; this.minor_ticks = minor_ticks; } diff --git a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/YAxisImpl.java b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/YAxisImpl.java index 109ad205c5..11b9d67510 100644 --- a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/YAxisImpl.java +++ b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/internal/YAxisImpl.java @@ -312,6 +312,7 @@ public void setLabelFormat(final java.text.NumberFormat fmt) { if (ticks instanceof LinearTicks) ((LinearTicks) ticks).setLabelFormat(fmt); + dirty_ticks = true; requestLayout(); } diff --git a/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/internal/LinearTicksTest.java b/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/internal/LinearTicksTest.java index 074cb4f8e5..83cb415188 100644 --- a/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/internal/LinearTicksTest.java +++ b/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/internal/LinearTicksTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; /** JUnit test @@ -47,6 +48,25 @@ public void testNiceDistance() } } + /** A label format set by the user applies to every label, and a value + * that is numerically almost zero still reads as zero, not "-0.00" */ + @Test + public void testLabelFormat() + { + final LinearTicks ticks = new LinearTicks(); + ticks.setLabelFormat(LinearTicks.createDecimalFormat(2)); + ticks.compute(-0.7, 0.7, gc, buf.getWidth()); + for (MajorTick tick : ticks.getMajorTicks()) + { + final String label = tick.getLabel(); + if (label.isEmpty()) + continue; + assertThat(label, not(equalTo("-0.00"))); + assertThat(label + " has two decimals", label.matches("-?\\d+\\.\\d\\d"), equalTo(true)); + } + assertThat(ticks.format(-1e-17), equalTo("0.00")); + } + @Test public void testNormalTicks() { diff --git a/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/internal/YAxisLabelFormatTest.java b/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/internal/YAxisLabelFormatTest.java new file mode 100644 index 0000000000..b34bb2850b --- /dev/null +++ b/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/internal/YAxisLabelFormatTest.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2026 Oak Ridge National Laboratory. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + ******************************************************************************/ +package org.csstudio.javafx.rtplot.internal; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.text.DecimalFormat; + +import org.junit.jupiter.api.Test; + +/** JUnit test for the label format of a {@link YAxisImpl} */ +@SuppressWarnings("nls") +public class YAxisLabelFormatTest +{ + /** Setting a label format must relabel ticks that were already computed */ + @Test + public void testLabelFormatRelabelsTicks() + { + final PlotPartListener listener = new PlotPartListener() + { + @Override + public void layoutPlotPart(final PlotPart plotPart) + { + // The test computes the ticks itself, no plot to lay out + } + + @Override + public void refreshPlotPart(final PlotPart plotPart) + { + // Nothing is drawn in this test + } + }; + final YAxisImpl axis = new YAxisImpl<>("", listener); + axis.setValueRange(0.0, 1.0); + axis.setBounds(0, 0, 60, 300); + final Graphics2D gc = new BufferedImage(60, 300, BufferedImage.TYPE_INT_ARGB).createGraphics(); + axis.computeTicks(gc); + assertThat(axis.getTicks().getMajorTicks().get(0).getLabel(), equalTo("0.0")); + + axis.setLabelFormat(new DecimalFormat("0.000")); + axis.computeTicks(gc); + assertThat(axis.getTicks().getMajorTicks().get(0).getLabel(), equalTo("0.000")); + gc.dispose(); + } +}