Skip to content

Commit 9cfa75b

Browse files
Benjamin PeterBenjamin Peter
Benjamin Peter
authored and
Benjamin Peter
committed
Improve axis label in case of empty name or unit
- Remove leading white space when a unit was present but no axis name - Use an empty label if no axis name was specified and the unit was an empty string - Extracted axis label generator to own method for easier overwrite
1 parent f8a5094 commit 9cfa75b

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

chartfx-chart/src/main/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameter.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -1081,17 +1081,22 @@ protected void updateAxisLabel() {
10811081
if (state.isClean(ChartBits.AxisLabelText)) {
10821082
return;
10831083
}
1084+
getAxisLabel().setText(generateAxisLabelText());
1085+
state.clear(ChartBits.AxisLabelText);
1086+
}
1087+
1088+
/* visible for testing */ protected String generateAxisLabelText() {
10841089
String unit = getUnit();
1085-
String prefix = MetricPrefix.getShortPrefix(getUnitScaling());
1090+
String unitPrefix = MetricPrefix.getShortPrefix(getUnitScaling());
10861091

1087-
if (unit == null && PropUtil.isNullOrEmpty(prefix)) {
1088-
getAxisLabel().setText(getName());
1092+
if (PropUtil.isNullOrEmpty(unit) && PropUtil.isNullOrEmpty(unitPrefix)) {
1093+
return getName();
10891094
} else {
10901095
unit = (unit == null) ? "" : unit;
1091-
prefix = (prefix == null) ? "" : prefix;
1092-
getAxisLabel().setText(getName() + " [" + prefix + unit + "]");
1096+
unitPrefix = (unitPrefix == null) ? "" : unitPrefix;
1097+
String namePart = PropUtil.isNullOrEmpty(getName()) ? "" : getName() + " ";
1098+
return namePart + "[" + unitPrefix + unit + "]";
10931099
}
1094-
state.clear(ChartBits.AxisLabelText);
10951100
}
10961101

10971102
protected void updateScaleAndUnitPrefix() {

chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/AbstractAxisParameterTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void testBasicGetterSetters() {
136136
assertEquals("axis name2", axis.getAxisLabel().getText());
137137

138138
axis.set("axis name2", "", -3.0, +3.0);
139-
assertEquals("axis name2 []", axis.getAxisLabel().getText());
139+
assertEquals("axis name2", axis.getAxisLabel().getText());
140140

141141
axis.set("axis name2", "axis unit2");
142142
assertEquals("axis name2", axis.getName());

chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/AbstractAxisTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ void testHelper() {
168168

169169
axis.setUnit("");
170170
axis.setSide(Side.BOTTOM);
171-
assertEquals(+49, axis.computePrefHeight(100), 2);
171+
assertEquals(+32, axis.computePrefHeight(100), 2);
172172
assertEquals(+150.0, axis.computePrefWidth(-1));
173173
axis.setSide(Side.LEFT);
174174
assertEquals(+150, axis.computePrefHeight(-1));
175-
assertEquals(+48, axis.computePrefWidth(100), 2);
175+
assertEquals(+26, axis.computePrefWidth(100), 2);
176176

177177
assertDoesNotThrow(axis::clear);
178178
assertDoesNotThrow(axis::forceRedraw);

chartfx-chart/src/test/java/io/fair_acc/chartfx/axes/spi/DefaultNumericAxisTests.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.fair_acc.chartfx.axes.spi;
22

3-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertFalse;
6-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.stream.Stream;
76

87
import org.junit.jupiter.api.Test;
98
import org.junit.jupiter.api.function.ThrowingSupplier;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.Arguments;
11+
import org.junit.jupiter.params.provider.MethodSource;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214

@@ -93,4 +95,25 @@ public void parameterTests() {
9395
tickValues.clear();
9496
axis.calculateMinorTickValues(tickValues);
9597
}
98+
99+
@ParameterizedTest
100+
@MethodSource("testAxisLabelTextProvider")
101+
void testAxisLabelText(String expected, String name, String unit, double unitScaling) throws Exception {
102+
final DefaultNumericAxis axis = new DefaultNumericAxis(name, unit);
103+
axis.setUnitScaling(unitScaling);
104+
assertEquals(expected, axis.generateAxisLabelText());
105+
}
106+
107+
private static Stream<Arguments> testAxisLabelTextProvider() { // NOPMD -- is used in annotation /not detected by PMD
108+
return Stream.of(
109+
Arguments.arguments("axis name [axis unit]", "axis name", "axis unit", 1.),
110+
Arguments.arguments("DeviceA [V]", "DeviceA", "V", 1.),
111+
Arguments.arguments("DeviceA [mV]", "DeviceA", "V", 0.001),
112+
Arguments.arguments("axis name", "axis name", null, 1.),
113+
Arguments.arguments("[V]", "", "V", 1.),
114+
Arguments.arguments("[V]", null, "V", 1.),
115+
Arguments.arguments("", "", "", 1.),
116+
Arguments.arguments(null, null, null, 1.)
117+
);
118+
}
96119
}

0 commit comments

Comments
 (0)