'Updating lineChart in JavaFX

I am new to Java and JavaFX and have a question. I have an application with a single stage/scene in JavaFX. It has multiple tabs. In one of those tabs, I want the user to be able to input some values in text fields, press a submit button, and have a lineChart, on that same scene, be then displayed. I'm not having luck getting this to work.
An empty line Chart shows up when I run the app, but I cannot figure out how to get the series of data to appear.

Here is simple test code in the Controller class for the method that the code executes when the submit button is pressed:

@FXML
private void getEngDataPlot(ActionEvent event)  {

    lineChart.setTitle("Test charting");
    //defining a series
    XYChart.Series series = new XYChart.Series();
    series.setName("My Test Data");
    //populating the series with data
    series.getData().add(new XYChart.Data(1, 23));
    series.getData().add(new XYChart.Data(2, 14));
    series.getData().add(new XYChart.Data(3, 15));
    series.getData().add(new XYChart.Data(4, 24));
    series.getData().add(new XYChart.Data(5, 34));
    series.getData().add(new XYChart.Data(6, 36));
    series.getData().add(new XYChart.Data(7, 22));
    series.getData().add(new XYChart.Data(8, 45));
    series.getData().add(new XYChart.Data(9, 43));
    series.getData().add(new XYChart.Data(10, 17));
    series.getData().add(new XYChart.Data(11, 29));
    series.getData().add(new XYChart.Data(12, 25));

    lineChart.getData().add(series);

}

must I 'do' something else in this method to get the chart on the scene to load? I see show.stage(); in examples, but at the point I execute this method, my app is running and I already have the 'stage' shown - don't I? I'm obviously missing something fundamental to how this is supposed to hang together, but I don't know what.

thank you, and pardon my ignorance.



Solution 1:[1]

Most probably your chart axis are not set coorrectly so you can't see new data. Try to add next lines:

lineChart.getXAxis().setAutoRanging(true);
lineChart.getYAxis().setAutoRanging(true);

Or take a look at next example:

public class ChartUpdate extends Application {
    @Override
    public void start(Stage primaryStage) {
        //random axis here
        final LineChart lineChart = new LineChart(new NumberAxis(1, 22, 0.5), new NumberAxis(1, 22, 0.5));
        lineChart.getXAxis().setAutoRanging(true);
        lineChart.getYAxis().setAutoRanging(true);
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                lineChart.setTitle("Test charting");
                //defining a series
                XYChart.Series series = new XYChart.Series();
                series.setName("My Test Data");
                //populating the series with data
                series.getData().add(new XYChart.Data(1, 23));
                series.getData().add(new XYChart.Data(2, 14));
                series.getData().add(new XYChart.Data(3, 15));
                series.getData().add(new XYChart.Data(4, 24));
                series.getData().add(new XYChart.Data(5, 34));
                series.getData().add(new XYChart.Data(6, 36));
                series.getData().add(new XYChart.Data(7, 22));
                series.getData().add(new XYChart.Data(8, 45));
                series.getData().add(new XYChart.Data(9, 43));
                series.getData().add(new XYChart.Data(10, 17));
                series.getData().add(new XYChart.Data(11, 29));
                series.getData().add(new XYChart.Data(12, 25));

                lineChart.getData().add(series);
            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(btn, lineChart);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) { launch(); }
}

Solution 2:[2]

Maybe you already found the answer for your problem, but I belive this might help others in the future.

When creating a LineChart in Scene Builder, notice that there are two types of LineCharts: LineChart and LineChart (N x N). The first one has a category x-axis (where you can only define categories to show like a string). The second one has a number x-axis.

So if you want to display data vs. data, like in the example you show, you should use LineChart (N x N).

Hope it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergey Grinev
Solution 2 user2703830