【问题标题】:Ordering the xAxis Category in JavaFX 8 BarChart在 JavaFX 8 BarChart 中订购 xAxis 类别
【发布时间】:2015-05-08 16:50:50
【问题描述】:

如何在 JavaFX 8 中对条形图的 xAxis 进行排序或排序,虽然 xAxis 虽然定义为字符串,但表示数字。例如:

@Override public void start(Stage stage) {
    stage.setTitle("Bar Chart Sample");
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    final BarChart<String,Number> bc = 
        new BarChart<>(xAxis,yAxis);
    bc.setTitle("Country Summary");
    xAxis.setLabel("Number");       
    yAxis.setLabel("Value");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("Series-1");
    series1.getData().add(new XYChart.Data("2",200));
    series1.getData().add(new XYChart.Data("3",500));
    series1.getData().add(new XYChart.Data("4",100));

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("Series-2");
    series2.getData().add(new XYChart.Data("1",900));
    series2.getData().add(new XYChart.Data("2",150));
    series2.getData().add(new XYChart.Data("3",50));
    series2.getData().add(new XYChart.Data("4",700));

    Scene scene  = new Scene(bc,800,600);
    bc.getData().addAll(series1, series2);

    stage.setScene(scene);
    stage.show();
}

虽然这是显示问题的一种简单方式,但在我的代码中,我从一个巨大的 excel 文件生成 BarChart。正在使用的代码的 sn-p 如下:

final ObservableList<XYChart.Series<String,Number>> barChartData =      FXCollections.observableArrayList();

            Iterator<Map.Entry<String, ArrayList<XYBean>>> it = xyBean.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, ArrayList<XYBean>> entry = it.next();
                XYChart.Series series = new XYChart.Series<>();
                series.setName(entry.getKey());
                for (XYBean xybean : entry.getValue()) {
                    series.getData().add(new XYChart.Data(xybean.getxValue(), xybean.getyValue()));

                }

                barChartData.add(series);

            }
barChart.setData(barChartData);

请帮忙,提前致谢!

【问题讨论】:

    标签: javafx


    【解决方案1】:

    您需要对类别进行排序。

    Collections.sort(xAxis.getCategories());
    

    不起作用,但是。

    一种可行的方法如下所示:

    // fill this whenever you add new data to the chart
    private final Set<String> categories = new LinkedHashSet<>();
    
    // after all data is added to the chart, sort the categories, set them manually and enable auto-ranging again (if needed)
    final ObservableList<String> c = FXCollections.observableArrayList(categories);
    Collections.sort(c);
    xAxis.setCategories(c);
    xAxis.setAutoRanging(true);
    

    【讨论】:

    • 需要注意两点: (a) getCategories 在 CategoryAxis 中可用 - 如果您从图表中获取它,则必须先转换它。 (b) setCategories 然后 setAutoranging 很重要,否则标签没有排序,只有条形!
    【解决方案2】:

    一种方法是从 xAxis 获取类别,并使用比较器对它们进行排序。虽然我的印象是类别轴无论如何都是自己做的。

    ObservableList<String>list = xAxis.getCategories();
    Comparator<String> byValue = (e1, e2) -> Integer.compare(Integer.parseInt(e1), Integer.parseInt(e2));
    xAxis.setCategories(list.sort(byValue));
    

    【讨论】:

    • 感谢您的回答,但我尝试使用比较器,但可能我做错了,请分享您如何实现这一目标的代码,考虑到我代码中的 sn-p?谢谢。
    • ObservableListlist = xAxis.getCategories();这条线似乎没有得到任何东西,当我检查列表大小时,它为零,我错过了什么吗?谢谢。
    • 确保在将数据加载到图表后调用它。
    • 首先,非常感谢您在这方面帮助我,但仍然无法正常工作。我已将您建议的代码放在最后,见下文: Scene scene = new Scene(barChart, 400, 200); fxPanel.setScene(场景); ObservableList 列表 = xAxis.getCategories();比较器 byValue = (e1, e2) -> Double.compare(Double.parseDouble(e1), Double.parseDouble(e2)); list.sort(byValue);列表已排序,但 barChart 未排序。
    • 列表的输出是:排序前的列表--- [1.0, 4.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 29.0, 30.0, 31.0, 0.0, 2.0, 3.0, 5.0, 6.0] 排序后的列表 ----- [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 , 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 29.0, 30.0, 31.0]>s
    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多