JavaFX - 線形圖



線形圖或線圖以一系列資料點(標記)的形式顯示資訊,這些資料點透過直線段連線。線形圖顯示資料在相等的時間頻率下如何變化。

以下是描繪不同年份學校數量的線形圖。

Line Chart

JavaFX 中的線形圖

在 JavaFX 中,線形圖由名為 LineChart 的類表示。此類屬於 javafx.scene.chart 包。透過例項化此類,您可以在 JavaFX 中建立 LineChart 節點。

要在 JavaFX 中生成線形圖,您應該按照以下步驟操作。

步驟 1:定義軸

在 Application 類的 start() 方法中定義線形圖的 X 軸和 Y 軸,併為其設定標籤。在我們的示例中,X 軸表示從 1960 年到 2020 年的年份,每十年有一個主要刻度標記。

public class ClassName extends Application { 
   @Override     
   public void start(Stage primaryStage) throws Exception {
      //Defining X axis  
      NumberAxis xAxis = new NumberAxis(1960, 2020, 10); 
      xAxis.setLabel("Years"); 
        
      //Defining y axis 
      NumberAxis yAxis = new NumberAxis(0, 350, 50); 
      yAxis.setLabel("No.of schools");   
   }    
}

步驟 2:建立線形圖

透過例項化 javafx.scene.chart 包中名為 LineChart 的類來建立線形圖。在該類的建構函式中,傳遞表示在上一步驟中建立的 X 軸和 Y 軸的物件。

LineChart linechart = new LineChart(xAxis, yAxis);

步驟 3:準備資料

例項化 XYChart.Series 類。然後將資料(一系列 x 和 y 座標)新增到此類的 Observable 列表中,如下所示:

XYChart.Series series = new XYChart.Series(); 
series.setName("No of schools in an year"); 
        
series.getData().add(new XYChart.Data(1970, 15)); 
series.getData().add(new XYChart.Data(1980, 30)); 
series.getData().add(new XYChart.Data(1990, 60)); 
series.getData().add(new XYChart.Data(2000, 120)); 
series.getData().add(new XYChart.Data(2013, 240)); 
series.getData().add(new XYChart.Data(2014, 300));

步驟 4:將資料新增到線形圖

將上一步驟中準備的資料系列新增到線形圖中,如下所示:

//Setting the data to Line chart    
linechart.getData().add(series);

步驟 5:建立 Group 物件

start() 方法中,透過例項化名為 Group 的類來建立一個組物件。這屬於 javafx.scene 包。

將上一步驟中建立的 LineChart(節點)物件作為引數傳遞給 Group 類的建構函式。應執行此操作以將其新增到組中,如下所示:

Group root = new Group(linechart);

步驟 6:啟動應用程式

最後,請按照以下步驟正確啟動應用程式:

  • 首先,透過將 Group 物件作為引數值傳遞給其建構函式來例項化名為 Scene 的類。在此建構函式中,您還可以將應用程式螢幕的尺寸作為可選引數傳遞。

  • 然後,使用 Stage 類的 setTitle() 方法為舞臺設定標題。

  • 現在,使用名為 Stage 的類的 setScene() 方法將 Scene 物件新增到舞臺。

  • 使用名為 show() 的方法顯示場景的內容。

  • 最後,藉助 launch() 方法啟動應用程式。

示例

下表顯示了從 1970 年到 2014 年某個地區學校的數量。

年份 學校數量
1970 15
1980 30
1990 60
2000 120
2013 240
2014 300

以下是一個 Java 程式,它使用 JavaFX 生成一個描繪上述資料的線形圖。

將此程式碼儲存在名為 LineChartExample.java 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
         
public class LineChartExample extends Application { 
   @Override 
   public void start(Stage stage) {
      //Defining the x axis             
      NumberAxis xAxis = new NumberAxis(1960, 2020, 10); 
      xAxis.setLabel("Years"); 
        
      //Defining the y axis   
      NumberAxis yAxis = new NumberAxis   (0, 350, 50); 
      yAxis.setLabel("No.of schools"); 
        
      //Creating the line chart 
      LineChart linechart = new LineChart(xAxis, yAxis);  
        
      //Prepare XYChart.Series objects by setting data 
      XYChart.Series series = new XYChart.Series(); 
      series.setName("No of schools in an year"); 
        
      series.getData().add(new XYChart.Data(1970, 15)); 
      series.getData().add(new XYChart.Data(1980, 30)); 
      series.getData().add(new XYChart.Data(1990, 60)); 
      series.getData().add(new XYChart.Data(2000, 120)); 
      series.getData().add(new XYChart.Data(2013, 240)); 
      series.getData().add(new XYChart.Data(2014, 300)); 
            
      //Setting the data to Line chart    
      linechart.getData().add(series);        
        
      //Creating a Group object  
      Group root = new Group(linechart); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 400);  
      
      //Setting title to the Stage 
      stage.setTitle("Line Chart"); 
         
      //Adding scene to the stage 
      stage.setScene(scene);
	   
      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls LineChartExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls LineChartExample

輸出

執行上述程式後,會生成一個 JavaFX 視窗,其中顯示如下所示的線形圖。

Line Chart Example

示例

下表說明了每月銷售的電子產品數量。

產品 銷量(每月)
筆記型電腦 176
電視 30
手機 540
智慧手錶 250
MacBook 60

在另一個示例中,使用 JavaFX 生成一個描繪上述資料的線形圖。將此程式碼儲存在名為 LineChartItems.java 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.CategoryAxis; 
import javafx.scene.chart.XYChart; 
         
public class LineChartItems extends Application { 
   @Override 
   public void start(Stage stage) {
      //Defining the x axis             
      CategoryAxis xAxis = new CategoryAxis(); 
      xAxis.setLabel("Items"); 
        
      //Defining the y axis   
      NumberAxis yAxis = new NumberAxis(); 
      yAxis.setLabel("Sales (per month)"); 
        
      //Creating the line chart 
      LineChart linechart = new LineChart(xAxis, yAxis);  
        
      //Prepare XYChart.Series objects by setting data 
      XYChart.Series series = new XYChart.Series(); 
      series.setName("Items sold per month"); 
        
      series.getData().add(new XYChart.Data("Laptop", 176)); 
      series.getData().add(new XYChart.Data("TV", 30)); 
      series.getData().add(new XYChart.Data("Mobile", 540)); 
      series.getData().add(new XYChart.Data("Smart Watch", 250)); 
      series.getData().add(new XYChart.Data("MacBook", 60));         
         
      //Creating a scene object 
      Scene scene = new Scene(linechart, 600, 400); 
	  //Setting the data to Line chart    
      linechart.getData().add(series);
      
      //Setting title to the Stage 
      stage.setTitle("Line Chart"); 
         
      //Adding scene to the stage 
      stage.setScene(scene);
	   
      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls LineChartItems.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls LineChartItems

輸出

執行上述程式後,會生成一個 JavaFX 視窗,其中顯示如下所示的線形圖。

Line Chart Example
廣告