实时图表 - ArrayList 转换错误

Real time graph - error with ArrayList conversion

我目前正在使用 XYChart 在 java 中创建图表,我这样做是使用以下代码:

    stage.setTitle("Emotion Analyser");

    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();

    xAxis.setLabel("Utterances");

    final LineChart<String,Number> lineChart = 
            new LineChart<String,Number>(xAxis,yAxis);

    lineChart.setTitle("Emotion Analysis");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("Happy");

    series1.getData().add(new XYChart.Data("1st", 23));
    series1.getData().add(new XYChart.Data("2nd", 14));
    series1.getData().add(new XYChart.Data("3rd", 15));
    series1.getData().add(new XYChart.Data("4th", 24));
    series1.getData().add(new XYChart.Data("5th", 34));
    series1.getData().add(new XYChart.Data("6th", 36));
    series1.getData().add(new XYChart.Data("7th", 22));
    series1.getData().add(new XYChart.Data("8th", 45));
    series1.getData().add(new XYChart.Data("9th", 43));
    series1.getData().add(new XYChart.Data("10th", 17)); 

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("sad");
    series2.getData().add(new XYChart.Data("1st", 33));
    series2.getData().add(new XYChart.Data("2nd", 34));
    series2.getData().add(new XYChart.Data("3rd", 25));
    series2.getData().add(new XYChart.Data("4th", 44));
    series2.getData().add(new XYChart.Data("5th", 39));
    series2.getData().add(new XYChart.Data("6th", 16));
    series2.getData().add(new XYChart.Data("7th", 55));
    series2.getData().add(new XYChart.Data("8th", 54));
    series2.getData().add(new XYChart.Data("9th", 48));
    series2.getData().add(new XYChart.Data("10th", 27)); 

    XYChart.Series series3 = new XYChart.Series();
    series3.setName("Shocked");

    series3.getData().add(new XYChart.Data("1st", 44));
    series3.getData().add(new XYChart.Data("2nd", 35));
    series3.getData().add(new XYChart.Data("3rd", 36));
    series3.getData().add(new XYChart.Data("4th", 33));
    series3.getData().add(new XYChart.Data("5th", 31));
    series3.getData().add(new XYChart.Data("6th", 26));
    series3.getData().add(new XYChart.Data("7th", 22));
    series3.getData().add(new XYChart.Data("8th", 25));
    series3.getData().add(new XYChart.Data("9th", 43));
    series3.getData().add(new XYChart.Data("10th", 44)); 

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

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

图片: 这是当您没有所有数据时想要手动添加它们的时候......但是我正在尝试创建一个实时更新图,它不知道每次它会有多少行运行,所以一次 运行 它可能在图表上有 2 条线,第二次 运行 它可能有 6.

所以我想以某种方式使用像这样的 ArrayList:

和之前一样,

我试过:

list.get(0).setName("Happyq");

但这没有用

错误:

Executing /

错误

Caused by java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

表示当列表为空时 (Size: 0),您正在尝试访问列表的第一个元素(例如,通过调用 list.get(0))。

堆栈跟踪告诉您这发生在 GraphTry1.java 的第 42 行。

有关读取和解释堆栈跟踪的详细信息,请参阅 this question