JavaFX - 氣泡圖



氣泡圖用於繪製三維資料;第三維將由氣泡的大小(半徑)表示。

以下是描繪已完成工作的示例氣泡圖。

Bubble Chart

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

生成氣泡圖的步驟

要在 JavaFX 中生成氣泡圖,請按照以下步驟操作。

步驟 1:建立類

建立一個 Java 類並繼承 javafx.application 包的 Application 類。您可以如下實現此類的 start() 方法。

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {     
   }    
 }

步驟 2:定義軸

定義氣泡圖的 X 軸和 Y 軸,併為其設定標籤。在我們的示例中,X 軸表示年齡,Y 軸表示體重。而氣泡的半徑表示已完成的工作。

//Defining the X axis               
NumberAxis xAxis = new NumberAxis(0, 100, 10);        
xAxis.setLabel("Age");  

//Defining Y axis        
NumberAxis yAxis = new NumberAxis(20, 100, 10); 
yAxis.setLabel("Weight");

步驟 3:建立氣泡圖

透過例項化 javafx.scene.chart 包中名為 BubbleChart 的類來建立氣泡圖。在該類的建構函式中,傳遞表示在先前步驟中建立的 X 軸和 Y 軸的物件。

//Creating the Bubble chart 
BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis);

步驟 4:準備資料

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

//Prepare XYChart.Series objects by setting data        
XYChart.Series series = new XYChart.Series();  
series.setName("work");  

series.getData().add(new XYChart.Data(10,30,4));  
series.getData().add(new XYChart.Data(25,40,5)); 
series.getData().add(new XYChart.Data(40,50,9)); 
series.getData().add(new XYChart.Data(55,60,7));    
series.getData().add(new XYChart.Data(70,70,9));        
series.getData().add(new XYChart.Data(85,80,6)); 

步驟 5:將資料新增到氣泡圖

將先前步驟中準備的資料系列新增到氣泡圖中,如下所示:

//Setting the data to bar chart        
bubbleChart.getData().add(series); 

步驟 6:建立 Group 物件

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

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

Group root = new Group(bubbleChart);

步驟 7:建立 Scene 物件

透過例項化名為 Scene 的類來建立一個 Scene,該類屬於 javafx.scene 包。在此類中,傳遞先前步驟中建立的 Group 物件 (root)。

除了根物件之外,您還可以傳遞兩個表示螢幕高度和寬度的雙精度引數,以及 Group 類的物件,如下所示。

Scene scene = new Scene(group ,600, 300);

步驟 8:設定 Stage 的標題

您可以使用 Stage 類的 setTitle() 方法設定 Stage 的標題。primaryStage 是一個 Stage 物件,它作為引數傳遞給 Scene 類的 start 方法。

使用 primaryStage 物件,將 Scene 的標題設定為 Sample Application,如下所示。

primaryStage.setTitle("Sample Application");

步驟 9:將 Scene 新增到 Stage

您可以使用名為 Stage 的類的 setScene() 方法將 Scene 物件新增到 Stage。使用以下方法新增在先前步驟中準備的 Scene 物件。

primaryStage.setScene(scene);

步驟 10:顯示 Stage 的內容

使用名為 Stage 類的 show() 方法顯示 Scene 的內容,如下所示。

primaryStage.show();

步驟 11:啟動應用程式

透過從 main 方法呼叫 Application 類的靜態方法 launch() 來啟動 JavaFX 應用程式,如下所示。

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

示例

讓我們考慮不同的人以及他們的年齡、體重和工作能力。工作能力可以視為以圖表中的氣泡形式繪製的小時數。

體重
年齡
30 40 50 60 70 80
10 4 工作
25 5
40 6
55 8
70 9
85 15

以下是一個 Java 程式,它使用 JavaFX 生成一個氣泡圖,描繪了以上資料。

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

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.chart.BubbleChart; 
import javafx.stage.Stage;  
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
         
public class BubbleChartExample extends Application { 
   @Override 
   public void start(Stage stage) {     
      //Defining the axes               
      NumberAxis xAxis = new NumberAxis(0, 100, 10);        
      xAxis.setLabel("Age"); 
        
      NumberAxis yAxis = new NumberAxis(20, 100, 10); 
      yAxis.setLabel("Weight"); 
      
      //Creating the Bubble chart 
      BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis);    
         
      //Prepare XYChart.Series objects by setting data        
      XYChart.Series series = new XYChart.Series();  
      series.setName("work"); 
         
      series.getData().add(new XYChart.Data(10,30,4));  
      series.getData().add(new XYChart.Data(25,40,5)); 
      series.getData().add(new XYChart.Data(40,50,9)); 
      series.getData().add(new XYChart.Data(55,60,7));    
      series.getData().add(new XYChart.Data(70,70,9));        
      series.getData().add(new XYChart.Data(85,80,6));
      
      //Setting the data to bar chart         
      bubbleChart.getData().add(series); 
         
      //Creating a Group object  
      Group root = new Group(bubbleChart); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 400);  
      
      //Setting title to the Stage 
      stage.setTitle("Bubble 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 BubbleChartExample.java 
java BubbleChartExample

執行後,上述程式將生成一個 JavaFX 視窗,顯示如下所示的氣泡圖。

Bubblechart Example
javafx_charts.htm
廣告

© . All rights reserved.