Issue
NumberAxis and CategoryAxis are used for plotting my AreaChart. />
I would like to know how can I change display values for both axis since I need to:
- display humanReadableByteCount (KB, MB, GB) instead of bytes representation for the Y axis
- display the hour:minute:second only for thee X axis
I was unable to find any information on how to modify the display values in the doc: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/NumberAxis.html
Solution
I was able to solve this by implementing setTickLabelFormatter
which overrides toString:
ratesAxis.setTickLabelFormatter(new StringConverter<>() {
@Override
public String toString(Number t) {
return String.valueOf(ReadFile.humanReadableByteCountSI(t));
}
@Override
public Number fromString(String string) {
return 1;
}
});
From another class:
static String humanReadableByteCountSI(Number bytesString) {
int bytes = (int) bytesString.intValue();
if (-1000 < bytes && bytes < 1000) {
return bytes + " B";
}
CharacterIterator ci = new StringCharacterIterator("kMGTPE");
while (bytes <= -999_950 || bytes >= 999_950) {
bytes /= 1000;
ci.next();
}
return String.format("%.1f %cB", bytes / 1000.0, ci.current());
}
Answered By - feedthemachine
Answer Checked By - David Marino (JavaFixing Volunteer)