Creating LineChart in JavaFX with FXML | JavaFX Tutorial

In this tutorial we will learn to create a LineChart in JavaFX application. Its very easy to create a line chart, following are the steps for Creating LineChart in JavaFX with FXML:


Step 1: Create an FXML based project in NetBeans/Eclipse and follow the steps described in the video tutorial. 
Coding steps to create LineChart:

Step 2: Now we need to create Series for the chart and set the name. (creating only one series)

We have two types of axis –
CategoryAxis – string values like (to represent like months – Jan, Feb, March, April)
NumberAxis – for data points (Numbers only)


In this demo, we will use CategoryAxis and NumberAxis.

XYChart.Series series = new XYChart.Series();
series.setName(“Sales”); //setting series name (appear as legends)

Step 3: Adding data in this series. (series is a list of XYChart.Data objects)
series.getData().add(new XYChart.Data(“Jan”, 23));
series.getData().add(new XYChart.Data(“Feb”, 14));
series.getData().add(new XYChart.Data(“March”, 15));
series.getData().add(new XYChart.Data(“April”, 24));
series.getData().add(new XYChart.Data(“May”, 34));
series.getData().add(new XYChart.Data(“June”, 36));
series.getData().add(new XYChart.Data(“July”, 22));
series.getData().add(new XYChart.Data(“Aug”, 45));
series.getData().add(new XYChart.Data(“Sep”, 43));
series.getData().add(new XYChart.Data(“Oct”, 17));
series.getData().add(new XYChart.Data(“Nov”, 29));
series.getData().add(new XYChart.Data(“Dec”, 25));

Step 4: Adding series in LineChart.
lineChart.getData().add(series);


Step 5: Build and Run.
 
For detail understanding please watch this video tutorial.

If you found this tutorial relevant and useful, so please hit like button to appreciate us. Your valuable feedback is very important to us, also you can subscribe to learn more.

Leave a Comment