Creating bar charts

A bar chart uses two axes with rectangular bars that can be either positioned either vertically or horizontally. The length of a bar is proportional to the value it represents. A bar chart can be used to show time series data.

In the following series of examples, we will be using a set of European country populations for three decades, as listed in the following table. The data is a subset of population data found at https://ourworldindata.org/grapher/population-by-country?tab=data:

Country

1950

1960

1970

Belgium

8,639,369

9,118,700

9,637,800

France

42,518,000

46,584,000

51,918,000

Germany

68,374,572

72,480,869

77,783,164

Netherlands

10,113,527

11,486,000

13,032,335

Sweden

7,014,005

7,480,395

8,042,803

United Kingdom

50,127,000

52,372,000

55,632,000

The first of three bar charts will be constructed using JavaFX. We start with a series of declarations for the countries as part of a class that extends the Application class:

public class MainApp extends Application { 
    final static String belgium = "Belgium"; 
    final static String france = "France"; 
    final static String germany = "Germany"; 
    final static String netherlands = "Netherlands"; 
    final static String sweden = "Sweden"; 
    final static String unitedKingdom = "United Kingdom"; 
 
... 
} 

Next, we declared a series of instance variables that represent the parts of a graph. The first are CategoryAxis and NumberAxis instances:

final CategoryAxis xAxis = new CategoryAxis(); 
final NumberAxis yAxis = new NumberAxis(); 

The population and country data is stored in a series of XYChart.Series instances. Here, we have declared six different series, which use a string and number pair. The first example does not use all six series, but later examples will. We will initially assign a country string and its corresponding population to three series. These series will represent the populations for the decades 1950, 1960, and 1970:

final XYChart.Series<String, Number> series1 =  
    new XYChart.Series<>(); 
final XYChart.Series<String, Number> series2  
    new XYChart.Series<>(); 
final XYChart.Series<String, Number> series3 =  
    new XYChart.Series<>(); 
final XYChart.Series<String, Number> series4 =  
    new XYChart.Series<>(); 
final XYChart.Series<String, Number> series5 =  
    new XYChart.Series<>(); 
final XYChart.Series<String, Number> series6 =  
    new XYChart.Series<>(); 

We will start with two simple bar charts. The first one will show the countries as categories where the year changes occur within the category on the X axis and the population along the Y axis. The second shows the decades as categories containing the counties. The last example is a stacked bar chart.

Using country as the category

The elements of the bar chart are set up in the simpleBarChartByCountry method. The title of the chart is set and a BarChart class instance is created using the two axes. The chart, its X axis, and its Y axis also have labels that are initialized here:

public void simpleBarChartByCountry(Stage stage) { 
    stage.setTitle("Bar Chart"); 
    final BarChart<String, Number> barChart 
                = new BarChart<>(xAxis, yAxis); 
    barChart.setTitle("Country Summary"); 
    xAxis.setLabel("Country"); 
    yAxis.setLabel("Population"); 
    ... 
} 

Next, the first three series are initialized with a name, and then the country and population data for that series. A helper method, addDataItem, as introduced in the previous section, is used to add the data to each series:

series1.setName("1950"); 
addDataItem(series1,belgium, 8639369); 
addDataItem(series1,france, 42518000); 
addDataItem(series1,germany, 68374572); 
addDataItem(series1,netherlands, 10113527); 
addDataItem(series1,sweden, 7014005); 
addDataItem(series1,unitedKingdom, 50127000); 
 
series2.setName("1960"); 
addDataItem(series2,belgium, 9118700); 
addDataItem(series2,france, 46584000); 
addDataItem(series2,germany, 72480869); 
addDataItem(series2,netherlands, 11486000); 
addDataItem(series2,sweden, 7480395); 
addDataItem(series2,unitedKingdom, 52372000); 
 
series3.setName("1970"); 
addDataItem(series3,belgium, 9637800); 
addDataItem(series3,france, 51918000); 
addDataItem(series3,germany, 77783164); 
addDataItem(series3,netherlands, 13032335); 
addDataItem(series3,sweden, 8042803); 
addDataItem(series3,unitedKingdom, 55632000); 

The last part of the method creates a scene instance. The three series are added to the scene and the scene is attached to the stage using the setScene method. A stage is a class that essentially represents the client area of a window:

Scene scene = new Scene(barChart, 800, 600); 
barChart.getData().addAll(series1, series2, series3); 
stage.setScene(scene); 
stage.show(); 

The last of the two methods is the start method, which is called automatically when the window is displayed. It is passed the Stage instance. Here, we call the simpleBarChartByCountry method:

public void start(Stage stage) { 
    simpleBarChartByCountry(stage); 
} 

The main method consists of a call to the Application class's launch method:

public static void main(String[] args) { 
    launch(args); 
} 

When the application is executed, the following graph is displayed:

Using country as the category

Using decade as the category

In the following example, we will demonstrate how to display the same information, but we will organize the X axis categories by year. We will use the simpleBarChartByYear method, as shown next. The axis and titles are set up in the same way as before, but with different values for the title and labels:

public void simpleBarChartByYear(Stage stage) { 
    stage.setTitle("Bar Chart"); 
    final BarChart<String, Number> barChart 
            = new BarChart<>(xAxis, yAxis); 
    barChart.setTitle("Year Summary"); 
    xAxis.setLabel("Year"); 
    yAxis.setLabel("Population"); 
    ... 
} 

The following string variables are declared for the three decades:

String year1950 = "1950"; 
String year1960 = "1960"; 
String year1970 = "1970"; 

The data series are created in the same way as before, except the country name is used for the series name and the year is used for the category. In addition, six series are used, one for each country:

series1.setName(belgium); 
addDataItem(series1, year1950, 8639369); 
addDataItem(series1, year1960, 9118700); 
addDataItem(series1, year1970, 9637800); 
 
series2.setName(france); 
addDataItem(series2, year1950, 42518000); 
addDataItem(series2, year1960, 46584000); 
addDataItem(series2, year1970, 51918000); 
 
series3.setName(germany); 
addDataItem(series3, year1950, 68374572); 
addDataItem(series3, year1960, 72480869); 
addDataItem(series3, year1970, 77783164); 
 
series4.setName(netherlands); 
addDataItem(series4, year1950, 10113527); 
addDataItem(series4, year1960, 11486000); 
addDataItem(series4, year1970, 13032335); 
 
series5.setName(sweden); 
addDataItem(series5, year1950, 7014005); 
addDataItem(series5, year1960, 7480395); 
addDataItem(series5, year1970, 8042803); 
 
series6.setName(unitedKingdom); 
addDataItem(series6, year1950, 50127000); 
addDataItem(series6, year1960, 52372000); 
addDataItem(series6, year1970, 55632000); 

The scene is created and attached to the stage:

Scene scene = new Scene(barChart, 800, 600); 
barChart.getData().addAll(series1, series2,  
    series3, series4, series5, series6); 
stage.setScene(scene); 
stage.show(); 

The main method is unchanged, but the start method calls the simpleBarChartByYear method instead:

public void start(Stage stage) { 
    simpleBarChartByYear(stage); 
} 

When the application is executed, the following graph is displayed:

Using decade as the category
..................Content has been hidden....................

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