JavaFX - 如何在BarChart中对值进行排序

时间:2015-03-26 20:53:52

标签: java javafx javafx-2 bar-chart

我有以下JavaFX BarChart(底部是NumberAxis,左边是CategoryAxis):

http://i.imgur.com/3cHPuX1.png

如何反转(即按值排序)类别的顺序,以便J显示在顶部,A显示在底部?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

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()));
    }
});

答案 1 :(得分:0)

可以按图表大小的降序对图表数据进行排序,以便始终对类别轴进行排序。

bc.getData().sort((Series<String, Number> o1, Series<String, Number> o2) -> o1.getData().size() <= o2.getData().size()?1:0);