Reputation: 35
I have the following JavaFX BarChart (NumberAxis on the bottom, CategoryAxis on the left):
https://i.sstatic.net/jpiu1.png
How can I reverse (i.e. sort by value) the order of the categories, so that J shows at the top and A at the bottom?
Upvotes: 0
Views: 2360
Reputation: 349
The chart data could be sorted in the decreasing order of their sizes so that the category axis is always sorted .
bc.getData().sort((Series<String, Number> o1, Series<String, Number> o2) -> o1.getData().size() <= o2.getData().size()?1:0);
Upvotes: 0
Reputation: 31
You can do this:
XYChart.Series series1 = new XYChart.Series();
series1.getData().add(new XYChart.Data(1, "A"));
series1.getData().add(new XYChart.Data(1, "B"));
series1.getData().add(new XYChart.Data(1, "C"));
series1.getData().add(new XYChart.Data(1, "D"));
series1.getData().add(new XYChart.Data(1, "E"));
series1.getData().add(new XYChart.Data(2, "F"));
series1.getData().add(new XYChart.Data(2, "G"));
series1.getData().add(new XYChart.Data(4, "H"));
series1.getData().add(new XYChart.Data(4, "J"));
Collections.sort(series1.getData(), new Comparator<XYChart.Data>() {
@Override
public int compare(Data o1, Data o2) {
Number xValue1 = (Number) o1.getXValue();
Number xValue2 = (Number) o2.getXValue();
return new BigDecimal(xValue1.toString()).compareTo(new BigDecimal(xValue2.toString()));
}
});
Upvotes: 2