Skip to content

Commit

Permalink
Improve axis label in case of empty name or unit
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Benjamin Peter authored and Benjamin Peter committed Dec 6, 2024
1 parent f8a5094 commit 667f345
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<Arguments> 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.)
);
}
}

0 comments on commit 667f345

Please sign in to comment.