我有一个LineChart实例用于在折线图中显示信息,但是无法正确排序x轴。
public class Main extends Application {
@Override public void start(Stage stage) {
stage.setTitle("Line Chart Sample");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Month");
final LineChart<String,Number> lineChart =
new LineChart<String,Number>(xAxis,yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
XYChart.Series series1 = new XYChart.Series();
series1.setName("Portfolio 1");
series1.getData().add(new XYChart.Data("4", 12.15));
series1.getData().add(new XYChart.Data("7", 11.25));
series1.getData().add(new XYChart.Data("10", 3.00));
series1.getData().add(new XYChart.Data("14", 1.60));
series1.getData().add(new XYChart.Data("19", 1.05));
series1.getData().add(new XYChart.Data("25", 26.30));
series1.getData().add(new XYChart.Data("26", 7.50));
series1.getData().add(new XYChart.Data("27", 4.05));
series1.getData().add(new XYChart.Data("28", 4.00));
XYChart.Series series2 = new XYChart.Series();
series2.setName("Portfolio 2");
series2.getData().add(new XYChart.Data("7", 21.75));
series2.getData().add(new XYChart.Data("28", 13.20));
XYChart.Series series3 = new XYChart.Series();
series3.setName("Portfolio 3");
series3.getData().add(new XYChart.Data("2", 5.60));
series3.getData().add(new XYChart.Data("3", 7.70));
series3.getData().add(new XYChart.Data("10", 25.85));
series3.getData().add(new XYChart.Data("19", 6.00));
series3.getData().add(new XYChart.Data("28", 4.85));
series3.getData().add(new XYChart.Data("29", 0.80));
XYChart.Series series4 = new XYChart.Series();
series4.setName("Portfolio 4");
series4.getData().add(new XYChart.Data("9", 6.35));
series4.getData().add(new XYChart.Data("11", 3.20));
series4.getData().add(new XYChart.Data("19", 6.45));
series4.getData().add(new XYChart.Data("28", 6.00));
series4.getData().add(new XYChart.Data("29", 4.00));
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().addAll(series1, series2, series3, series4);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
基本上,如果y轴中只有一个值对应x值,它将显示在错误的位置。调试器向我显示数据是按预期顺序添加的。
顺便说一句,如果删除了所有其他图表,则可以正确显示每个图表。
答案 0 :(得分:0)
我相信它是因为您正在为xAxis使用CategoryAxis。这意味着您要使用字符串值标签添加类别(它们恰好是数字)。所以它并不知道订单很重要。
您可以使用NumberAxis为xAxis修复它。然后,如果xAxis标签的十进制值(它将自动插入刻度标记)不是您想要的,您可以使用this question.
中概述的自定义TickLabelFormatter。