×

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 Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 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 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 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 Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 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 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 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 File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Gradient 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 gradient colors in these shapes using the...

7 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

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 Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Bloom

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

4 minutes read.