JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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