Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/axis name generation handle missing info #681

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -168,11 +168,11 @@ void testHelper() {

axis.setUnit("");
axis.setSide(Side.BOTTOM);
assertEquals(+49, axis.computePrefHeight(100), 2);
assertEquals(+32, axis.computePrefHeight(100), 2);
assertEquals(+150.0, axis.computePrefWidth(-1));
axis.setSide(Side.LEFT);
assertEquals(+150, axis.computePrefHeight(-1));
assertEquals(+48, axis.computePrefWidth(100), 2);
assertEquals(+26, axis.computePrefWidth(100), 2);

assertDoesNotThrow(axis::clear);
assertDoesNotThrow(axis::forceRedraw);
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) {
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.)
);
}
}
Loading