diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameter.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameter.java index 2b3902cc1..a10ff9e91 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameter.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameter.java @@ -1081,17 +1081,22 @@ protected void updateAxisLabel() { if (state.isClean(ChartBits.AxisLabelText)) { return; } + getAxisLabel().setText(generateAxisLabelText()); + state.clear(ChartBits.AxisLabelText); + } + + /* visible for testing */ protected String generateAxisLabelText() { String unit = getUnit(); - String prefix = MetricPrefix.getShortPrefix(getUnitScaling()); + String unitPrefix = MetricPrefix.getShortPrefix(getUnitScaling()); - if (unit == null && PropUtil.isNullOrEmpty(prefix)) { - getAxisLabel().setText(getName()); + if (PropUtil.isNullOrEmpty(unit) && PropUtil.isNullOrEmpty(unitPrefix)) { + return getName(); } else { unit = (unit == null) ? "" : unit; - prefix = (prefix == null) ? "" : prefix; - getAxisLabel().setText(getName() + " [" + prefix + unit + "]"); + unitPrefix = (unitPrefix == null) ? "" : unitPrefix; + String namePart = PropUtil.isNullOrEmpty(getName()) ? "" : getName() + " "; + return namePart + "[" + unitPrefix + unit + "]"; } - state.clear(ChartBits.AxisLabelText); } protected void updateScaleAndUnitPrefix() { diff --git a/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameterTests.java b/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameterTests.java index 1cbb1ec8a..170870229 100644 --- a/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameterTests.java +++ b/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameterTests.java @@ -136,7 +136,7 @@ void testBasicGetterSetters() { assertEquals("axis name2", axis.getAxisLabel().getText()); axis.set("axis name2", "", -3.0, +3.0); - assertEquals("axis name2 []", axis.getAxisLabel().getText()); + assertEquals("axis name2", axis.getAxisLabel().getText()); axis.set("axis name2", "axis unit2"); assertEquals("axis name2", axis.getName()); diff --git a/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/DefaultNumericAxisTests.java b/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/DefaultNumericAxisTests.java index dca8770d8..63df949b3 100644 --- a/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/DefaultNumericAxisTests.java +++ b/chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/DefaultNumericAxisTests.java @@ -1,12 +1,14 @@ package io.fair_acc.chartfx.axes.spi; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.ThrowingSupplier; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,4 +95,25 @@ public void parameterTests() { tickValues.clear(); axis.calculateMinorTickValues(tickValues); } + + @ParameterizedTest + @MethodSource("testAxisLabelTextProvider") + void testAxisLabelText(String expected, String name, String unit, double unitScaling) throws Exception { + final DefaultNumericAxis axis = new DefaultNumericAxis(name, unit); + axis.setUnitScaling(unitScaling); + assertEquals(expected, axis.generateAxisLabelText()); + } + + private static Stream testAxisLabelTextProvider() { // NOPMD -- is used in annotation /not detected by PMD + return Stream.of( + Arguments.arguments("axis name [axis unit]", "axis name", "axis unit", 1.), + Arguments.arguments("DeviceA [V]", "DeviceA", "V", 1.), + Arguments.arguments("DeviceA [mV]", "DeviceA", "V", 0.001), + Arguments.arguments("axis name", "axis name", null, 1.), + Arguments.arguments("[V]", "", "V", 1.), + Arguments.arguments("[V]", null, "V", 1.), + Arguments.arguments("", "", "", 1.), + Arguments.arguments(null, null, null, 1.) + ); + } }