Java 排序图表系列数据
您好,我已将数据添加到XYSeries中,并尝试将数据从最短长度排序到最大长度。我不知道如何处理这些数据,有人能帮我吗。这是我的代码:Java 排序图表系列数据,java,sorting,graph,javafx,Java,Sorting,Graph,Javafx,您好,我已将数据添加到XYSeries中,并尝试将数据从最短长度排序到最大长度。我不知道如何处理这些数据,有人能帮我吗。这是我的代码: xLabel = "Link Id"; yLabel = "Length (Km)"; bc = new BarChart<>(xAxis,yAxis); bc.setTitle(gTitle); //Set x/y Axis Label
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);
xLabel=“链接Id”;
yLabel=“长度(Km)”;
bc=新条形图(xAxis,yAxis);
bc.setTitle(gTitle);
//设置x/y轴标签
//xAxis.setLabel(xLabel);
yAxis.setLabel(yLabel);
//double[]lengthArray=新的双精度[LinkId.length];
XYChart.Series系列1=新的XYChart.Series();
series1.setName(xLabel);
对于(整数m=0;m
假设您的系列
和数据
输入正确,您应该能够
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);
}
}
import java.util.Comparator;
导入java.util.Random;
导入java.util.stream.stream;
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.chart.BarChart;
导入javafx.scene.chart.CategoryAxis;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.chart.XYChart.Data;
导入javafx.scene.chart.XYChart.Series;
导入javafx.stage.stage;
公共类BarChartSortTest扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
随机rng=新随机();
条形图=新条形图(新类别轴(),新编号轴());
字符串cats=“ABCDEFGH”;
系列=新系列();
序列名称(“随机数据”);
chart.getData().add(系列);
(猫的)分流(“”)
.map(cat->新数据(cat,rng.nextDouble())
.forEach(series.getData()::添加);
series.getData().sort(Comparator.comparingDouble(d->d.getYValue().doubleValue());
初始阶段。设置场景(新场景(图600600));
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}