您好我已经将数据添加到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);
答案 0 :(得分:1)
假设您的Series
和Data
输入正确,您应该可以
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);
}
}