×

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes. All methods needed for this purpose are present in the javafx.scene.chart.LineChart class.

The constructor of Line Chart:

1. LineChart(Axis x, Axis y):

It is used to create a Line Chart with specified x and y-axis.

2. LineChart(Axis x, Axis y, ObseravableList newlist):

It is used to create a Line Chart with specified x and y-axis and specified new

data.

Charts in JavaFX applications- Line Chart showing yearly rainfall:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
        final NumberAxis newxaxis = new NumberAxis(2009,2015,1);  
        final NumberAxis newyaxis = new NumberAxis(100,800,100);  
          
        newxaxis.setLabel("Year");  
        newyaxis.setLabel("Rainfall");  
          
        LineChart chart = new LineChart(newxaxis,newyaxis);  
          
        XYChart.Series series = new XYChart.Series();  
          
        series.setName("Rainfall Analysis");  
        series.getData().add(new XYChart.Data(2009,300));  
        series.getData().add(new XYChart.Data(2010,450));  
        series.getData().add(new XYChart.Data(2011,680));  
        series.getData().add(new XYChart.Data(2012,600));  
        series.getData().add(new XYChart.Data(2013,350));  
        series.getData().add(new XYChart.Data(2014,550));  
        series.getData().add(new XYChart.Data(2015,450));  
          
       chart.getData().add(series);  
          
        Group root = new Group(chart);  
        Scene scene = new Scene(root,600,400);     primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX Chart - Line Chart Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the LineChart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.chart.LineChart, javafx.scene.chart.NumberAxis, javafx.scene.chart.XYChart.

Then we have created one class named ChartUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold LineChart with  x, y-axis, a LineChart object is created which is then passed to the Scene class object.

LineChart is created with the help of its constructor. And x and y-axis are passed to it and the data is added to LineChart using series.getData() method.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Chart – Line Chart Example”. Also, it displays Line Chart with the x and y-axis and displays the data for rainfall in years from 2009 to 2015.

Charts in JavaFX applications- Line Chart

Charts in JavaFX applications- Line Chart showing employees revenue:

Example:

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 ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    NumberAxis xAxis = new NumberAxis();
        xAxis.setLabel("Employees");


        NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel(" Revenue ");


        LineChart lineChart = new LineChart(xAxis, yAxis);


        XYChart.Series dataSeries1 = new XYChart.Series();
        dataSeries1.setName("Revenue for the year 2020");


        dataSeries1.getData().add(new XYChart.Data( 10, 567));
        dataSeries1.getData().add(new XYChart.Data( 50, 612));
        dataSeries1.getData().add(new XYChart.Data(100, 800));
        dataSeries1.getData().add(new XYChart.Data(20, 780));
        dataSeries1.getData().add(new XYChart.Data(40, 810));
        dataSeries1.getData().add(new XYChart.Data(80, 850));


        lineChart.getData().add(dataSeries1);


        VBox vbox = new VBox(lineChart);


        Scene scene = new Scene(vbox, 600, 400);
          primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX Chart - Line Chart Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the employee revenue LineChart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.chart.LineChart, javafx.scene.chart.NumberAxis, javafx.scene.chart.XYChart, javafx.scene.layout.VBox.

Then we have created one class named ChartUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold LineChart with x, y-axis, and employee revenue data, a LineChart object is created which is then passed to the Scene class object.

LineChart is created with the help of its constructor. And x and y-axis are passed to it and the data is added to LineChart using dataseries1.getData() method.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Chart – Line Chart Example". Also, it displays Line Chart with the x and y-axis and displays the revenue of a specified number of employees mentioned on the x-axis.

Charts in JavaFX applications- Line Chart

Related Topics

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

3 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Fill Transition

In the JavaFX application, in order to generate the fill transition, we have to apply fill transition on any particular shape. It has the ability to fill the given shape...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.