Search code examples
javajavafx-8bar-chartlinechartaxis-labels

Removing negative values from axis


I have a BarChart and a LineChart.

The possible values go from negative(-) to positive(+). How would I only display the positive axis-labels? As in only remove the numbers.

Should I make a custom Chart or is there an easier way?


Solution

  • After looking through the NumberAxis source, I found out that the DefaultFormatter class inside the NumberAxis class is accountable for this. So overriding the toString method of it was enough.

    myAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(neerslagAxis) {
                @Override
                public String toString(Number object) {
                    if ((double) object < 0) {
                        return "";
                    } else {
                        return ("" + object).replace(".0", "");
                    }
                }
            });