gpt4 book ai didi

java - 对 XYChart 系列数据进行排序

转载 作者:行者123 更新时间:2023-11-30 07:50:18 28 4
gpt4 key购买 nike

您好,我已将数据添加到 XYSeries 中,并尝试将数据从最短长度到最大长度排序。我不知道如何操作这些数据,任何人都可以帮助我。这是我的代码:

           xLabel = "Link Id";
yLabel = "Length (Km)";

bc = new BarChart<>(xAxis,yAxis);
bc.setTitle(gTitle);

//Set x/y Axis Label
//xAxis.setLabel(xLabel);
yAxis.setLabel(yLabel);

//double[] lengthArray = new double[linkIds.length];
XYChart.Series series1 = new XYChart.Series();
series1.setName(xLabel);

for (Integer m = 0; m < linkIdsOTS.length; m++) {
double length = netPlan.getLinkLengthInKm(otsLayerId, m);
//lengthArray[i] = length;
series1.getData().add(new XYChart.Data(m.toString(), length));
}

bc.getData().addAll(series1);

最佳答案

假设您的系列数据输入正确,您应该能够做到

series1.getData().sort(Comparator.comparingDouble(d -> d.getYValue().doubleValue()));

SSCCE:

import java.util.Comparator;
import java.util.Random;
import java.util.stream.Stream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.stage.Stage;

public class BarChartSortTest extends Application {

@Override
public void start(Stage primaryStage) {
Random rng = new Random();
BarChart<String, Number> chart = new BarChart<>(new CategoryAxis(), new NumberAxis());
String cats = "ABCDEFGH" ;
Series<String, Number> series = new Series<>();
series.setName("Random data");
chart.getData().add(series);
Stream.of(cats.split(""))
.map(cat -> new Data<String, Number>(cat, rng.nextDouble()))
.forEach(series.getData()::add);

series.getData().sort(Comparator.comparingDouble(d -> d.getYValue().doubleValue()));

primaryStage.setScene(new Scene(chart, 600, 600));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

关于java - 对 XYChart 系列数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33400146/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com