Updated June 12, 2023
Introduction to JavaFX Line Chart
Charts are known as a graphical method of representing data. There are different types of charts based on the difference in their visualization. The line chart is one such chart in which the series of data are linked with connection points. In this, variations of data can also be displayed. A line chart can be created by instantiating the class javafx.scene.chart.LineChart. Let us look into the JavaFX Line chart in detail.
Syntax of JavaFX Line Chart
A line chart can be created using the following syntax.
LineChart ll = new LineChart(x, y);
Make sure the class javafx.scene.chart.LineChart is imported into the program. Otherwise, errors can occur.
Constructors of JavaFX Line Chart
There are two constructors for the JavaFX Line chart. They are:
- LineChart(Axis X, Axis Y): A new instance of LineChart will be created with the specified axis.
- LineChart(Axis X, Axis Y, ObservableList<xychart.series> D): A new instance of LineChart will be created with the specified axis and data.
Methods of JavaFX Line Chart
Following are some of the methods given.
- axisSortingPolicyProperty(): specifies whether the data passed to the chart needs to be sorted based on the natural order of 1 of the axes.
- dataItemAdded( Series<X, Y> s,int index, XYChart.Data<X, Y>i ): When a data item i is added to the series s, this method is called.
- dataItemChanged( Data<X,Y> i ): When a data item i is changed, this method is called. Item can be x value, y value or extra value.
- dataItemRemoved( Data<X,Y> i, XYChart.Series<X,Y> s ): When a data item i is removed and still visible on the chart, this method is called.
- createSymbolsProperty(): When this method is true, data items that do not have any symbol node will be created with CSS styleable symbols.
- updateAxisRange(): When an invalidated range has to be updated, this method will be called.
- updateLegend(): This method will be called when a series is removed or added and the legend has to be updated.
- getAxisSortingPolicy(): axisSortingPolicy property’s value will be returned.
- layoutPlotChildren(): To update and layout the plot children, this method will be called.
- seriesAdded( Series<X,Y> s, int sIndex ): A series s will be added to the chart.
- seriesChanged( Change<? extends XYChart.Series> c ): When a change occurs in the series of the chart, this method will be called.
- seriesRemoved( Series<X,Y> s ): When a series s is removed and still visible on the chart, this method is called.
- setAxisSortingPolicy( SortingPolicyv ): axisSortingPolicy property’s value will be set as v.
- setCreateSymbols( boolean v ): createSymbols property’s value will be set as v.
- getCreateSymbols(): symbols for data points will be created or not will be indicated when this method is called.
How to Create JavaFX Line Chart?
To create JavaFX charts, the following steps can be performed.
1. Create a class
Create a class that extends from the application class.
public class JavaFXChartsExample extends Application {
}
2. Configure the Axes
Axes details have to be mentioned as follows.
//x axis representation
NumberAxis x = new NumberAxis();
x.setLabel("No of books owned");
//y axis representation
NumberAxis y = new NumberAxis();
y.setLabel("Rate per book");
3. Create the chart
Instantiate the class based on the chart needed. The following syntax is used for LineChart.
LineChart ll = new LineChart(x, y);
4. Add Data to the series
This is the most crucial step in this process where an instance is created for XYChart.Series. After that, the values that need to be displayed in the chart will be added using the below syntax.
XYChart.Series sr = new XYChart.Series();
sr.getData().add(new XYChart.Data( 1, 567));
5. Add data to the chart
Add data to the series created above using the below syntax.
ll.getData().add(sr);
6. Configure Group and Scene
Next, a group and scene will be created. The scene is created by instantiating the class javafx.scene once the group is created. Then only the group can be passed as one of the arguments in the scene.
VBox vbox = new VBox(ll);
Scene sc = new Scene(vbox, 400, 200);
s.setScene(sc);
s.setHeight(350);
s.setWidth(1250);
s.show();
Program to Implement JavaFX Line Chart
Now, let us see different JavaFX programs to implement line chart.
Program #1
Program to demonstrate the line chart
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaFXLineChartExample extends Application {
public void start(Stage s) {
s.setTitle("LineChart Sample");
//x and y axis representation
NumberAxis x = new NumberAxis();
x.setLabel("No of books owned");
NumberAxis y = new NumberAxis();
y.setLabel("Rate per book");
//line chart syntax
LineChart ll = new LineChart(x, y);
XYChart.Series sr = new XYChart.Series();
sr.setName("January");
sr.getData().add(new XYChart.Data( 1, 567));
sr.getData().add(new XYChart.Data( 4, 290));
sr.getData().add(new XYChart.Data(3, 200));
ll.getData().add(sr);
VBox vbox = new VBox(ll);
Scene sc = new Scene(vbox, 400, 200);
s.setScene(sc);
s.setHeight(350);
s.setWidth(1250);
s.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
A line chart with 1 line is displayed where Rate per book is in the y-axis and no. of books owned in the x-axis.
Program #2
Program to demonstrate the line chart
import javafx.application.Application;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class JavaFXLineChartExample extends Application {
@Override
public void start(Stage s) {
//create pane
Pane p = new Pane();
// Create series
ObservableList<XYChart.Series> sl = FXCollections.observableArrayList();
// Create dataset for males and add it to the series
ObservableList<XYChart.Data> l1 = FXCollections.observableArrayList(
new XYChart.Data(0, 0),
new XYChart.Data(2, 25),
new XYChart.Data(4, 37),
new XYChart.Data(6, 46),
new XYChart.Data(8, 115)
);
sl.add(new XYChart.Series("Male", l1));
// Create dataset for females and add it to the series
ObservableList<XYChart.Data> l2 = FXCollections.observableArrayList(
new XYChart.Data(0, 0),
new XYChart.Data(2, 43),
new XYChart.Data(4, 51),
new XYChart.Data(6, 64),
new XYChart.Data(8, 92)
);
sl.add(new XYChart.Series(" Female ", l2));
// Create axes
Axis x = new NumberAxis("Books read", 0, 8, 1);
Axis y = new NumberAxis("Time taken in hours", 0, 150, 10);
LineChart c = new LineChart(x, y, sl);
p.getChildren().add(c);
Scene sc = new Scene(p);
s.setScene(sc);
s.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sample Output
Here, a line chart with two separate lines for males and females is displayed.
Conclusion
As already discussed, Charts are well known for representing data. The line chart is one such chart in which the series of data are linked with connection points.
Recommended Articles
This is a guide to JavaFX Line Chart. Here we discuss how to create JavaFX Line Chart with the Methods and Constructors. You may look at the following articles to learn more –