Creating index charts

An index chart is a line chart that shows the percentage change of something over time. Frequently, such a chart is based on a single data attribute. In the following example, we will be using the Belgian population for six decades. The data is a subset of population data found at https://ourworldindata.org/grapher/population-by-country?tab=data:

Decade

Population

1950

8639369

1960

9118700

1970

9637800

1980

9846800

1990

9969310

2000

10263618

We start by creating the MainApp class, which extends Application. We create a series of instance variables. The XYChart.Series class represents a series of data points for some plot. In our case, this will be for the decades and population, which we will initialize shortly. The next declaration is for the CategoryAxis and NumberAxis instances. These represent the X and Y axes respectively. The declaration for the Y axis includes range and increment values for the population. This makes the chart a bit more readable. The last declaration is a string variable for the country:

public class MainApp extends Application { 
    final XYChart.Series<String, Number> series =  
        new XYChart.Series<>(); 
    final CategoryAxis xAxis = new CategoryAxis(); 
    final NumberAxis yAxis =  
        new NumberAxis(8000000, 11000000, 1000000); 
    final static String belgium = "Belgium"; 
    ... 
} 

In JavaFX, the main method usually launches the application using the base class launch method. Eventually, the start method is called, which we override. In this example, we call the simpleLineChart method where the user interface is created:

public static void main(String[] args) { 
    launch(args); 
} 
 
public void start(Stage stage) { 
    simpleIndexChart (stage); 
} 

The simpleLineChart follows and is passed an instance of the Stage class. This represents the client area of the application's window. We start by setting a title for the application and the line chart proper. The label of the Y axis is set. An instance of the LineChart class is initialized using the X and Y axis instances. This class represents the line chart:

public void simpleIndexChart (Stage stage) { 
    stage.setTitle("Index Chart"); 
    lineChart.setTitle("Belgium Population"); 
    yAxis.setLabel("Population"); 
    final LineChart<String, Number> lineChart 
            = new LineChart<>(xAxis, yAxis); 
 
    ... 
} 

The series is given a name, and then the population for each decade is added to the series using the addDataItem helper method:

series.setName("Population"); 
addDataItem(series, "1950", 8639369); 
addDataItem(series, "1960", 9118700); 
addDataItem(series, "1970", 9637800); 
addDataItem(series, "1980", 9846800); 
addDataItem(series, "1990", 9969310); 
addDataItem(series, "2000", 10263618); 

The addDataItem method follows, which creates an XYChart.Data class instance using the String and Number values passed to it. It then adds the instance to the series:

public void addDataItem(XYChart.Series<String, Number> series, 
        String x, Number y) { 
    series.getData().add(new XYChart.Data<>(x, y)); 
} 

The last part of the simpleLineChart method creates a Scene class instance that represents the content of the stage. JavaFX uses the concept of a stage and scene to deal with the internals of the application's GUI.

The scene is created using a line chart, and the application's size is set to 800 by 600 pixels. The series is then added to the line chart and scene is added to stage. The show method displays the application:

Scene scene = new Scene(lineChart, 800, 600); 
lineChart.getData().add(series); 
stage.setScene(scene); 
stage.show(); 

When the application executes the following window will be displayed:

Creating index charts
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.227.82