5
votes

I'm trying to create a chart who's yAxis is designed to show number of employee number, so it must only show whole numbers.
But I found it's not that easy as I already tried to yAxis.setTickUnit(1) but it won't work when the values are small(etc. the max value is 3, it'll still show 0.5,1.5..., I only want tick value like 1,2,3,4..)
How Could I to achieve this?

According to @jewelsea 's answer, I tried this(In javafx 2.2 jdk7)

class IntegerStringConverter extends StringConverter<Number>{

    public IntegerStringConverter() {
    }

    @Override
    public String toString(Number object) {
        if(object.intValue()!=object.doubleValue())
            return "";
        return ""+(object.intValue());
    }

    @Override
    public Number fromString(String string) {
        Number val = Double.parseDouble(string);
        return val.intValue();
    }
}  

It's result is kind of acceptable. Double value's are gone, but there ticks are still there.
double values gone but ticks are still there

2
I am facing the same issue,So I used your code and it works, Thanks. But when Number axis has only 0 and 1, the 1 disappears, are u facing the same issue?Vijay Vennapusa
To get rid of the unlabeled ticks try yAxis.setMinorTickVisible(false);. Worked for me.Brad Turek

2 Answers

2
votes
0
votes

You can set the NumberAxis(double lowerBound, double upperBound, double tickUnit) values by using the constructor. This works fine for JaxaFX 8, in case people like me are still looking for this.

Source: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/NumberAxis.html