实时图 - ArrayList转换错误

时间:2017-03-07 12:27:12

标签: java arraylist javafx graph real-time

我目前正在使用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();

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

所以我想以某种方式使用ArrayList,如:

arraylist

并执行与之前相同的操作,

我尝试过:

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

但那不起作用

错误:

Executing /

1 个答案:

答案 0 :(得分:2)

错误

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

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

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

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